Push Changes from Techcrafters Repo
This commit is contained in:
@@ -1,218 +1,216 @@
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Text;
|
||||
|
||||
namespace HMIToolkit
|
||||
{
|
||||
// PLC - C#
|
||||
// --------
|
||||
// int - short
|
||||
// word - ushort
|
||||
namespace HMIToolkit;
|
||||
// PLC - C#
|
||||
// --------
|
||||
// int - short
|
||||
// word - ushort
|
||||
|
||||
// Constants for interaction with data
|
||||
public static class HMIConstants
|
||||
{
|
||||
public const int StringLength = 81;
|
||||
public const int NumInterlocks = 16;
|
||||
}
|
||||
// Constants for interaction with data
|
||||
public static class HMIConstants
|
||||
{
|
||||
public const int StringLength = 81;
|
||||
public const int NumInterlocks = 16;
|
||||
}
|
||||
|
||||
|
||||
[StructLayout(LayoutKind.Sequential, Pack = 0)]
|
||||
public struct HMIAnalogValue
|
||||
{
|
||||
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = HMIConstants.StringLength)]
|
||||
public string sName;
|
||||
[StructLayout(LayoutKind.Sequential, Pack = 0)]
|
||||
public struct HMIAnalogValue
|
||||
{
|
||||
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = HMIConstants.StringLength)]
|
||||
public string sName;
|
||||
|
||||
// 1 = Ok, 2 = Error
|
||||
public short iStatus;
|
||||
// 1 = Ok, 2 = Error
|
||||
public short iStatus;
|
||||
|
||||
public float rValue;
|
||||
public float rValue;
|
||||
|
||||
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = HMIConstants.StringLength)]
|
||||
public string sUnit;
|
||||
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = HMIConstants.StringLength)]
|
||||
public string sUnit;
|
||||
|
||||
[MarshalAs(UnmanagedType.I1)]
|
||||
public bool xUsed;
|
||||
}
|
||||
[MarshalAs(UnmanagedType.I1)]
|
||||
public bool xUsed;
|
||||
}
|
||||
|
||||
// TwinCAT2 Pack = 1, TwinCAT 3 Pack = 0
|
||||
[StructLayout(LayoutKind.Sequential, Pack = 0)]
|
||||
public struct HMIControlButton
|
||||
{
|
||||
[MarshalAs(UnmanagedType.I1)]
|
||||
public bool xRequest;
|
||||
// TwinCAT2 Pack = 1, TwinCAT 3 Pack = 0
|
||||
[StructLayout(LayoutKind.Sequential, Pack = 0)]
|
||||
public struct HMIControlButton
|
||||
{
|
||||
[MarshalAs(UnmanagedType.I1)]
|
||||
public bool xRequest;
|
||||
|
||||
[MarshalAs(UnmanagedType.I1)]
|
||||
public bool xRelease;
|
||||
[MarshalAs(UnmanagedType.I1)]
|
||||
public bool xRelease;
|
||||
|
||||
public short iFeedback;
|
||||
}
|
||||
public short iFeedback;
|
||||
}
|
||||
|
||||
// TwinCAT2 Pack = 1, TwinCAT 3 Pack = 0
|
||||
[StructLayout(LayoutKind.Sequential, Pack = 0)]
|
||||
public struct HMIInterlock
|
||||
{
|
||||
public ushort wProcessINTLKStatus;
|
||||
// TwinCAT2 Pack = 1, TwinCAT 3 Pack = 0
|
||||
[StructLayout(LayoutKind.Sequential, Pack = 0)]
|
||||
public struct HMIInterlock
|
||||
{
|
||||
public ushort wProcessINTLKStatus;
|
||||
|
||||
public ushort wSafeyINTLKStatus;
|
||||
public ushort wSafeyINTLKStatus;
|
||||
|
||||
public ushort wProcessINTLKUsed;
|
||||
public ushort wProcessINTLKUsed;
|
||||
|
||||
public ushort wSafeyINTLKUsed;
|
||||
public ushort wSafeyINTLKUsed;
|
||||
|
||||
// 16 * String(80) = 81 bytes a 16 indexes
|
||||
// combined in one string because reading a two dimensional array is not possible
|
||||
[MarshalAs(UnmanagedType.ByValArray, SizeConst = HMIConstants.StringLength * HMIConstants.NumInterlocks)]
|
||||
public byte[] asProcessINTLKName;
|
||||
// 16 * String(80) = 81 bytes a 16 indexes
|
||||
// combined in one string because reading a two dimensional array is not possible
|
||||
[MarshalAs(UnmanagedType.ByValArray, SizeConst = HMIConstants.StringLength * HMIConstants.NumInterlocks)]
|
||||
public byte[] asProcessINTLKName;
|
||||
|
||||
// 16 * String(80) = 81 bytes a 16 indexes
|
||||
// combined in one string because reading a two dimensional array is not possible
|
||||
[MarshalAs(UnmanagedType.ByValArray, SizeConst = HMIConstants.StringLength * HMIConstants.NumInterlocks)]
|
||||
public byte[] asSafetyINTLKName;
|
||||
// 16 * String(80) = 81 bytes a 16 indexes
|
||||
// combined in one string because reading a two dimensional array is not possible
|
||||
[MarshalAs(UnmanagedType.ByValArray, SizeConst = HMIConstants.StringLength * HMIConstants.NumInterlocks)]
|
||||
public byte[] asSafetyINTLKName;
|
||||
|
||||
[MarshalAs(UnmanagedType.I1)]
|
||||
public bool xProcessINTLKOk;
|
||||
[MarshalAs(UnmanagedType.I1)]
|
||||
public bool xProcessINTLKOk;
|
||||
|
||||
[MarshalAs(UnmanagedType.I1)]
|
||||
public bool xSafetyINTLKOk;
|
||||
}
|
||||
[MarshalAs(UnmanagedType.I1)]
|
||||
public bool xSafetyINTLKOk;
|
||||
}
|
||||
|
||||
// TwinCAT2 Pack = 1, TwinCAT 3 Pack = 0
|
||||
[StructLayout(LayoutKind.Sequential, Pack = 0)]
|
||||
public struct HMIValveData
|
||||
{
|
||||
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = HMIConstants.StringLength)]
|
||||
public string sName;
|
||||
// TwinCAT2 Pack = 1, TwinCAT 3 Pack = 0
|
||||
[StructLayout(LayoutKind.Sequential, Pack = 0)]
|
||||
public struct HMIValveData
|
||||
{
|
||||
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = HMIConstants.StringLength)]
|
||||
public string sName;
|
||||
|
||||
public HMIControlButton stAutomaticButton;
|
||||
public HMIControlButton stAutomaticButton;
|
||||
|
||||
public HMIControlButton stManualButton;
|
||||
public HMIControlButton stManualButton;
|
||||
|
||||
public HMIControlButton stOpenButton;
|
||||
public HMIControlButton stOpenButton;
|
||||
|
||||
public HMIControlButton stCloseButton;
|
||||
public HMIControlButton stCloseButton;
|
||||
|
||||
// 1 = Opened, 2 = Opening/Closing, 3 = Closed, 4 = Error
|
||||
public short iStatus;
|
||||
// 1 = Opened, 2 = Opening/Closing, 3 = Closed, 4 = Error
|
||||
public short iStatus;
|
||||
|
||||
// 1 = Automatic mode, 2 = Manual mode
|
||||
public short iCurrentMode;
|
||||
// 1 = Automatic mode, 2 = Manual mode
|
||||
public short iCurrentMode;
|
||||
|
||||
public HMIInterlock stInterlock;
|
||||
public HMIInterlock stInterlock;
|
||||
|
||||
[MarshalAs(UnmanagedType.I1)]
|
||||
public bool xUsed;
|
||||
}
|
||||
[MarshalAs(UnmanagedType.I1)]
|
||||
public bool xUsed;
|
||||
}
|
||||
|
||||
[StructLayout(LayoutKind.Sequential, Pack = 0)]
|
||||
public struct HMIAnalogValveData
|
||||
{
|
||||
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = HMIConstants.StringLength)]
|
||||
public string sName;
|
||||
[StructLayout(LayoutKind.Sequential, Pack = 0)]
|
||||
public struct HMIAnalogValveData
|
||||
{
|
||||
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = HMIConstants.StringLength)]
|
||||
public string sName;
|
||||
|
||||
public HMIControlButton stAutomaticButton;
|
||||
public HMIControlButton stAutomaticButton;
|
||||
|
||||
public HMIControlButton stManualButton;
|
||||
public HMIControlButton stManualButton;
|
||||
|
||||
public HMIControlButton stOpenButton;
|
||||
public HMIControlButton stOpenButton;
|
||||
|
||||
public HMIControlButton stCloseButton;
|
||||
public HMIControlButton stCloseButton;
|
||||
|
||||
// 1 = Opened, 2 = Opening/Closing, 3 = Closed, 4 = Error
|
||||
public short iStatus;
|
||||
// 1 = Opened, 2 = Opening/Closing, 3 = Closed, 4 = Error
|
||||
public short iStatus;
|
||||
|
||||
// 1 = Automatic mode, 2 = Manual mode
|
||||
public short iCurrentMode;
|
||||
// 1 = Automatic mode, 2 = Manual mode
|
||||
public short iCurrentMode;
|
||||
|
||||
public HMIInterlock stInterlock;
|
||||
public HMIInterlock stInterlock;
|
||||
|
||||
HMIAnalogValue stSetpoint;
|
||||
HMIAnalogValue stSetpoint;
|
||||
|
||||
HMIAnalogValue stProcessValue;
|
||||
HMIAnalogValue stProcessValue;
|
||||
|
||||
[MarshalAs(UnmanagedType.I1)]
|
||||
public bool xUsed;
|
||||
}
|
||||
[MarshalAs(UnmanagedType.I1)]
|
||||
public bool xUsed;
|
||||
}
|
||||
|
||||
// TwinCAT2 Pack = 1, TwinCAT 3 Pack = 0
|
||||
[StructLayout(LayoutKind.Sequential, Pack = 0)]
|
||||
public struct HMIAnalogMotorData
|
||||
{
|
||||
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = HMIConstants.StringLength)]
|
||||
public string sName;
|
||||
// TwinCAT2 Pack = 1, TwinCAT 3 Pack = 0
|
||||
[StructLayout(LayoutKind.Sequential, Pack = 0)]
|
||||
public struct HMIAnalogMotorData
|
||||
{
|
||||
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = HMIConstants.StringLength)]
|
||||
public string sName;
|
||||
|
||||
public HMIControlButton stAutomaticButton;
|
||||
public HMIControlButton stAutomaticButton;
|
||||
|
||||
public HMIControlButton stManualButton;
|
||||
public HMIControlButton stManualButton;
|
||||
|
||||
public HMIControlButton stStartButton;
|
||||
public HMIControlButton stStartButton;
|
||||
|
||||
public HMIControlButton stStopButton;
|
||||
public HMIControlButton stStopButton;
|
||||
|
||||
// 1 = Opened, 2 = Opening/Closing, 3 = Closed, 4 = Error
|
||||
public short iStatus;
|
||||
// 1 = Opened, 2 = Opening/Closing, 3 = Closed, 4 = Error
|
||||
public short iStatus;
|
||||
|
||||
// 1 = Automatic mode, 2 = Manual mode
|
||||
public short iCurrentMode;
|
||||
// 1 = Automatic mode, 2 = Manual mode
|
||||
public short iCurrentMode;
|
||||
|
||||
public HMIAnalogValue stSetpoint;
|
||||
public HMIAnalogValue stSetpoint;
|
||||
|
||||
public HMIAnalogValue stProcessValue;
|
||||
public HMIAnalogValue stProcessValue;
|
||||
|
||||
public HMIInterlock stInterlock;
|
||||
public HMIInterlock stInterlock;
|
||||
|
||||
[MarshalAs(UnmanagedType.I1)]
|
||||
public bool xUsed;
|
||||
}
|
||||
[MarshalAs(UnmanagedType.I1)]
|
||||
public bool xUsed;
|
||||
}
|
||||
|
||||
// TwinCAT2 Pack = 1, TwinCAT 3 Pack = 0
|
||||
[StructLayout(LayoutKind.Sequential, Pack = 0)]
|
||||
public struct HMIOrpSensorData
|
||||
{
|
||||
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = HMIConstants.StringLength)]
|
||||
public string sName;
|
||||
// TwinCAT2 Pack = 1, TwinCAT 3 Pack = 0
|
||||
[StructLayout(LayoutKind.Sequential, Pack = 0)]
|
||||
public struct HMIOrpSensorData
|
||||
{
|
||||
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = HMIConstants.StringLength)]
|
||||
public string sName;
|
||||
|
||||
// 1 = Ok, 2 = Error
|
||||
public short iStatus;
|
||||
// 1 = Ok, 2 = Error
|
||||
public short iStatus;
|
||||
|
||||
public float rValuePH;
|
||||
public float rValuePH;
|
||||
|
||||
public float rValueTemp;
|
||||
public float rValueTemp;
|
||||
|
||||
public float rValueORP;
|
||||
public float rValueORP;
|
||||
|
||||
public float rValueDLI;
|
||||
public float rValueDLI;
|
||||
|
||||
[MarshalAs(UnmanagedType.I1)]
|
||||
public bool xUsed;
|
||||
}
|
||||
[MarshalAs(UnmanagedType.I1)]
|
||||
public bool xUsed;
|
||||
}
|
||||
|
||||
static class HMIUtilities
|
||||
{
|
||||
// Converts the interlock byte array into a string array
|
||||
public static string[] GetInterlockStringArray(byte[] byteArray)
|
||||
{
|
||||
string[] temp = new string[HMIConstants.NumInterlocks];
|
||||
int size;
|
||||
static class HMIUtilities
|
||||
{
|
||||
// Converts the interlock byte array into a string array
|
||||
public static string[] GetInterlockStringArray(byte[] byteArray)
|
||||
{
|
||||
string[] temp = new string[HMIConstants.NumInterlocks];
|
||||
int size;
|
||||
|
||||
// Check if byteArray is of correct size
|
||||
if (byteArray.Length != (HMIConstants.StringLength * HMIConstants.NumInterlocks))
|
||||
return temp;
|
||||
// Check if byteArray is of correct size
|
||||
if (byteArray.Length != (HMIConstants.StringLength * HMIConstants.NumInterlocks))
|
||||
return temp;
|
||||
|
||||
for (int i = 0; i < HMIConstants.NumInterlocks; i++)
|
||||
{
|
||||
// Calculate length of string by finding the 0 terminator so the unused bytes get truncated
|
||||
size = Array.IndexOf(byteArray, (byte)0, i * HMIConstants.StringLength) - (i * HMIConstants.StringLength);
|
||||
for (int i = 0; i < HMIConstants.NumInterlocks; i++)
|
||||
{
|
||||
// Calculate length of string by finding the 0 terminator so the unused bytes get truncated
|
||||
size = Array.IndexOf(byteArray, (byte)0, i * HMIConstants.StringLength) - (i * HMIConstants.StringLength);
|
||||
|
||||
// Check if we found a valid 0 terminator
|
||||
if (size >= 0)
|
||||
// Build string from byteArray with calculated size
|
||||
temp[i] = Encoding.ASCII.GetString(byteArray, i * HMIConstants.StringLength, size);
|
||||
else
|
||||
// No valid 0 string terminator was found so return an empty string
|
||||
temp[i] = "";
|
||||
}
|
||||
// Check if we found a valid 0 terminator
|
||||
if (size >= 0)
|
||||
// Build string from byteArray with calculated size
|
||||
temp[i] = Encoding.ASCII.GetString(byteArray, i * HMIConstants.StringLength, size);
|
||||
else
|
||||
// No valid 0 string terminator was found so return an empty string
|
||||
temp[i] = "";
|
||||
}
|
||||
|
||||
return temp;
|
||||
}
|
||||
}
|
||||
return temp;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user