MCProtocol.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Runtime.InteropServices;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. namespace MECF.Framework.RT.Core.IoProviders
  8. {
  9. public partial class MCProtocol
  10. {
  11. ////
  12. ///
  13. ///
  14. /// Melsec Protocol QnA compatible 3E frame Constant and Message Format Definition
  15. ///
  16. ///
  17. ///
  18. ///
  19. public const int CMD_BATCH_READ = 0x0401;
  20. public const int CMD_BATCH_WRITE = 0x1401;
  21. public const int CMD_RANDOM_READ = 0x0403;
  22. public const int CMD_RANDOM_WRITE = 0x1402;
  23. public const int SUB_CMD_WORD_UNITS = 0;
  24. public const int SUB_CMD_BYTE_UNITS = 1;
  25. public static readonly int JUNK_SIZE = Marshal.SizeOf(typeof(ushort)); // RESERVED
  26. static public readonly int PLCDataSize = Marshal.SizeOf(typeof(ushort));
  27. static public readonly int PLCDataBits = PLCDataSize * 8;
  28. static public readonly int BufDataSize = Marshal.SizeOf(typeof(byte));
  29. static public readonly int BufDataBits = BufDataSize * 8;
  30. static public readonly int IntSize = Marshal.SizeOf(typeof(int));
  31. static public readonly int IntBits = IntSize * 8;
  32. static public readonly int ShortSize = Marshal.SizeOf(typeof(ushort));
  33. // List of Commands and Functions for the QnA Compatible 3E Frame Device Memory Read/Write
  34. // refer to manual 3-62
  35. public const ushort MC_COMMAND_MULTIPLE_BATCH_READ = 0x0406;
  36. public const ushort MC_COMMAND_MULTIPLE_BATCH_WRITE = 0x1406;
  37. public const ushort MC_COMMAND_BATCH_READ = 0x0401;
  38. public const ushort MC_COMMAND_BATCH_WRITE = 0x1401;
  39. public const ushort MC_COMMAND_RANDOM_READ = 0x0403;
  40. public const ushort MC_COMMAND_RANDOM_WRITE = 0x1402;
  41. public const ushort MC_COMMAND_MONITOR_DATA_REGISTRATION = 0x0801;
  42. public const ushort MC_COMMAND_MONITOR = 0x0802;
  43. public const ushort MC_SUBCOMMAND_BIT_UNITS = 0x0001;
  44. public const ushort MC_SUBCOMMAND_WORD_UNITS = 0x0000;
  45. // List of Device code for QnACPU
  46. // refer to manual 3-70
  47. public const byte MC_DEVICE_CODE_INPUT_BIT = 0x9C;
  48. public const byte MC_DEVICE_CODE_OUTPUT_BIT = 0x0D;
  49. public const byte MC_DEVICE_CODE_INTERNAL_RELAY_BIT = 0x90;
  50. public const byte MC_DEVICE_CODE_LATCH_RELAY_BIT = 0x92;
  51. public const byte MC_DEVICE_CODE_ANNUNCIATOR_BIT = 0x93;
  52. public const byte MC_DEVICE_CODE_EDGE_RELAY_BIT = 0x94;
  53. public const byte MC_DEVICE_CODE_LINK_RELAY_BIT = 0xA0;
  54. public const byte MC_DEVICE_CODE_DATA_REGISTER_WORD = 0xA8;
  55. public const byte MC_DEVICE_CODE_LINK_REGISTER_WORD = 0xB4;
  56. // refer to manual 3-4
  57. public const ushort MC_SUBHEADER_COMMAND_MESSAGE = 0x0050;
  58. public const ushort MC_SUBHEADER_RESPONSE_MESSAGE = 0x00D0;
  59. // refer to manual 3-14
  60. public const ushort MC_REQUEST_MODULE_IO_NUMBER = 0x03FF;
  61. public const byte MC_REQUEST_MODULE_STATION_NUMBER = 0x00;
  62. public static readonly ushort MC_CPU_MONITOR_TIMER = 0x0008; // Wait time (Unit: 250ms)
  63. public const ushort MC_COMPLETE_CODE_SUCCESS = 0x0000;
  64. // User Defined Constant
  65. public static readonly ushort MC_NONE_DESIGNATE_DATA_LENGTH = 0x0000;
  66. public static readonly int MC_SUBHEADER_SIZE = Marshal.SizeOf(MC_SUBHEADER_COMMAND_MESSAGE);
  67. public static readonly int MC_QHEADER_COMMAND_SIZE = Marshal.SizeOf(typeof(MC_COMMAND_HEADER));
  68. public static readonly int MC_QHEADER_RESPONSE_SIZE = Marshal.SizeOf(typeof(MC_RESPONSE_HEADER));
  69. public static readonly int MC_BATCH_COMMAND_SIZE = Marshal.SizeOf(typeof(MC_BATCH_COMMAND));
  70. //public static readonly int MC_POINT_BITS = sizeof(ushort) * 8;
  71. //public const int MC_CA_WRITE_SIZE = 1024;
  72. //public const int MC_COMMAND_DATA_SIZE = 1024;
  73. //public const int MC_IO_BUFFER_LENGTH = 1024;
  74. // Command Message Format
  75. //[Serializable, StructLayout(LayoutKind.Sequential, Pack = 1)]
  76. //public struct MC_DATA_FORMAT
  77. //{
  78. // public MC_HEADER mc_Header;
  79. // public MC_APPLICATION_DATA mc_ApplicationData;
  80. //};
  81. //[Serializable, StructLayout(LayoutKind.Sequential, Pack = 1)]
  82. //public struct MC_HEADER // Ethernet module adds and deletes the header
  83. //{
  84. //};
  85. //[Serializable, StructLayout(LayoutKind.Sequential, Pack = 1)]
  86. //public struct MC_APPLICATION_DATA
  87. //{
  88. // public MC_SUBHEADER mc_SubHeader;
  89. // public MC_TEXT mc_Text;
  90. //};
  91. //[Serializable, StructLayout(LayoutKind.Sequential, Pack = 1)]
  92. //public struct MC_SUBHEADER // 2 bytes (3E frame)
  93. //{
  94. // public enum SUBHEADER_TYPE : ushort
  95. // {
  96. // MC_COMMAND_MESSAGE = 0x0050,
  97. // MC_RESPONSE_MESSAGE = 0x00D0
  98. // };
  99. // public SUBHEADER_TYPE mc_SubHeaderType;
  100. //}
  101. //[Serializable, StructLayout(LayoutKind.Sequential, Pack = 1)]
  102. //public struct MC_TEXT // Text Command or Text Response
  103. //{
  104. //}
  105. //public const byte MC_LOCAL_STATION_MCSocket_NUMBER = 0x00;
  106. //public const byte MC_LOCAL_STATION_PC_NUMBER = 0xFF;
  107. // refer to manual 3-7
  108. //[Serializable, StructLayout(LayoutKind.Sequential, Pack = 1)]
  109. //public struct MC_QHEADER_COMMAND
  110. //{
  111. // public byte nMCSocketNo;
  112. // public byte nStationNo;
  113. // public ushort nRequestIONumber;
  114. // public byte nRequestStationNumber;
  115. // public ushort nRequestDataLength;
  116. // public ushort nCPUMonitorTimer;
  117. //}
  118. [Serializable, StructLayout(LayoutKind.Sequential, Pack = 1)]
  119. public struct MC_COMMAND_HEADER
  120. {
  121. public ushort ProtocolID; // MC_COMMAND_MESSAGE or MC_RESPONSE_MESSAGE
  122. public byte NetworkID;
  123. public byte StationID;
  124. public ushort RequestIONumber;
  125. public byte RequestStationNumber;
  126. public ushort RequestDataLen;
  127. public ushort CPUMonitorTimer;
  128. };// MC_COMMAND_HEADER;
  129. [Serializable, StructLayout(LayoutKind.Sequential, Pack = 1)]
  130. public struct MC_RESPONSE_HEADER
  131. {
  132. public ushort ProtocolId; //nSubheader
  133. public byte NetworkId; //SocketNumber
  134. public byte StationId;
  135. public ushort RequestIoNumber;
  136. public byte RequestStationNumber;
  137. public ushort ResponseDataLen;
  138. public ushort CompleteCode;
  139. };
  140. //[Serializable, StructLayout(LayoutKind.Sequential, Pack = 1)]
  141. //public struct MC_QHEADER_RESPONSE
  142. //{
  143. // public ushort nSubHeader;
  144. // public byte nMCSocketNo;
  145. // public byte nStationNo;
  146. // public ushort nRequestIONumber;
  147. // public byte nRequestStationNumber;
  148. // public ushort nResponseDataLength;
  149. // public ushort nCompleteCode;
  150. //}
  151. [Serializable, StructLayout(LayoutKind.Sequential, Pack = 1)]
  152. public struct MC_BATCH_COMMAND
  153. {
  154. public ushort Command;
  155. public ushort SubCommand;
  156. public ushort HeadAddr;
  157. public byte Reserved;
  158. public byte DeviceCode;
  159. public ushort DevicePoints;
  160. }
  161. //[Serializable, StructLayout(LayoutKind.Sequential, Pack = 1)]
  162. //public struct MC_HEAD_DEVICE
  163. //{
  164. // public byte bLow;
  165. // public byte bMid;
  166. // public byte bHig;
  167. // public int HeadDevice
  168. // {
  169. // get
  170. // {
  171. // return (bLow | bMid << sizeof(byte) | bHig << sizeof(byte) * 2);
  172. // }
  173. // set
  174. // {
  175. // bLow = (byte)(0x00 | value);
  176. // bMid = (byte)(0x00 | value >> sizeof(byte));
  177. // bHig = (byte)(0x00 | value >> sizeof(byte) * 2);
  178. // }
  179. // }
  180. //}
  181. //[Serializable, StructLayout(LayoutKind.Sequential, Pack = 1)]
  182. //public struct MC_CA_RANDOM_WRITE_BIT
  183. //{
  184. // public ushort nCommand;
  185. // public ushort nSubCommand;
  186. // public byte nBitPoints;
  187. // public DATA_RANDOM_WRITE_BIT[] data;
  188. // [Serializable, StructLayout(LayoutKind.Sequential, Pack = 1)]
  189. // public struct DATA_RANDOM_WRITE_BIT
  190. // {
  191. // public ushort nHeadDeviceAddr;
  192. // public byte nReserved;
  193. // public byte nDeviceCode;
  194. // public byte nSet;
  195. // }
  196. // byte[] ToByteArray() // can serialize with pointer type?
  197. // {
  198. // byte[] buffer = new byte[0];
  199. // return buffer;
  200. // }
  201. //};
  202. public static byte[] TransBoolArrayToByteData(bool[] value)
  203. {
  204. int length = (value.Length + 1) / 2;
  205. byte[] buffer = new byte[length];
  206. for (int i = 0; i < length; i++)
  207. {
  208. if (value[i * 2 + 0])
  209. buffer[i] += 0x10;
  210. if ((i * 2 + 1) < value.Length)
  211. {
  212. if (value[i * 2 + 1])
  213. buffer[i] += 0x01;
  214. }
  215. }
  216. return buffer;
  217. }
  218. public static bool[] TransByteDataToBoolArray(byte[] value, int offset, int size)
  219. {
  220. int count = size * 2;
  221. bool[] result = new bool[count];
  222. for (int i = 0; i < size; i++)
  223. {
  224. if ((value[i] & 0x10) == 0x10)
  225. {
  226. result[i * 2 + 0] = true;
  227. }
  228. if ((value[i] & 0x01) == 0x01)
  229. {
  230. result[i * 2 + 1] = true;
  231. }
  232. }
  233. return result;
  234. }
  235. public static T ToStruct<T>(byte[] by) where T : struct
  236. {
  237. int objectSize = Marshal.SizeOf(typeof(T));
  238. if (objectSize > by.Length) return default(T);
  239. // Allocate some unmanaged memory.
  240. IntPtr buffer = Marshal.AllocHGlobal(objectSize);
  241. // Copy the read byte array (byte[]) into the unmanaged memory block.
  242. Marshal.Copy(by, 0, buffer, objectSize);
  243. // Push the memory into a new struct of type (T).
  244. T returnStruct = (T)Marshal.PtrToStructure(buffer, typeof(T));
  245. // Free the unmanaged memory block.
  246. Marshal.FreeHGlobal(buffer);
  247. return returnStruct;
  248. }
  249. public static object ToStruct(byte[] buffer, Type t)
  250. {
  251. int objectSize = Marshal.SizeOf(t);
  252. if (objectSize > buffer.Length) return null;
  253. // Allocate some unmanaged memory.
  254. IntPtr buf = Marshal.AllocHGlobal(objectSize);
  255. // Copy the read byte array (byte[]) into the unmanaged memory block.
  256. Marshal.Copy(buffer, 0, buf, objectSize);
  257. // Push the memory into a new struct of type (T).
  258. object result = Marshal.PtrToStructure(buf, t);
  259. // Free the unmanaged memory block.
  260. Marshal.FreeHGlobal(buf);
  261. return result;
  262. }
  263. public static byte[] Struct2Bytes(object o)
  264. {
  265. // create a new byte buffer the size of your struct
  266. byte[] buffer = new byte[Marshal.SizeOf(o)];
  267. // pin the buffer so we can copy data into it w/o GC collecting it
  268. GCHandle bufferHandle = GCHandle.Alloc(buffer, GCHandleType.Pinned);
  269. // copy the struct data into the buffer
  270. Marshal.StructureToPtr(o, bufferHandle.AddrOfPinnedObject(), false);
  271. // free the GC handle
  272. bufferHandle.Free();
  273. return buffer;
  274. }
  275. public static byte[] Ushort2Byte(ushort[] data)
  276. {
  277. int sizeofT1 = Marshal.SizeOf(typeof(ushort));
  278. int sizeofT2 = Marshal.SizeOf(typeof(byte));
  279. byte[] by = new byte[sizeofT1 * data.Length / sizeofT2];
  280. for (int i = data.Length; i-- > 0;)
  281. {
  282. BitConverter.GetBytes(data[i]).CopyTo(by, i * 2);
  283. }
  284. return by;
  285. }
  286. public static byte[] Ushort2Byte(ushort[] data, int offset, int length)
  287. {
  288. int sizeofT1 = Marshal.SizeOf(typeof(ushort));
  289. int sizeofT2 = Marshal.SizeOf(typeof(byte));
  290. byte[] by = new byte[sizeofT1 * length / sizeofT2];
  291. for (int i = offset+length; i-- > offset;)
  292. {
  293. var value = BitConverter.GetBytes(data[i]);
  294. value.CopyTo(by, (i-offset) * 2);
  295. }
  296. return by;
  297. }
  298. public static ushort[] Byte2Ushort(byte[] data)
  299. {
  300. return Byte2Ushort(data, 0, data.Length);
  301. }
  302. public static ushort[] Byte2Ushort(byte[] data, int offset, int length)
  303. {
  304. System.Diagnostics.Debug.Assert(data.Length % 2 == 0);
  305. int sizeofT1 = Marshal.SizeOf(typeof(byte));
  306. int sizeofT2 = Marshal.SizeOf(typeof(ushort));
  307. ushort[] us = new ushort[sizeofT1 * length / sizeofT2];
  308. for (int i = 0; i < us.Length; i++)
  309. {
  310. us[i] = BitConverter.ToUInt16(data, offset + i * 2);
  311. }
  312. return us;
  313. }
  314. }
  315. }