MYMCProtocolPlc.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  1. using Aitex.Core.RT.DataCenter;
  2. using Aitex.Core.RT.Log;
  3. using MECF.Framework.Common.Communications;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Runtime.InteropServices;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using System.Xml;
  11. namespace MECF.Framework.RT.Core.IoProviders
  12. {
  13. public class MYMCProtocolPlc : IoProvider, IConnection
  14. {
  15. private string _ip = "127.0.0.1";
  16. private string _localIp = "127.0.0.1";
  17. private int _port = 6731;
  18. private int _socketId = 101;
  19. private int _stationId = 102;
  20. private byte[] _bufferIn;
  21. private byte[] _bufferOut;
  22. private int _bufferSize = 2048;
  23. private MCProtocol.MC_COMMAND_HEADER _header;
  24. //private MCProtocol.MC_RESPONSE_HEADER _response;
  25. private MCProtocol.MC_BATCH_COMMAND _batchCommand;
  26. private MCSocket _socket;
  27. private int perIoLength = 960;
  28. public string Address
  29. {
  30. get { return $"{_ip}:{_port}"; }
  31. }
  32. public bool IsConnected
  33. {
  34. get { return IsOpened; }
  35. }
  36. public bool Connect()
  37. {
  38. return true;
  39. }
  40. public bool Disconnect()
  41. {
  42. return true;
  43. }
  44. protected override void Close()
  45. {
  46. _socket.Close();
  47. SetState(IoProviderStateEnum.Closed);
  48. }
  49. protected override void Open()
  50. {
  51. _socket = new MCSocket();
  52. _header = new MCProtocol.MC_COMMAND_HEADER()
  53. {
  54. ProtocolID = MCProtocol.MC_SUBHEADER_COMMAND_MESSAGE,
  55. NetworkID = (byte)_socketId,
  56. StationID = (byte)_stationId,
  57. RequestIONumber = MCProtocol.MC_REQUEST_MODULE_IO_NUMBER,
  58. RequestStationNumber = MCProtocol.MC_REQUEST_MODULE_STATION_NUMBER,
  59. RequestDataLen = 0,
  60. CPUMonitorTimer = (ushort)(MCProtocol.MC_CPU_MONITOR_TIMER * 2),
  61. };
  62. _batchCommand = new MCProtocol.MC_BATCH_COMMAND()
  63. {
  64. Command = MCProtocol.MC_COMMAND_BATCH_READ,
  65. DeviceCode = MCProtocol.MC_DEVICE_CODE_DATA_REGISTER_WORD,
  66. DevicePoints = 0,
  67. HeadAddr = 0,
  68. Reserved = 0,
  69. SubCommand = MCProtocol.MC_SUBCOMMAND_WORD_UNITS,
  70. };
  71. _bufferOut = new byte[_bufferSize];
  72. _bufferIn = new byte[_bufferSize];
  73. _socket.Open(_ip, _port, _localIp);
  74. SetState(IoProviderStateEnum.Opened);
  75. }
  76. protected override short[] ReadAi(int offset, int size)
  77. {
  78. short[] buff = new short[size];
  79. int count = size / perIoLength;
  80. if (count < 1)
  81. {
  82. short[] aibuffer = DoReadAi(offset, size);
  83. if (aibuffer != null)
  84. Array.Copy(aibuffer, 0, buff, 0, aibuffer.Length);
  85. }
  86. else
  87. {
  88. short[] aibuffer;
  89. for (int i = 0; i < count; i++)
  90. {
  91. aibuffer = DoReadAi(i * perIoLength + offset, perIoLength);
  92. if (aibuffer != null)
  93. Array.Copy(aibuffer, 0, buff, i * perIoLength, aibuffer.Length);
  94. }
  95. if (size % perIoLength != 0)
  96. {
  97. aibuffer = DoReadAi(offset + perIoLength * count, size % perIoLength);
  98. if (aibuffer != null)
  99. Array.Copy(aibuffer, 0, buff, size - size % perIoLength, aibuffer.Length);
  100. }
  101. }
  102. return buff;
  103. }
  104. protected short[] DoReadAi(int offset, int size)
  105. {
  106. _batchCommand.Command = MCProtocol.MC_COMMAND_BATCH_READ;
  107. _batchCommand.SubCommand = MCProtocol.MC_SUBCOMMAND_WORD_UNITS;
  108. _batchCommand.HeadAddr = (ushort)(offset & 0xFFFF);
  109. _batchCommand.Reserved = (byte)(offset >> 16);
  110. _batchCommand.DevicePoints = (ushort)size;
  111. _header.RequestDataLen = (ushort)(MCProtocol.MC_BATCH_COMMAND_SIZE + MCProtocol.JUNK_SIZE);
  112. byte[] buffer = MCProtocol.Struct2Bytes(_header);
  113. byte[] command = MCProtocol.Struct2Bytes(_batchCommand);
  114. Array.Copy(buffer, 0, _bufferOut, 0, buffer.Length);
  115. Array.Copy(command, 0, _bufferOut, MCProtocol.MC_QHEADER_COMMAND_SIZE, command.Length);
  116. if (!WriteData(_bufferOut, MCProtocol.MC_QHEADER_COMMAND_SIZE + MCProtocol.MC_BATCH_COMMAND_SIZE))
  117. {
  118. return null;
  119. }
  120. if (!ReceiveData(_bufferIn, MCProtocol.MC_QHEADER_RESPONSE_SIZE))
  121. {
  122. return null;
  123. }
  124. MCProtocol.MC_RESPONSE_HEADER responseHeader =
  125. MCProtocol.ToStruct<MCProtocol.MC_RESPONSE_HEADER>(_bufferIn);
  126. if (responseHeader.CompleteCode != MCProtocol.MC_COMPLETE_CODE_SUCCESS)
  127. {
  128. return null;
  129. }
  130. int dataLength = responseHeader.ResponseDataLen - MCProtocol.JUNK_SIZE;
  131. if (!ReceiveData(_bufferIn, dataLength))
  132. {
  133. return null;
  134. }
  135. int sizeofT1 = Marshal.SizeOf(typeof(byte));
  136. int sizeofT2 = Marshal.SizeOf(typeof(ushort));
  137. short[] result = new short[sizeofT1 * 2 / sizeofT2];
  138. for (int i = 0; i < result.Length; i++)
  139. {
  140. result[i] = (short)BitConverter.ToUInt16(_bufferIn, i * 2);
  141. }
  142. return Array.ConvertAll(MCProtocol.Byte2Ushort(_bufferIn, 0, dataLength), x => (short)x);
  143. }
  144. protected override bool[] ReadDi(int offset, int size)
  145. {
  146. bool[] buff = new bool[size];
  147. int count = size / perIoLength;
  148. if (count < 1)
  149. {
  150. bool[] dibuffer = DoReadDi(offset, size);
  151. if (dibuffer != null)
  152. Array.Copy(dibuffer, 0, buff, 0, dibuffer.Length);
  153. }
  154. else
  155. {
  156. bool[] dibuffer;
  157. for (int i = 0; i < count; i++)
  158. {
  159. dibuffer = DoReadDi(i * perIoLength + offset, perIoLength);
  160. if (dibuffer != null)
  161. Array.Copy(dibuffer, 0, buff, i * perIoLength, dibuffer.Length);
  162. }
  163. if (size % perIoLength != 0)
  164. {
  165. dibuffer = DoReadDi(offset + perIoLength * count, size % perIoLength);
  166. if (dibuffer != null)
  167. Array.Copy(dibuffer, 0, buff, size - size % perIoLength, dibuffer.Length);
  168. }
  169. }
  170. return buff;
  171. }
  172. protected bool[] DoReadDi(int offset, int size)
  173. {
  174. _batchCommand.Command = MCProtocol.MC_COMMAND_BATCH_READ;
  175. _batchCommand.SubCommand = MCProtocol.MC_SUBCOMMAND_BIT_UNITS;
  176. _batchCommand.HeadAddr = (ushort)(offset & 0xFFFF);
  177. _batchCommand.Reserved = (byte)(offset >> 16);
  178. _batchCommand.DevicePoints = (ushort)size;
  179. _header.RequestDataLen = (ushort)(MCProtocol.MC_BATCH_COMMAND_SIZE + MCProtocol.JUNK_SIZE);
  180. byte[] buffer = MCProtocol.Struct2Bytes(_header);
  181. byte[] command = MCProtocol.Struct2Bytes(_batchCommand);
  182. Array.Copy(buffer, 0, _bufferOut, 0, buffer.Length);
  183. Array.Copy(command, 0, _bufferOut, MCProtocol.MC_QHEADER_COMMAND_SIZE, command.Length);
  184. if (!WriteData(_bufferOut, MCProtocol.MC_QHEADER_COMMAND_SIZE + MCProtocol.MC_BATCH_COMMAND_SIZE))
  185. {
  186. return null;
  187. }
  188. if (!ReceiveData(_bufferIn, MCProtocol.MC_QHEADER_RESPONSE_SIZE))
  189. {
  190. return null;
  191. }
  192. MCProtocol.MC_RESPONSE_HEADER responseHeader =
  193. MCProtocol.ToStruct<MCProtocol.MC_RESPONSE_HEADER>(_bufferIn);
  194. if (responseHeader.CompleteCode != MCProtocol.MC_COMPLETE_CODE_SUCCESS)
  195. {
  196. return null;
  197. }
  198. int dataLength = responseHeader.ResponseDataLen - MCProtocol.JUNK_SIZE;
  199. if (!ReceiveData(_bufferIn, dataLength))
  200. {
  201. return null;
  202. }
  203. byte[] result = new byte[dataLength * 2];
  204. for (int i = 0; i < dataLength; i++)
  205. {
  206. if ((_bufferIn[i] & 0x10) == 0x10)
  207. {
  208. result[i * 2 + 0] = 0x01;
  209. }
  210. if ((_bufferIn[i] & 0x01) == 0x01)
  211. {
  212. result[i * 2 + 1] = 0x01;
  213. }
  214. }
  215. return result.Select(x => x == 0x01).Take(size).ToArray();
  216. }
  217. protected override void SetParameter(XmlElement nodeParameter)
  218. {
  219. string strIp = nodeParameter.GetAttribute("ip");
  220. string strPort = nodeParameter.GetAttribute("port");
  221. string networkId = nodeParameter.GetAttribute("network_id");
  222. string stationId = nodeParameter.GetAttribute("station_id");
  223. if (!string.IsNullOrEmpty(nodeParameter.GetAttribute("buffer_size")))
  224. {
  225. int.TryParse(nodeParameter.GetAttribute("buffer_size"), out _bufferSize);
  226. }
  227. _port = int.Parse(strPort);
  228. _ip = strIp;
  229. _socketId = int.Parse(networkId);
  230. _stationId = int.Parse(stationId);
  231. ConnectionManager.Instance.Subscribe(Name, this);
  232. DATA.Subscribe($"{Module}.{Name}.IsConnected", () => _socket == null ? false : _socket.Connected);
  233. }
  234. protected override void WriteAo(int offset, short[] data)
  235. {
  236. short[] databuffer = new short[perIoLength];
  237. int count = data.Length / perIoLength;
  238. if (count < 1)
  239. {
  240. Array.Copy(data, 0, databuffer, 0, data.Length);
  241. Array.Resize(ref databuffer, data.Length);
  242. DoWriteAo(offset, databuffer.Select(x => (ushort)x).ToArray());
  243. }
  244. else
  245. {
  246. for (int i = 0; i < count; i++)
  247. {
  248. Array.Copy(data, i * perIoLength, databuffer, 0, databuffer.Length);
  249. DoWriteAo(offset + perIoLength * i, databuffer.Select(x => (ushort)x).ToArray());
  250. }
  251. if (data.Length % perIoLength != 0)
  252. {
  253. Array.Copy(data, perIoLength * count, databuffer, 0, data.Length % perIoLength);
  254. Array.Resize(ref databuffer, data.Length % perIoLength);
  255. DoWriteAo(offset + perIoLength * count, databuffer.Select(x => (ushort)x).ToArray());
  256. }
  257. }
  258. }
  259. protected void DoWriteAo(int offset, ushort[] data)
  260. {
  261. _batchCommand.Command = MCProtocol.MC_COMMAND_BATCH_WRITE;
  262. _batchCommand.SubCommand = MCProtocol.MC_SUBCOMMAND_WORD_UNITS;
  263. _batchCommand.HeadAddr = (ushort)(offset & 0xFFFF);
  264. _batchCommand.Reserved = (byte)(offset >> 16);
  265. _batchCommand.DevicePoints = (ushort)data.Length;
  266. _header.RequestDataLen = (ushort)(MCProtocol.MC_BATCH_COMMAND_SIZE + MCProtocol.JUNK_SIZE + MCProtocol.ShortSize * data.Length);
  267. byte[] header = MCProtocol.Struct2Bytes(_header);
  268. byte[] command = MCProtocol.Struct2Bytes(_batchCommand);
  269. byte[] byteData = MCProtocol.Ushort2Byte(data);
  270. Array.Copy(header, 0, _bufferOut, 0, header.Length);
  271. Array.Copy(command, 0, _bufferOut, MCProtocol.MC_QHEADER_COMMAND_SIZE, command.Length);
  272. Array.Copy(byteData, 0, _bufferOut, MCProtocol.MC_QHEADER_COMMAND_SIZE + MCProtocol.MC_BATCH_COMMAND_SIZE, byteData.Length);
  273. if (!WriteData(_bufferOut, MCProtocol.MC_QHEADER_COMMAND_SIZE + MCProtocol.MC_BATCH_COMMAND_SIZE + MCProtocol.ShortSize * data.Length))
  274. {
  275. return;
  276. }
  277. if (!ReceiveData(_bufferIn, MCProtocol.MC_QHEADER_RESPONSE_SIZE))
  278. {
  279. return;
  280. }
  281. MCProtocol.MC_RESPONSE_HEADER responseHeader =
  282. MCProtocol.ToStruct<MCProtocol.MC_RESPONSE_HEADER>(_bufferIn);
  283. if (responseHeader.CompleteCode != MCProtocol.MC_COMPLETE_CODE_SUCCESS)
  284. {
  285. LOG.Write("Write PLC failed with code," + responseHeader.CompleteCode);
  286. return;
  287. }
  288. int dataLength = responseHeader.ResponseDataLen - MCProtocol.JUNK_SIZE;
  289. if (dataLength > 0 && !ReceiveData(_bufferIn, dataLength))
  290. {
  291. return;
  292. }
  293. }
  294. protected override void WriteDo(int offset, bool[] data)
  295. {
  296. bool[] databuffer = new bool[perIoLength];
  297. int count = data.Length / perIoLength;
  298. if (count < 1)
  299. {
  300. Array.Copy(data, 0, databuffer, 0, data.Length);
  301. Array.Resize(ref databuffer, data.Length);
  302. DoWriteDo(offset, databuffer);
  303. }
  304. else
  305. {
  306. for (int i = 0; i < count; i++)
  307. {
  308. Array.Copy(data, i * perIoLength, databuffer, 0, databuffer.Length);
  309. DoWriteDo(offset + perIoLength * i, databuffer);
  310. }
  311. if (data.Length % perIoLength != 0)
  312. {
  313. Array.Copy(data, perIoLength * count, databuffer, 0, data.Length % perIoLength);
  314. Array.Resize(ref databuffer, data.Length % perIoLength);
  315. DoWriteDo(offset + perIoLength * count, databuffer);
  316. }
  317. }
  318. }
  319. protected void DoWriteDo(int offset, bool[] data)
  320. {
  321. // bool[] boolData = new bool[data.Length];
  322. // boolData = data.Select(x => x==(byte)1).ToArray();
  323. // WriteDo(offset, boolData);
  324. //}
  325. //protected void WriteDo(int offset, bool[] data)
  326. //{
  327. _batchCommand.Command = MCProtocol.MC_COMMAND_BATCH_WRITE;
  328. _batchCommand.SubCommand = MCProtocol.MC_SUBCOMMAND_BIT_UNITS;
  329. byte[] byteData = MCProtocol.TransBoolArrayToByteData(data);
  330. _batchCommand.HeadAddr = (ushort)(offset & 0xFFFF);
  331. _batchCommand.Reserved = (byte)(offset >> 16);
  332. _batchCommand.DevicePoints = (ushort)data.Length;
  333. _header.RequestDataLen = (ushort)(MCProtocol.MC_BATCH_COMMAND_SIZE + MCProtocol.JUNK_SIZE + byteData.Length);
  334. byte[] header = MCProtocol.Struct2Bytes(_header);
  335. byte[] command = MCProtocol.Struct2Bytes(_batchCommand);
  336. Array.Copy(header, 0, _bufferOut, 0, header.Length);
  337. Array.Copy(command, 0, _bufferOut, MCProtocol.MC_QHEADER_COMMAND_SIZE, command.Length);
  338. Array.Copy(byteData, 0, _bufferOut, MCProtocol.MC_QHEADER_COMMAND_SIZE + MCProtocol.MC_BATCH_COMMAND_SIZE, byteData.Length);
  339. if (!WriteData(_bufferOut, MCProtocol.MC_QHEADER_COMMAND_SIZE + MCProtocol.MC_BATCH_COMMAND_SIZE + byteData.Length))
  340. {
  341. return;
  342. }
  343. if (!ReceiveData(_bufferIn, MCProtocol.MC_QHEADER_RESPONSE_SIZE))
  344. {
  345. return;
  346. }
  347. MCProtocol.MC_RESPONSE_HEADER responseHeader =
  348. MCProtocol.ToStruct<MCProtocol.MC_RESPONSE_HEADER>(_bufferIn);
  349. if (responseHeader.CompleteCode != MCProtocol.MC_COMPLETE_CODE_SUCCESS)
  350. {
  351. LOG.Write("Write PLC failed with code," + responseHeader.CompleteCode);
  352. return;
  353. }
  354. int dataLength = responseHeader.ResponseDataLen - MCProtocol.JUNK_SIZE;
  355. if (dataLength > 0 && !ReceiveData(_bufferIn, dataLength))
  356. {
  357. return;
  358. }
  359. }
  360. private bool WriteData(byte[] data, int length)
  361. {
  362. return _socket.Write(data, length);
  363. }
  364. private bool ReceiveData(byte[] data, int length)
  365. {
  366. return _socket.Read(data, length);
  367. }
  368. }
  369. }