MCProtocolPlc.cs 17 KB

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