SuperMCProtocolPLC.cs 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  1. using Aitex.Core.RT.DataCenter;
  2. using Aitex.Core.RT.Event;
  3. using Aitex.Core.RT.Log;
  4. using Aitex.Core.RT.OperationCenter;
  5. using Aitex.Core.Util;
  6. using MECF.Framework.RT.Core.IoProviders;
  7. using MECF.Framework.RT.Core.IoProviders.Mitsubishis;
  8. using System;
  9. using System.Collections;
  10. using System.Collections.Generic;
  11. using System.Diagnostics;
  12. using System.Linq;
  13. using System.Threading;
  14. using System.Xml;
  15. namespace athosRT.Devices
  16. {
  17. public class SuperMCProtocolPLC : IoProvider
  18. {
  19. private string _ip = "127.0.0.1";
  20. private string _localIp = "127.0.0.1";
  21. private int _port = 6731;
  22. private int _socketId = 101;
  23. private int _stationId = 102;
  24. private byte[] _bufferIn;
  25. private byte[] _bufferOut;
  26. private MCProtocol.MC_COMMAND_HEADER _header;
  27. private MCProtocol.MC_BATCH_COMMAND _batchCommand;
  28. private MCSocket _socket;
  29. private int demandAiFrom;
  30. private int demandAiSize;
  31. private int demandAoFrom;
  32. private int demandAoSize;
  33. private bool inDemanding = false;
  34. private string aiStoragename = "R";
  35. private string aoStoragename = "R";
  36. private string diStoragename = "M";
  37. private string doStoragename = "M";
  38. private int perIoLength = 960;
  39. private int diStartOffset = 0;
  40. private int doStartOffset = 0;
  41. private int aiStartOffset = 0;
  42. private int aoStartOffset = 0;
  43. private Stopwatch stopwatchDi = new Stopwatch();
  44. private Stopwatch stopwatchDo = new Stopwatch();
  45. private Stopwatch stopwatchAi = new Stopwatch();
  46. private Stopwatch stopwatchAo = new Stopwatch();
  47. private int comunicationSpanDi;
  48. private int comunicationSpanDo;
  49. private int comunicationSpanAi;
  50. private int comunicationSpanAo;
  51. protected int comunicationSpanTotal;
  52. private Stopwatch stopwatchTotal = new Stopwatch();
  53. private List<IoBlockItem> _blockSectionsDemand;
  54. public override void Initialize(
  55. string module,
  56. string name,
  57. List<IoBlockItem> lstBuffers,
  58. IIoBuffer buffer,
  59. XmlElement nodeParameter,
  60. Dictionary<int, string> ioMappingPathFile)
  61. {
  62. this.Module = module;
  63. this.Name = name;
  64. this._source = module + "." + name;
  65. this._buffer = buffer;
  66. this._nodeParameter = nodeParameter;
  67. this._blockSections = lstBuffers;
  68. buffer.SetBufferBlock(this._source, lstBuffers);
  69. buffer.SetIoMap(this._source, ioMappingPathFile);
  70. this.SetParameter(nodeParameter);
  71. this.State = IoProviderStateEnum.Uninitialized;
  72. this._thread = new PeriodicJob(50, OnTimer, name);
  73. }
  74. protected override bool OnTimer()
  75. {
  76. if (this.State == IoProviderStateEnum.Uninitialized)
  77. {
  78. this.SetState(IoProviderStateEnum.Opening);
  79. this.Open();
  80. }
  81. if (this.State == IoProviderStateEnum.Opened)
  82. {
  83. try
  84. {
  85. this.stopwatchTotal.Start();
  86. foreach (IoBlockItem blockSection in this._blockSections)
  87. {
  88. if (blockSection.Type == IoType.DI)
  89. {
  90. this.stopwatchDi.Start();
  91. bool[] buffer = this.ReadDi(blockSection.Offset, (int) blockSection.Size);
  92. if (buffer != null)
  93. this._buffer.SetDiBuffer(this._source, blockSection.Offset, buffer);
  94. this.stopwatchDi.Stop();
  95. }
  96. else if (blockSection.Type == IoType.AI)
  97. {
  98. this.stopwatchAi.Start();
  99. if (this.inDemanding)
  100. {
  101. short[] buffer = this.ReadAi(blockSection.Offset, (int) blockSection.Size);
  102. if (buffer != null)
  103. this._buffer.SetAiBuffer(this._source, blockSection.Offset, buffer);
  104. }
  105. else if (this._blockSectionsDemand != null)
  106. {
  107. IoBlockItem ioBlockItem = this._blockSectionsDemand.Find((Predicate<IoBlockItem>) (x => x.Type == IoType.AI));
  108. if (ioBlockItem != null && this.demandAiSize > 0)
  109. {
  110. short[] buffer = this.ReadAi(ioBlockItem.Offset, (int) ioBlockItem.Size);
  111. if (buffer != null)
  112. this._buffer.SetAiBuffer(this._source, this.demandAiFrom, buffer, this.demandAiSize);
  113. }
  114. }
  115. else
  116. {
  117. short[] buffer = this.ReadAi(blockSection.Offset, (int) blockSection.Size);
  118. if (buffer != null)
  119. this._buffer.SetAiBuffer(this._source, blockSection.Offset, buffer);
  120. }
  121. this.stopwatchAi.Stop();
  122. }
  123. }
  124. this.comunicationSpanDi = (int) this.stopwatchDi.ElapsedMilliseconds;
  125. this.stopwatchDi.Reset();
  126. this.comunicationSpanAi = (int) this.stopwatchAi.ElapsedMilliseconds;
  127. this.stopwatchAi.Reset();
  128. this.stopwatchAo.Start();
  129. Dictionary<int, short[]> aoBuffer = this._buffer.GetAoBuffer(this._source);
  130. if (aoBuffer != null)
  131. {
  132. if (this.inDemanding)
  133. {
  134. if (this.demandAoSize > 0)
  135. {
  136. foreach (KeyValuePair<int, short[]> keyValuePair in aoBuffer)
  137. this.WriteAo(keyValuePair.Key, keyValuePair.Value);
  138. }
  139. }
  140. else
  141. {
  142. foreach (KeyValuePair<int, short[]> keyValuePair in aoBuffer)
  143. {
  144. if (keyValuePair.Key == this.demandAoFrom)
  145. {
  146. short[] numArray = new short[keyValuePair.Value.Length - this.demandAoSize];
  147. Array.Copy((Array) keyValuePair.Value, this.demandAoSize, (Array) numArray, 0, numArray.Length);
  148. this.WriteAo(this.demandAoFrom + this.demandAoSize, numArray);
  149. }
  150. else
  151. this.WriteAo(keyValuePair.Key, keyValuePair.Value);
  152. }
  153. }
  154. }
  155. this.stopwatchAo.Stop();
  156. this.comunicationSpanAo = (int) this.stopwatchAo.ElapsedMilliseconds;
  157. this.stopwatchAo.Reset();
  158. this.stopwatchDo.Start();
  159. Dictionary<int, bool[]> doBuffer = this._buffer.GetDoBuffer(this._source);
  160. if (doBuffer != null)
  161. {
  162. foreach (KeyValuePair<int, bool[]> keyValuePair in doBuffer)
  163. this.WriteDo(keyValuePair.Key, keyValuePair.Value);
  164. }
  165. this.stopwatchDo.Stop();
  166. this.comunicationSpanDo = (int) this.stopwatchDo.ElapsedMilliseconds;
  167. this.stopwatchDo.Reset();
  168. this.stopwatchTotal.Stop();
  169. this.comunicationSpanTotal = (int) this.stopwatchTotal.ElapsedMilliseconds;
  170. this.stopwatchTotal.Reset();
  171. }
  172. catch (Exception ex)
  173. {
  174. LOG.Write(string.Format("{0} {1}", (object) this.Name, (object) ex), file: "D:\\sorter\\trunk\\Efem\\Jet\\Jet_001_2P_Jet\\EfemRT\\Devices\\SuperMCProtocalPLC.cs", member: nameof (OnTimer), line: 285);
  175. Thread.Sleep(1000);//原本就有
  176. this.Close();
  177. this.Open();
  178. }
  179. }
  180. this._trigError.CLK = this.State == IoProviderStateEnum.Error;
  181. if (this._trigError.Q)
  182. EV.PostMessage<EventEnum>(this.Module, EventEnum.DefaultAlarm, (object) string.Format("{0} error", (object) this._source));
  183. return true;
  184. }
  185. protected override void SetParameter(XmlElement nodeParameter)
  186. {
  187. string attribute1 = nodeParameter.GetAttribute("ip");
  188. this._localIp = nodeParameter.GetAttribute("localIp");
  189. string attribute2 = nodeParameter.GetAttribute("port");
  190. string attribute3 = nodeParameter.GetAttribute("network_id");
  191. string attribute4 = nodeParameter.GetAttribute("station_id");
  192. this.aiStoragename = nodeParameter.GetAttribute("aiStoragename");
  193. this.aoStoragename = nodeParameter.GetAttribute("aoStoragename");
  194. this.diStoragename = nodeParameter.GetAttribute("diStoragename");
  195. this.doStoragename = nodeParameter.GetAttribute("doStoragename");
  196. int.TryParse(nodeParameter.GetAttribute("perIoLength"), out this.perIoLength);
  197. this.perIoLength = this.perIoLength == 0 ? 960 : this.perIoLength;
  198. int.TryParse(nodeParameter.GetAttribute("demandAiFrom"), out this.demandAiFrom);
  199. int.TryParse(nodeParameter.GetAttribute("demandAiSize"), out this.demandAiSize);
  200. int.TryParse(nodeParameter.GetAttribute("demandAoFrom"), out this.demandAoFrom);
  201. int.TryParse(nodeParameter.GetAttribute("demandAoSize"), out this.demandAoSize);
  202. if (nodeParameter.HasAttribute("diStartOffset"))
  203. int.TryParse(nodeParameter.GetAttribute("diStartOffset"), out this.diStartOffset);
  204. if (nodeParameter.HasAttribute("doStartOffset"))
  205. int.TryParse(nodeParameter.GetAttribute("doStartOffset"), out this.doStartOffset);
  206. this._port = int.Parse(attribute2);
  207. this._ip = attribute1;
  208. this._socketId = int.Parse(attribute3);
  209. this._stationId = int.Parse(attribute4);
  210. DATA.Subscribe(this.Name + ".CommunicationStatus", (Func<object>) (() => (this._socket == null ? 0 : (this._socket.Connected ? 1 : 0))), SubscriptionAttribute.FLAG.SaveDB);
  211. OP.Subscribe(this.Name + ".Reconnect", (Func<string, object[], bool>) ((cmd, args) =>
  212. {
  213. this.Close();
  214. this.Open();
  215. return true;
  216. }));
  217. }
  218. protected override void Open()
  219. {
  220. this._socket = new MCSocket();
  221. this._header = new MCProtocol.MC_COMMAND_HEADER()
  222. {
  223. ProtocolID = (ushort) 80,
  224. NetworkID = (byte) this._socketId,
  225. StationID = (byte) this._stationId,
  226. RequestIONumber = (ushort) 1023,
  227. RequestStationNumber = (byte) 0,
  228. RequestDataLen = (ushort) 0,
  229. CPUMonitorTimer = (ushort) ((uint) MCProtocol.MC_CPU_MONITOR_TIMER * 2U)
  230. };
  231. this._batchCommand = new MCProtocol.MC_BATCH_COMMAND()
  232. {
  233. Command = (ushort) 1025,
  234. DeviceCode = (byte) 168,
  235. DevicePoints = (ushort) 0,
  236. HeadAddr = (ushort) 0,
  237. Reserved = (byte) 0,
  238. SubCommand = (ushort) 0
  239. };
  240. this._bufferOut = new byte[2048];
  241. this._bufferIn = new byte[2048];
  242. this._socket.Open(this._ip, this._port, this._localIp);
  243. this.SetState(IoProviderStateEnum.Opened);
  244. }
  245. protected override void Close()
  246. {
  247. this._socket.Close();
  248. this.SetState(IoProviderStateEnum.Closed);
  249. }
  250. protected override bool[] ReadDi(int _offset, int size)
  251. {
  252. int offset = _offset + this.diStartOffset;
  253. bool[] destinationArray = new bool[size];
  254. int num = size / this.perIoLength;
  255. if (num < 1)
  256. {
  257. bool[] sourceArray = this.DoReadDi(offset, size);
  258. if (sourceArray != null)
  259. Array.Copy((Array) sourceArray, 0, (Array) destinationArray, 0, sourceArray.Length);
  260. }
  261. else
  262. {
  263. for (int index = 0; index < num; ++index)
  264. {
  265. bool[] sourceArray = this.DoReadDi(index * this.perIoLength + offset, this.perIoLength);
  266. if (sourceArray != null)
  267. Array.Copy((Array) sourceArray, 0, (Array) destinationArray, index * this.perIoLength, sourceArray.Length);
  268. }
  269. if (size % this.perIoLength != 0)
  270. {
  271. bool[] sourceArray = this.DoReadDi(offset + this.perIoLength * num, size % this.perIoLength);
  272. if (sourceArray != null)
  273. Array.Copy((Array) sourceArray, 0, (Array) destinationArray, size - size % this.perIoLength, sourceArray.Length);
  274. }
  275. }
  276. return destinationArray;
  277. }
  278. public bool SetDemandReadWrite()
  279. {
  280. this.inDemanding = true;
  281. Thread.Sleep(500);//原本就有
  282. this.inDemanding = false;
  283. return true;
  284. }
  285. protected override short[] ReadAi(int offset, int size)
  286. {
  287. short[] destinationArray = new short[size];
  288. int num = size / this.perIoLength;
  289. if (num < 1)
  290. {
  291. short[] sourceArray = this.DoReadAi(offset, size);
  292. if (sourceArray != null)
  293. Array.Copy((Array) sourceArray, 0, (Array) destinationArray, 0, sourceArray.Length);
  294. }
  295. else
  296. {
  297. for (int index = 0; index < num; ++index)
  298. {
  299. short[] sourceArray = this.DoReadAi(index * this.perIoLength + offset, this.perIoLength);
  300. if (sourceArray != null)
  301. Array.Copy((Array) sourceArray, 0, (Array) destinationArray, index * this.perIoLength, sourceArray.Length);
  302. }
  303. if (size % this.perIoLength != 0)
  304. {
  305. short[] sourceArray = this.DoReadAi(offset + this.perIoLength * num, size % this.perIoLength);
  306. if (sourceArray != null)
  307. Array.Copy((Array) sourceArray, 0, (Array) destinationArray, size - size % this.perIoLength, sourceArray.Length);
  308. }
  309. }
  310. return destinationArray;
  311. }
  312. protected override void WriteDo(int _offset, bool[] data)
  313. {
  314. int offset = _offset + this.doStartOffset;
  315. bool[] array = new bool[this.perIoLength];
  316. int num = data.Length / this.perIoLength;
  317. if (num < 1)
  318. {
  319. Array.Copy((Array) data, 0, (Array) array, 0, data.Length);
  320. Array.Resize<bool>(ref array, data.Length);
  321. this.DoWriteDo(offset, array);
  322. }
  323. else
  324. {
  325. for (int index = 0; index < num; ++index)
  326. {
  327. Array.Copy((Array) data, index * this.perIoLength, (Array) array, 0, array.Length);
  328. this.DoWriteDo(offset + this.perIoLength * index, array);
  329. }
  330. if (data.Length % this.perIoLength != 0)
  331. {
  332. Array.Copy((Array) data, this.perIoLength * num, (Array) array, 0, data.Length % this.perIoLength);
  333. Array.Resize<bool>(ref array, data.Length % this.perIoLength);
  334. this.DoWriteDo(offset + this.perIoLength * num, array);
  335. }
  336. }
  337. }
  338. protected override void WriteAo(int offset, short[] data)
  339. {
  340. short[] array = new short[this.perIoLength];
  341. int num = data.Length / this.perIoLength;
  342. if (num < 1)
  343. {
  344. Array.Copy((Array) data, 0, (Array) array, 0, data.Length);
  345. Array.Resize<short>(ref array, data.Length);
  346. this.DoWriteAo(offset, array);
  347. }
  348. else
  349. {
  350. for (int count = 0; count < num; ++count)
  351. {
  352. Array.Copy((Array) data, count * this.perIoLength, (Array) array, 0, array.Length);
  353. this.DoWriteAo(offset + this.perIoLength * count, array, data.Length, count);
  354. }
  355. if (data.Length % this.perIoLength != 0)
  356. {
  357. Array.Copy((Array) data, this.perIoLength * num, (Array) array, 0, data.Length % this.perIoLength);
  358. Array.Resize<short>(ref array, data.Length % this.perIoLength);
  359. this.DoWriteAo(offset + this.perIoLength * num, array, data.Length);
  360. }
  361. }
  362. }
  363. private bool WriteData(byte[] data, int length) => this._socket.Write(data, length);
  364. private int ReceiveData(byte[] data) => this._socket.Read(data);
  365. private short[] DoReadAi(int offset, int size)
  366. {
  367. this._batchCommand.DeviceCode = (byte) MelsecElement.ChooseMelsecElement(this.aiStoragename).m_nBinCode;
  368. this._batchCommand.Command = (ushort) 1025;
  369. this._batchCommand.SubCommand = (ushort) 0;
  370. this._batchCommand.HeadAddr = (ushort) (offset & (int) ushort.MaxValue);
  371. this._batchCommand.Reserved = (byte) (offset >> 16);
  372. this._batchCommand.DevicePoints = (ushort) size;
  373. this._header.RequestDataLen = (ushort) (MCProtocol.MC_BATCH_COMMAND_SIZE + MCProtocol.JUNK_SIZE);
  374. byte[] sourceArray1 = MCProtocol.Struct2Bytes((object) this._header);
  375. byte[] sourceArray2 = MCProtocol.Struct2Bytes((object) this._batchCommand);
  376. Array.Copy((Array) sourceArray1, 0, (Array) this._bufferOut, 0, sourceArray1.Length);
  377. Array.Copy((Array) sourceArray2, 0, (Array) this._bufferOut, MCProtocol.MC_QHEADER_COMMAND_SIZE, sourceArray2.Length);
  378. if (!this.WriteData(this._bufferOut, MCProtocol.MC_QHEADER_COMMAND_SIZE + MCProtocol.MC_BATCH_COMMAND_SIZE))
  379. return (short[]) null;
  380. int length = this.ReceiveData(this._bufferIn) - MCProtocol.MC_QHEADER_RESPONSE_SIZE;
  381. if (length < 0)
  382. return (short[]) null;
  383. MCProtocol.MC_RESPONSE_HEADER mcResponseHeader = MCProtocol.ToStruct<MCProtocol.MC_RESPONSE_HEADER>(this._bufferIn);
  384. if (mcResponseHeader.ProtocolId != (ushort) 208 && mcResponseHeader.CompleteCode > (ushort) 0)
  385. return (short[]) null;
  386. short[] numArray = new short[size];
  387. byte[] destinationArray = new byte[length];
  388. Array.Copy((Array) this._bufferIn, MCProtocol.MC_QHEADER_RESPONSE_SIZE, (Array) destinationArray, 0, destinationArray.Length);
  389. int index1 = 0;
  390. for (int index2 = 0; index1 < numArray.Length & index2 < destinationArray.Length; index2 += 2)
  391. {
  392. numArray[index1] = (short) BitConverter.ToUInt16(destinationArray, index1 * 2);
  393. ++index1;
  394. }
  395. return numArray;
  396. }
  397. private void DoWriteAo(int offset, short[] data, int total = 0, int count = 100)
  398. {
  399. ushort[] numArray = new ushort[data.Length];
  400. ushort[] array = ((IEnumerable<short>) data).Select<short, ushort>((Func<short, ushort>) (x => (ushort) x)).ToArray<ushort>();
  401. this._batchCommand.DeviceCode = (byte) MelsecElement.ChooseMelsecElement(this.aoStoragename).m_nBinCode;
  402. this._batchCommand.Command = (ushort) 5121;
  403. this._batchCommand.SubCommand = (ushort) 0;
  404. this._batchCommand.HeadAddr = (ushort) (offset & (int) ushort.MaxValue);
  405. this._batchCommand.Reserved = (byte) (offset >> 16);
  406. this._batchCommand.DevicePoints = (ushort) data.Length;
  407. this._header.RequestDataLen = (ushort) (MCProtocol.MC_BATCH_COMMAND_SIZE + MCProtocol.JUNK_SIZE + MCProtocol.ShortSize * data.Length);
  408. byte[] sourceArray1 = MCProtocol.Struct2Bytes((object) this._header);
  409. byte[] sourceArray2 = MCProtocol.Struct2Bytes((object) this._batchCommand);
  410. byte[] sourceArray3 = MCProtocol.Ushort2Byte(array);
  411. Array.Copy((Array) sourceArray1, 0, (Array) this._bufferOut, 0, sourceArray1.Length);
  412. Array.Copy((Array) sourceArray2, 0, (Array) this._bufferOut, MCProtocol.MC_QHEADER_COMMAND_SIZE, sourceArray2.Length);
  413. Array.Copy((Array) sourceArray3, 0, (Array) this._bufferOut, MCProtocol.MC_QHEADER_COMMAND_SIZE + MCProtocol.MC_BATCH_COMMAND_SIZE, sourceArray3.Length);
  414. if (!this.WriteData(this._bufferOut, MCProtocol.MC_QHEADER_COMMAND_SIZE + MCProtocol.MC_BATCH_COMMAND_SIZE + MCProtocol.ShortSize * data.Length) || this.ReceiveData(this._bufferIn) <= 0)
  415. return;
  416. MCProtocol.MC_RESPONSE_HEADER mcResponseHeader = MCProtocol.ToStruct<MCProtocol.MC_RESPONSE_HEADER>(this._bufferIn);
  417. if (mcResponseHeader.ProtocolId == (ushort) 208 || mcResponseHeader.CompleteCode <= (ushort) 0)
  418. return;
  419. LOG.Write(" failed with code," + mcResponseHeader.CompleteCode.ToString(), file: "D:\\sorter\\trunk\\Efem\\Jet\\Jet_001_2P_Jet\\EfemRT\\Devices\\SuperMCProtocalPLC.cs", member: nameof (DoWriteAo), line: 619);
  420. }
  421. private bool[] DoReadDi(int offset, int size)
  422. {
  423. this._batchCommand.DeviceCode = (byte) MelsecElement.ChooseMelsecElement(this.diStoragename).m_nBinCode;
  424. this._batchCommand.Command = (ushort) 1025;
  425. this._batchCommand.SubCommand = (ushort) 0;
  426. this._batchCommand.HeadAddr = (ushort) (offset & (int) ushort.MaxValue);
  427. this._batchCommand.Reserved = (byte) (offset >> 16);
  428. this._batchCommand.DevicePoints = size % 16 > 0 ? (ushort) (size / 16 + 1) : (ushort) (size / 16);
  429. this._header.RequestDataLen = (ushort) (MCProtocol.MC_BATCH_COMMAND_SIZE + MCProtocol.JUNK_SIZE);
  430. byte[] sourceArray1 = MCProtocol.Struct2Bytes((object) this._header);
  431. byte[] sourceArray2 = MCProtocol.Struct2Bytes((object) this._batchCommand);
  432. Array.Copy((Array) sourceArray1, 0, (Array) this._bufferOut, 0, sourceArray1.Length);
  433. Array.Copy((Array) sourceArray2, 0, (Array) this._bufferOut, MCProtocol.MC_QHEADER_COMMAND_SIZE, sourceArray2.Length);
  434. if (!this.WriteData(this._bufferOut, MCProtocol.MC_QHEADER_COMMAND_SIZE + MCProtocol.MC_BATCH_COMMAND_SIZE))
  435. return (bool[]) null;
  436. int length = this.ReceiveData(this._bufferIn) - MCProtocol.MC_QHEADER_RESPONSE_SIZE;
  437. if (length < 0)
  438. return (bool[]) null;
  439. MCProtocol.MC_RESPONSE_HEADER mcResponseHeader = MCProtocol.ToStruct<MCProtocol.MC_RESPONSE_HEADER>(this._bufferIn);
  440. if (mcResponseHeader.ProtocolId != (ushort) 208 && mcResponseHeader.CompleteCode > (ushort) 0)
  441. return (bool[]) null;
  442. bool[] source = new bool[length * 8];
  443. byte[] numArray = new byte[length];
  444. Array.Copy((Array) this._bufferIn, MCProtocol.MC_QHEADER_RESPONSE_SIZE, (Array) numArray, 0, numArray.Length);
  445. BitArray bitArray = new BitArray(numArray);
  446. for (int index = 0; index < source.Length; ++index)
  447. source[index] = bitArray.Get(index);
  448. return ((IEnumerable<bool>) source).Select<bool, bool>((Func<bool, bool>) (x => x)).Take<bool>(size).ToArray<bool>();
  449. }
  450. private void DoWriteDo(int offset, bool[] data)
  451. {
  452. this._batchCommand.DeviceCode = (byte) MelsecElement.ChooseMelsecElement(this.doStoragename).m_nBinCode;
  453. this._batchCommand.Command = (ushort) 5121;
  454. this._batchCommand.SubCommand = (ushort) 0;
  455. this._batchCommand.HeadAddr = (ushort) (offset & (int) ushort.MaxValue);
  456. this._batchCommand.Reserved = (byte) (offset >> 16);
  457. this._batchCommand.DevicePoints = data.Length % 16 > 0 ? (ushort) (data.Length / 16 + 1) : (ushort) (data.Length / 16);
  458. this._header.RequestDataLen = (ushort) (MCProtocol.MC_BATCH_COMMAND_SIZE + MCProtocol.JUNK_SIZE + (int) this._batchCommand.DevicePoints * 2);
  459. byte[] sourceArray1 = new byte[(int) this._batchCommand.DevicePoints * 2];
  460. int index1 = 0;
  461. for (int index2 = 0; index1 < data.Length & index2 < sourceArray1.Length; ++index2)
  462. {
  463. sourceArray1[index2] += data[index1] ? (byte) 1 : (byte) 0;
  464. sourceArray1[index2] += index1 + 1 >= data.Length || !data[index1 + 1] ? (byte) 0 : (byte) 2;
  465. sourceArray1[index2] += index1 + 2 >= data.Length || !data[index1 + 2] ? (byte) 0 : (byte) 4;
  466. sourceArray1[index2] += index1 + 3 >= data.Length || !data[index1 + 3] ? (byte) 0 : (byte) 8;
  467. sourceArray1[index2] += index1 + 4 >= data.Length || !data[index1 + 4] ? (byte) 0 : (byte) 16;
  468. sourceArray1[index2] += index1 + 5 >= data.Length || !data[index1 + 5] ? (byte) 0 : (byte) 32;
  469. sourceArray1[index2] += index1 + 6 >= data.Length || !data[index1 + 6] ? (byte) 0 : (byte) 64;
  470. sourceArray1[index2] += index1 + 7 >= data.Length || !data[index1 + 7] ? (byte) 0 : (byte) 128;
  471. index1 += 8;
  472. }
  473. byte[] sourceArray2 = MCProtocol.Struct2Bytes((object) this._header);
  474. byte[] sourceArray3 = MCProtocol.Struct2Bytes((object) this._batchCommand);
  475. Array.Copy((Array) sourceArray2, 0, (Array) this._bufferOut, 0, sourceArray2.Length);
  476. Array.Copy((Array) sourceArray3, 0, (Array) this._bufferOut, MCProtocol.MC_QHEADER_COMMAND_SIZE, sourceArray3.Length);
  477. Array.Copy((Array) sourceArray1, 0, (Array) this._bufferOut, MCProtocol.MC_QHEADER_COMMAND_SIZE + MCProtocol.MC_BATCH_COMMAND_SIZE, sourceArray1.Length);
  478. if (!this.WriteData(this._bufferOut, MCProtocol.MC_QHEADER_COMMAND_SIZE + MCProtocol.MC_BATCH_COMMAND_SIZE + sourceArray1.Length) || this.ReceiveData(this._bufferIn) <= 0)
  479. return;
  480. MCProtocol.MC_RESPONSE_HEADER mcResponseHeader = MCProtocol.ToStruct<MCProtocol.MC_RESPONSE_HEADER>(this._bufferIn);
  481. if (mcResponseHeader.CompleteCode <= (ushort) 0)
  482. return;
  483. LOG.Write("Write PLC failed with code," + mcResponseHeader.CompleteCode.ToString(), file: "D:\\sorter\\trunk\\Efem\\Jet\\Jet_001_2P_Jet\\EfemRT\\Devices\\SuperMCProtocalPLC.cs", member: nameof (DoWriteDo), line: 728);
  484. }
  485. }
  486. }