MCProtocolPlc.cs 51 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370
  1. using Aitex.Core.RT.DataCenter;
  2. using Aitex.Core.RT.Event;
  3. using Aitex.Core.RT.IOCore;
  4. using Aitex.Core.RT.Log;
  5. using Aitex.Core.RT.OperationCenter;
  6. using Aitex.Core.RT.SCCore;
  7. using Aitex.Core.Util;
  8. using MECF.Framework.Common.Communications;
  9. using MECF.Framework.RT.Core.IoProviders;
  10. using MECF.Framework.RT.Core.IoProviders.Mitsubishis;
  11. using System;
  12. using System.Collections;
  13. using System.Collections.Generic;
  14. using System.Diagnostics;
  15. using System.Linq;
  16. using System.Net;
  17. using System.Net.Sockets;
  18. using System.Text;
  19. using System.Threading;
  20. using System.Threading.Tasks;
  21. using System.Xml;
  22. namespace FurnaceRT.Equipments.Systems
  23. {
  24. public class MCProtocolPlc : IoProvider, IConnection
  25. {
  26. public string Address
  27. {
  28. get { return $"{_ip}:{_port}"; }
  29. }
  30. public bool IsConnected
  31. {
  32. get { return IsOpened; }
  33. }
  34. public bool Connect()
  35. {
  36. return true;
  37. }
  38. public bool Disconnect()
  39. {
  40. return true;
  41. }
  42. private bool _isPlcConnected;
  43. public bool IsPlcConnected
  44. {
  45. get => _isPlcConnected;
  46. set
  47. {
  48. _isPlcConnected = value;
  49. }
  50. }
  51. private string _ip = "127.0.0.1";
  52. private string _localIp = "127.0.0.1";
  53. private int _port = 6731;
  54. private int _socketId = 101;
  55. private int _stationId = 102;
  56. private byte[] _bufferIn;
  57. private byte[] _bufferOut;
  58. private MCProtocol.MC_COMMAND_HEADER _header;
  59. //private MCProtocol.MC_RESPONSE_HEADER _response;
  60. private MCProtocol.MC_BATCH_COMMAND _batchCommand;
  61. private MCSocket _socket;
  62. //private int demandAiFrom; //should be the same the offset in lstBuffers
  63. //private int demandAiSize;
  64. //private int demandAoFrom; //should be the same the offset in lstBuffers
  65. //private int demandAoSize;
  66. //private bool inDemanding = false;
  67. private string _aiStoragename = "R";
  68. private string _aoStoragename = "R";
  69. private string _diStoragename = "B";
  70. private string _doStoragename = "B";
  71. private int perIoLength = 960;
  72. private bool _readDiByAi = false;
  73. private bool _writeDoByAo = false;
  74. private Stopwatch stopwatchDi = new Stopwatch();
  75. private Stopwatch stopwatchDo = new Stopwatch();
  76. private Stopwatch stopwatchAi = new Stopwatch();
  77. private Stopwatch stopwatchAo = new Stopwatch();
  78. private int comunicationSpanDi;
  79. private int comunicationSpanDo;
  80. private int comunicationSpanAi;
  81. private int comunicationSpanAo;
  82. protected int comunicationSpanTotal;
  83. private Stopwatch stopwatchTotal = new Stopwatch();
  84. private R_TRIG _trigReadDoAndAo = new R_TRIG();
  85. private int diStartAddress;
  86. private int doStartAddress;
  87. private int aiStartAddress;
  88. private int aoStartAddress;
  89. //private List<IoBlockItem> _blockSectionsDemand;
  90. public override void Initialize(string module, string name, List<IoBlockItem> lstBuffers, IIoBuffer buffer, XmlElement nodeParameter, Dictionary<int, string> ioMappingPathFile)
  91. {
  92. Module = module;
  93. Name = name;
  94. _source = module + "." + name;
  95. _buffer = buffer;
  96. _nodeParameter = nodeParameter;
  97. SetParameter(nodeParameter);
  98. _blockSections = lstBuffers;
  99. buffer.SetBufferBlock(_source, lstBuffers);
  100. buffer.SetIoMap(_source, ioMappingPathFile);
  101. int.TryParse(IO.GetDoList(_source)[0].Addr.Substring(1), out doStartAddress);
  102. int.TryParse(IO.GetDiList(_source)[0].Addr.Substring(1), out diStartAddress);
  103. int.TryParse(IO.GetAoList(_source)[0].Addr.Substring(1), out aoStartAddress);
  104. int.TryParse(IO.GetAiList(_source)[0].Addr.Substring(1), out aiStartAddress);
  105. State = IoProviderStateEnum.Uninitialized;
  106. _thread = new PeriodicJob(30, OnTimer, name);
  107. ConnectionManager.Instance.Subscribe(Name, this);
  108. DATA.Subscribe($"{Module}.{Name}.IsConnected", () => _socket == null ? false : _socket.Connected);
  109. OP.Subscribe($"{Name}.Reconnect", (string cmd, object[] args) =>
  110. {
  111. Close();
  112. Open();
  113. return true;
  114. });
  115. }
  116. public override void Reset()
  117. {
  118. _trigError.RST = true;
  119. _trigReadDoAndAo.RST = true;
  120. _trigNotConnected.RST = true;
  121. //if(!_plc.CheckIsConnected())
  122. if (!IsPlcConnected)
  123. {
  124. Close();
  125. Open();
  126. }
  127. }
  128. protected override bool OnTimer()
  129. {
  130. if (State == IoProviderStateEnum.Uninitialized)
  131. {
  132. _trigReadDoAndAo.RST = true;
  133. SetState(IoProviderStateEnum.Opening);
  134. Open();
  135. }
  136. if (State == IoProviderStateEnum.Opened)
  137. {
  138. try
  139. {
  140. stopwatchTotal.Start();
  141. foreach (var bufferSection in _blockSections)
  142. {
  143. if (bufferSection.Type == IoType.DI)
  144. {
  145. stopwatchDi.Start();
  146. bool[] diBuffer = ReadDi(bufferSection.Offset, bufferSection.Size);
  147. if (diBuffer != null)
  148. {
  149. _buffer.SetDiBuffer(_source, bufferSection.Offset, diBuffer);
  150. //TraceArray(diBuffer);
  151. }
  152. stopwatchDi.Stop();
  153. }
  154. else if (bufferSection.Type == IoType.DO)
  155. {
  156. stopwatchDo.Start();
  157. if (!_trigReadDoAndAo.M)
  158. {
  159. bool[] doBuffer = ReadDo(bufferSection.Offset, bufferSection.Size);
  160. if (doBuffer != null)
  161. {
  162. _buffer.SetDoBuffer(_source, bufferSection.Offset, doBuffer);
  163. //TraceArray(diBuffer);
  164. }
  165. }
  166. stopwatchDo.Stop();
  167. }
  168. else if (bufferSection.Type == IoType.AI)
  169. {
  170. stopwatchAi.Start();
  171. short[] aiBuffer = ReadAi(bufferSection.Offset, bufferSection.Size);
  172. if (aiBuffer != null)
  173. {
  174. _buffer.SetAiBuffer(_source, bufferSection.Offset, aiBuffer);
  175. }
  176. stopwatchAi.Stop();
  177. }
  178. else if (bufferSection.Type == IoType.AO)
  179. {
  180. stopwatchAo.Start();
  181. if (!_trigReadDoAndAo.M)
  182. {
  183. short[] aoBuffer = ReadAo(bufferSection.Offset, bufferSection.Size);
  184. if (aoBuffer != null)
  185. {
  186. _buffer.SetAoBuffer(_source, bufferSection.Offset, aoBuffer);
  187. }
  188. }
  189. stopwatchAo.Stop();
  190. }
  191. }
  192. if (!_trigReadDoAndAo.M)
  193. _trigReadDoAndAo.CLK = true;
  194. comunicationSpanDi = (int)stopwatchDi.ElapsedMilliseconds;
  195. stopwatchDi.Reset();
  196. comunicationSpanDo = (int)stopwatchDo.ElapsedMilliseconds;
  197. stopwatchDo.Reset();
  198. comunicationSpanAi = (int)stopwatchAi.ElapsedMilliseconds;
  199. stopwatchAi.Reset();
  200. comunicationSpanAo = (int)stopwatchAo.ElapsedMilliseconds;
  201. stopwatchAo.Reset();
  202. stopwatchAo.Start();
  203. Dictionary<int, short[]> aos = _buffer.GetAoBuffer(_source);
  204. if (aos != null)
  205. {
  206. foreach (var ao in aos)
  207. {
  208. WriteAo(ao.Key, ao.Value);
  209. }
  210. }
  211. stopwatchAo.Stop();
  212. comunicationSpanAo = (int)stopwatchAo.ElapsedMilliseconds;
  213. stopwatchAo.Reset();
  214. stopwatchDo.Start();
  215. Dictionary<int, bool[]> dos = _buffer.GetDoBuffer(_source);
  216. if (dos != null)
  217. {
  218. foreach (var doo in dos)
  219. {
  220. WriteDo(doo.Key, doo.Value);
  221. }
  222. }
  223. stopwatchDo.Stop();
  224. comunicationSpanDo = (int)stopwatchDo.ElapsedMilliseconds;
  225. stopwatchDo.Reset();
  226. stopwatchTotal.Stop();
  227. comunicationSpanTotal = (int)stopwatchTotal.ElapsedMilliseconds;
  228. stopwatchTotal.Reset();
  229. }
  230. catch (Exception ex)
  231. {
  232. LOG.Write($"{Name} {ex}");
  233. //SetState(IoProviderStateEnum.Error);
  234. Thread.Sleep(1000);
  235. Close();
  236. Open();
  237. }
  238. }
  239. _trigError.CLK = State == IoProviderStateEnum.Error;
  240. if (_trigError.Q)
  241. {
  242. EV.PostAlarmLog(Module, $"{_source} PLC {_ip}:{_port} error");
  243. }
  244. _trigNotConnected.CLK = State != IoProviderStateEnum.Opened;
  245. if (_trigNotConnected.T)
  246. {
  247. EV.PostInfoLog(Module, $"{_source} connected");
  248. }
  249. if (_trigNotConnected.R)
  250. {
  251. EV.PostAlarmLog(Module, $"{_source} PLC {_ip}:{_port} not connected");
  252. }
  253. return true;
  254. }
  255. protected override void SetParameter(XmlElement nodeParameter)
  256. {
  257. string strIp = nodeParameter.GetAttribute("ip");
  258. //_localIp = nodeParameter.GetAttribute("localIp");
  259. string strPort = nodeParameter.GetAttribute("port");
  260. string networkId = nodeParameter.GetAttribute("network_id");
  261. string stationId = nodeParameter.GetAttribute("station_id");
  262. _aiStoragename = nodeParameter.GetAttribute("aiStoragename");
  263. _aoStoragename = nodeParameter.GetAttribute("aoStoragename");
  264. _diStoragename = nodeParameter.GetAttribute("diStoragename");
  265. _doStoragename = nodeParameter.GetAttribute("doStoragename");
  266. if (_diStoragename == "D")
  267. _readDiByAi = true;
  268. if (_doStoragename == "D")
  269. _writeDoByAo = true;
  270. int.TryParse(nodeParameter.GetAttribute("perIoLength"), out perIoLength);
  271. perIoLength = perIoLength == 0 ? 960 : perIoLength;
  272. int.TryParse(nodeParameter.GetAttribute("doStartAddress"), out doStartAddress);
  273. int.TryParse(nodeParameter.GetAttribute("diStartAddress"), out diStartAddress);
  274. int.TryParse(nodeParameter.GetAttribute("aoStartAddress"), out aoStartAddress);
  275. int.TryParse(nodeParameter.GetAttribute("aiStartAddress"), out aiStartAddress);
  276. _port = int.Parse(strPort);
  277. _ip = strIp;
  278. _socketId = int.Parse(networkId);
  279. _stationId = int.Parse(stationId);
  280. }
  281. protected override void Open()
  282. {
  283. _socket = new MCSocket();
  284. _header = new MCProtocol.MC_COMMAND_HEADER()
  285. {
  286. ProtocolID = MCProtocol.MC_SUBHEADER_COMMAND_MESSAGE,
  287. NetworkID = (byte)_socketId,
  288. //NetworkID = (byte)0,
  289. StationID = (byte)_stationId,
  290. //StationID = (byte)0xff,
  291. RequestIONumber = MCProtocol.MC_REQUEST_MODULE_IO_NUMBER,
  292. RequestStationNumber = MCProtocol.MC_REQUEST_MODULE_STATION_NUMBER,
  293. RequestDataLen = 0,
  294. CPUMonitorTimer = (ushort)(MCProtocol.MC_CPU_MONITOR_TIMER * 2),
  295. };
  296. _batchCommand = new MCProtocol.MC_BATCH_COMMAND()
  297. {
  298. Command = MCProtocol.MC_COMMAND_BATCH_READ,
  299. DeviceCode = MCProtocol.MC_DEVICE_CODE_DATA_REGISTER_WORD,
  300. DevicePoints = 0,
  301. HeadAddr = 0,
  302. Reserved = 0,
  303. SubCommand = MCProtocol.MC_SUBCOMMAND_WORD_UNITS,
  304. };
  305. _bufferOut = new byte[2048];
  306. _bufferIn = new byte[2048];
  307. if (_socket.Open(_ip, _port, _localIp))
  308. {
  309. SetState(IoProviderStateEnum.Opened);
  310. _isPlcConnected = true;
  311. }
  312. }
  313. protected override void Close()
  314. {
  315. _socket.Close();
  316. SetState(IoProviderStateEnum.Closed);
  317. }
  318. protected override bool[] ReadDi(int offset, int size)
  319. {
  320. if (!_isPlcConnected)
  321. {
  322. return null;
  323. }
  324. if (_readDiByAi)
  325. {
  326. if (_socket == null || !_socket.Connected)
  327. {
  328. State = IoProviderStateEnum.Error;
  329. return null;
  330. }
  331. bool[] buff = new bool[size];
  332. var reasSize = size % 16 > 0 ? size / 16 + 1 : size / 16;
  333. var readBuff = ReadAiGetByte(diStartAddress, reasSize);
  334. var bitArray = new BitArray(readBuff);
  335. for (int i = 0; i < buff.Length; i++)
  336. {
  337. buff[i] = bitArray.Get(i);
  338. }
  339. return buff;
  340. }
  341. else
  342. {
  343. bool[] buff = new bool[size];
  344. int count = size / perIoLength;
  345. if (count < 1)
  346. {
  347. bool[] dibuffer = DoReadDi(diStartAddress, size);
  348. if (dibuffer != null)
  349. Array.Copy(dibuffer, 0, buff, 0, dibuffer.Length);
  350. else
  351. return null;
  352. }
  353. else
  354. {
  355. bool[] dibuffer;
  356. for (int i = 0; i < count; i++)
  357. {
  358. dibuffer = DoReadDi(i * perIoLength + diStartAddress, perIoLength);
  359. if (dibuffer != null)
  360. Array.Copy(dibuffer, 0, buff, i * perIoLength, dibuffer.Length);
  361. else
  362. return null;
  363. }
  364. if (size % perIoLength != 0)
  365. {
  366. dibuffer = DoReadDi(diStartAddress + perIoLength * count, size % perIoLength);
  367. if (dibuffer != null)
  368. Array.Copy(dibuffer, 0, buff, size - size % perIoLength, dibuffer.Length);
  369. else
  370. return null;
  371. }
  372. }
  373. return buff;
  374. }
  375. }
  376. protected bool[] ReadDo(int offset, int size)
  377. {
  378. if (!_isPlcConnected)
  379. {
  380. return null;
  381. }
  382. if (_readDiByAi)
  383. {
  384. if (_socket == null || !_socket.Connected)
  385. {
  386. State = IoProviderStateEnum.Error;
  387. return null;
  388. }
  389. bool[] buff = new bool[size];
  390. var reasSize = size % 16 > 0 ? size / 16 + 1 : size / 16;
  391. var readBuff = ReadAoGetByte(doStartAddress, reasSize);
  392. var bitArray = new BitArray(readBuff);
  393. for (int i = 0; i < buff.Length; i++)
  394. {
  395. buff[i] = bitArray.Get(i);
  396. }
  397. return buff;
  398. }
  399. else
  400. {
  401. bool[] buff = new bool[size];
  402. int count = size / perIoLength;
  403. if (count < 1)
  404. {
  405. bool[] dibuffer = DoReadDo(doStartAddress, size);
  406. if (dibuffer != null)
  407. Array.Copy(dibuffer, 0, buff, 0, dibuffer.Length);
  408. else
  409. return null;
  410. }
  411. else
  412. {
  413. bool[] dibuffer;
  414. for (int i = 0; i < count; i++)
  415. {
  416. dibuffer = DoReadDo(i * perIoLength + doStartAddress, perIoLength);
  417. if (dibuffer != null)
  418. Array.Copy(dibuffer, 0, buff, i * perIoLength, dibuffer.Length);
  419. else
  420. return null;
  421. }
  422. if (size % perIoLength != 0)
  423. {
  424. dibuffer = DoReadDo(doStartAddress + perIoLength * count, size % perIoLength);
  425. if (dibuffer != null)
  426. Array.Copy(dibuffer, 0, buff, size - size % perIoLength, dibuffer.Length);
  427. else
  428. return null;
  429. }
  430. }
  431. return buff;
  432. }
  433. }
  434. protected override short[] ReadAi(int offset, int size)
  435. {
  436. if (!_isPlcConnected)
  437. {
  438. return null;
  439. }
  440. short[] buff = new short[size];
  441. int count = size / perIoLength;
  442. if (count < 1)
  443. {
  444. short[] aibuffer = DoReadAi(aiStartAddress, size);
  445. if (aibuffer != null)
  446. Array.Copy(aibuffer, 0, buff, 0, aibuffer.Length);
  447. }
  448. else
  449. {
  450. short[] aibuffer;
  451. for (int i = 0; i < count; i++)
  452. {
  453. aibuffer = DoReadAi(i * perIoLength + aiStartAddress, perIoLength);
  454. if (aibuffer != null)
  455. Array.Copy(aibuffer, 0, buff, i * perIoLength, aibuffer.Length);
  456. }
  457. if (size % perIoLength != 0)
  458. {
  459. aibuffer = DoReadAi(aiStartAddress + perIoLength * count, size % perIoLength);
  460. if (aibuffer != null)
  461. Array.Copy(aibuffer, 0, buff, size - size % perIoLength, aibuffer.Length);
  462. }
  463. }
  464. return buff;
  465. }
  466. protected short[] ReadAo(int offset, int size)
  467. {
  468. if (!_isPlcConnected)
  469. {
  470. return null;
  471. }
  472. short[] buff = new short[size];
  473. int count = size / perIoLength;
  474. if (count < 1)
  475. {
  476. short[] aibuffer = DoReadAo(aoStartAddress, size);
  477. if (aibuffer != null)
  478. Array.Copy(aibuffer, 0, buff, 0, aibuffer.Length);
  479. }
  480. else
  481. {
  482. short[] aibuffer;
  483. for (int i = 0; i < count; i++)
  484. {
  485. aibuffer = DoReadAo(i * perIoLength + aoStartAddress, perIoLength);
  486. if (aibuffer != null)
  487. Array.Copy(aibuffer, 0, buff, i * perIoLength, aibuffer.Length);
  488. }
  489. if (size % perIoLength != 0)
  490. {
  491. aibuffer = DoReadAo(aoStartAddress + perIoLength * count, size % perIoLength);
  492. if (aibuffer != null)
  493. Array.Copy(aibuffer, 0, buff, size - size % perIoLength, aibuffer.Length);
  494. }
  495. }
  496. return buff;
  497. }
  498. protected override void WriteDo(int offset, bool[] data)
  499. {
  500. if (!_isPlcConnected)
  501. {
  502. return;
  503. }
  504. if (_writeDoByAo)
  505. {
  506. byte[] byteData = new byte[data.Length % 8 > 0 ? data.Length / 8 + 1 : data.Length / 8];
  507. for (int i = 0, byteDataIndex = 0; i < data.Length & byteDataIndex < byteData.Length; i += 8, byteDataIndex++)
  508. {
  509. byteData[byteDataIndex] += (byte)(data[i] ? 1 : 0);
  510. byteData[byteDataIndex] += (byte)(i + 1 < data.Length && data[i + 1] ? 1 << 1 : 0);
  511. byteData[byteDataIndex] += (byte)(i + 2 < data.Length && data[i + 2] ? 1 << 2 : 0);
  512. byteData[byteDataIndex] += (byte)(i + 3 < data.Length && data[i + 3] ? 1 << 3 : 0);
  513. byteData[byteDataIndex] += (byte)(i + 4 < data.Length && data[i + 4] ? 1 << 4 : 0);
  514. byteData[byteDataIndex] += (byte)(i + 5 < data.Length && data[i + 5] ? 1 << 5 : 0);
  515. byteData[byteDataIndex] += (byte)(i + 6 < data.Length && data[i + 6] ? 1 << 6 : 0);
  516. byteData[byteDataIndex] += (byte)(i + 7 < data.Length && data[i + 7] ? 1 << 7 : 0);
  517. }
  518. WriteAoByte(doStartAddress, byteData);
  519. }
  520. else
  521. {
  522. bool[] databuffer = new bool[perIoLength];
  523. int count = data.Length / perIoLength;
  524. if (count < 1)
  525. {
  526. Array.Copy(data, 0, databuffer, 0, data.Length);
  527. Array.Resize(ref databuffer, data.Length);
  528. DoWriteDo(doStartAddress, databuffer);
  529. }
  530. else
  531. {
  532. for (int i = 0; i < count; i++)
  533. {
  534. Array.Copy(data, i * perIoLength, databuffer, 0, databuffer.Length);
  535. DoWriteDo(doStartAddress + perIoLength * i, databuffer);
  536. }
  537. if (data.Length % perIoLength != 0)
  538. {
  539. Array.Copy(data, perIoLength * count, databuffer, 0, data.Length % perIoLength);
  540. Array.Resize(ref databuffer, data.Length % perIoLength);
  541. DoWriteDo(doStartAddress + perIoLength * count, databuffer);
  542. }
  543. }
  544. }
  545. }
  546. protected override void WriteAo(int offset, short[] data)
  547. {
  548. if (!_isPlcConnected)
  549. {
  550. return;
  551. }
  552. short[] databuffer = new short[perIoLength];
  553. int count = data.Length / perIoLength;
  554. if (count < 1)
  555. {
  556. Array.Copy(data, 0, databuffer, 0, data.Length);
  557. Array.Resize(ref databuffer, data.Length);
  558. DoWriteAo(aoStartAddress, databuffer);
  559. }
  560. else
  561. {
  562. for (int i = 0; i < count; i++)
  563. {
  564. Array.Copy(data, i * perIoLength, databuffer, 0, databuffer.Length);
  565. DoWriteAo(aoStartAddress + perIoLength * i, databuffer, data.Length, i);
  566. }
  567. if (data.Length % perIoLength != 0)
  568. {
  569. Array.Copy(data, perIoLength * count, databuffer, 0, data.Length % perIoLength);
  570. Array.Resize(ref databuffer, data.Length % perIoLength);
  571. DoWriteAo(aoStartAddress + perIoLength * count, databuffer, data.Length);
  572. }
  573. }
  574. }
  575. private byte[] ReadAiGetByte(int startAddress, int size)
  576. {
  577. byte[] buff = new byte[size * 2];
  578. int count = size / perIoLength;
  579. if (count < 1)
  580. {
  581. byte[] aibuffer = DoReadAiGetByte(startAddress, size, _aiStoragename);
  582. if (aibuffer != null)
  583. Array.Copy(aibuffer, 0, buff, 0, aibuffer.Length);
  584. }
  585. else
  586. {
  587. byte[] aibuffer;
  588. for (int i = 0; i < count; i++)
  589. {
  590. aibuffer = DoReadAiGetByte(i * perIoLength + startAddress, perIoLength, _aiStoragename);
  591. if (aibuffer != null)
  592. Array.Copy(aibuffer, 0, buff, i * perIoLength, aibuffer.Length);
  593. }
  594. if (size % perIoLength != 0)
  595. {
  596. aibuffer = DoReadAiGetByte(startAddress + perIoLength * count, size % perIoLength, _aiStoragename);
  597. if (aibuffer != null)
  598. Array.Copy(aibuffer, 0, buff, size - size % perIoLength, aibuffer.Length);
  599. }
  600. }
  601. return buff;
  602. }
  603. private byte[] ReadAoGetByte(int startAddress, int size)
  604. {
  605. byte[] buff = new byte[size * 2];
  606. int count = size / perIoLength;
  607. if (count < 1)
  608. {
  609. byte[] aibuffer = DoReadAoGetByte(startAddress, size, _aiStoragename);
  610. if (aibuffer != null)
  611. Array.Copy(aibuffer, 0, buff, 0, aibuffer.Length);
  612. }
  613. else
  614. {
  615. byte[] aibuffer;
  616. for (int i = 0; i < count; i++)
  617. {
  618. aibuffer = DoReadAoGetByte(i * perIoLength + startAddress, perIoLength, _aiStoragename);
  619. if (aibuffer != null)
  620. Array.Copy(aibuffer, 0, buff, i * perIoLength, aibuffer.Length);
  621. }
  622. if (size % perIoLength != 0)
  623. {
  624. aibuffer = DoReadAoGetByte(startAddress + perIoLength * count, size % perIoLength, _aiStoragename);
  625. if (aibuffer != null)
  626. Array.Copy(aibuffer, 0, buff, size - size % perIoLength, aibuffer.Length);
  627. }
  628. }
  629. return buff;
  630. }
  631. private void WriteAoByte(int startAddress, byte[] data)
  632. {
  633. byte[] databuffer = new byte[perIoLength * 2];
  634. int count = (data.Length / 2) / perIoLength;
  635. if (count < 1)
  636. {
  637. DoWriteAoByte(startAddress, data, _aoStoragename);
  638. }
  639. else
  640. {
  641. for (int i = 0; i < count; i++)
  642. {
  643. Array.Copy(data, i * perIoLength * 2, databuffer, 0, databuffer.Length * 2);
  644. DoWriteAoByte(startAddress + perIoLength * i, databuffer, _aoStoragename, data.Length * 2, i);
  645. }
  646. if (data.Length / 2 % perIoLength != 0)
  647. {
  648. Array.Copy(data, perIoLength * 2 * count, databuffer, 0, data.Length / 2 % perIoLength);
  649. Array.Resize(ref databuffer, data.Length / 2 % perIoLength);
  650. DoWriteAoByte(startAddress + perIoLength * count, databuffer, _aoStoragename, data.Length);
  651. }
  652. }
  653. }
  654. private void DoWriteAoByte(int offset, byte[] data, string aoStoragename, int total = 0, int count = 100)
  655. {
  656. _batchCommand.DeviceCode = (byte)MelsecElement.ChooseMelsecElement(aoStoragename).m_nBinCode;
  657. _batchCommand.Command = MCProtocol.MC_COMMAND_BATCH_WRITE;
  658. _batchCommand.SubCommand = MCProtocol.MC_SUBCOMMAND_WORD_UNITS;
  659. _batchCommand.HeadAddr = (ushort)(offset & 0xFFFF);
  660. _batchCommand.Reserved = (byte)(offset >> 16);
  661. _batchCommand.DevicePoints = (ushort)(data.Length / 2);
  662. _header.RequestDataLen = (ushort)(MCProtocol.MC_BATCH_COMMAND_SIZE + MCProtocol.JUNK_SIZE + MCProtocol.ShortSize * data.Length / 2);
  663. byte[] header = MCProtocol.Struct2Bytes(_header);
  664. byte[] command = MCProtocol.Struct2Bytes(_batchCommand);
  665. Array.Copy(header, 0, _bufferOut, 0, header.Length);
  666. Array.Copy(command, 0, _bufferOut, MCProtocol.MC_QHEADER_COMMAND_SIZE, command.Length);
  667. Array.Copy(data, 0, _bufferOut, MCProtocol.MC_QHEADER_COMMAND_SIZE + MCProtocol.MC_BATCH_COMMAND_SIZE, data.Length);
  668. if (!WriteData(_bufferOut, MCProtocol.MC_QHEADER_COMMAND_SIZE + MCProtocol.MC_BATCH_COMMAND_SIZE + MCProtocol.ShortSize * data.Length / 2))
  669. {
  670. return;
  671. }
  672. if (ReceiveData(_bufferIn) <= 0)
  673. {
  674. return;
  675. }
  676. MCProtocol.MC_RESPONSE_HEADER responseHeader =
  677. MCProtocol.ToStruct<MCProtocol.MC_RESPONSE_HEADER>(_bufferIn);
  678. if (responseHeader.ProtocolId != 0x00d0 && responseHeader.CompleteCode != MCProtocol.MC_COMPLETE_CODE_SUCCESS)
  679. {
  680. LOG.Write(" failed with code," + responseHeader.CompleteCode);
  681. return;
  682. }
  683. }
  684. private byte[] DoReadAiGetByte(int offset, int size, string _aiStoragename)
  685. {
  686. _batchCommand.DeviceCode = (byte)MelsecElement.ChooseMelsecElement(_aiStoragename).m_nBinCode;
  687. _batchCommand.Command = MCProtocol.MC_COMMAND_BATCH_READ;
  688. _batchCommand.SubCommand = MCProtocol.MC_SUBCOMMAND_WORD_UNITS;
  689. _batchCommand.HeadAddr = (ushort)(offset & 0xFFFF);
  690. _batchCommand.Reserved = (byte)(offset >> 16);
  691. _batchCommand.DevicePoints = (ushort)size;
  692. _header.RequestDataLen = (ushort)(MCProtocol.MC_BATCH_COMMAND_SIZE + MCProtocol.JUNK_SIZE);
  693. byte[] buffer = MCProtocol.Struct2Bytes(_header);
  694. byte[] command = MCProtocol.Struct2Bytes(_batchCommand);
  695. Array.Copy(buffer, 0, _bufferOut, 0, buffer.Length);
  696. Array.Copy(command, 0, _bufferOut, MCProtocol.MC_QHEADER_COMMAND_SIZE, command.Length);
  697. if (!WriteData(_bufferOut, MCProtocol.MC_QHEADER_COMMAND_SIZE + MCProtocol.MC_BATCH_COMMAND_SIZE))
  698. {
  699. return null;
  700. }
  701. //receiveLen < MCProtocol.MC_QHEADER_RESPONSE_SIZE means no data in bufferIn
  702. int receiveLen = ReceiveData(_bufferIn);
  703. int dataLength = receiveLen - MCProtocol.MC_QHEADER_RESPONSE_SIZE;
  704. if (dataLength < 0)
  705. {
  706. return null;
  707. }
  708. MCProtocol.MC_RESPONSE_HEADER responseHeader =
  709. MCProtocol.ToStruct<MCProtocol.MC_RESPONSE_HEADER>(_bufferIn);
  710. if (responseHeader.ProtocolId != 0x00d0 && responseHeader.CompleteCode != MCProtocol.MC_COMPLETE_CODE_SUCCESS)
  711. {
  712. return null;
  713. }
  714. byte[] dataBuff = new byte[dataLength];
  715. Array.Copy(_bufferIn, MCProtocol.MC_QHEADER_RESPONSE_SIZE, dataBuff, 0, dataBuff.Length);
  716. return dataBuff;
  717. }
  718. private byte[] DoReadAoGetByte(int offset, int size, string _aiStoragename)
  719. {
  720. _batchCommand.DeviceCode = (byte)MelsecElement.ChooseMelsecElement(_aiStoragename).m_nBinCode;
  721. _batchCommand.Command = MCProtocol.MC_COMMAND_BATCH_READ;
  722. _batchCommand.SubCommand = MCProtocol.MC_SUBCOMMAND_WORD_UNITS;
  723. _batchCommand.HeadAddr = (ushort)(offset & 0xFFFF);
  724. _batchCommand.Reserved = (byte)(offset >> 16);
  725. _batchCommand.DevicePoints = (ushort)size;
  726. _header.RequestDataLen = (ushort)(MCProtocol.MC_BATCH_COMMAND_SIZE + MCProtocol.JUNK_SIZE);
  727. byte[] buffer = MCProtocol.Struct2Bytes(_header);
  728. byte[] command = MCProtocol.Struct2Bytes(_batchCommand);
  729. Array.Copy(buffer, 0, _bufferOut, 0, buffer.Length);
  730. Array.Copy(command, 0, _bufferOut, MCProtocol.MC_QHEADER_COMMAND_SIZE, command.Length);
  731. if (!WriteData(_bufferOut, MCProtocol.MC_QHEADER_COMMAND_SIZE + MCProtocol.MC_BATCH_COMMAND_SIZE))
  732. {
  733. return null;
  734. }
  735. //receiveLen < MCProtocol.MC_QHEADER_RESPONSE_SIZE means no data in bufferIn
  736. int receiveLen = ReceiveData(_bufferIn);
  737. int dataLength = receiveLen - MCProtocol.MC_QHEADER_RESPONSE_SIZE;
  738. if (dataLength < 0)
  739. {
  740. return null;
  741. }
  742. MCProtocol.MC_RESPONSE_HEADER responseHeader =
  743. MCProtocol.ToStruct<MCProtocol.MC_RESPONSE_HEADER>(_bufferIn);
  744. if (responseHeader.ProtocolId != 0x00d0 && responseHeader.CompleteCode != MCProtocol.MC_COMPLETE_CODE_SUCCESS)
  745. {
  746. return null;
  747. }
  748. byte[] dataBuff = new byte[dataLength];
  749. Array.Copy(_bufferIn, MCProtocol.MC_QHEADER_RESPONSE_SIZE, dataBuff, 0, dataBuff.Length);
  750. return dataBuff;
  751. }
  752. private bool WriteData(byte[] data, int length)
  753. {
  754. return _socket.Write(data, length);
  755. }
  756. private int ReceiveData(byte[] data)
  757. {
  758. return _socket.Read(data);
  759. }
  760. private short[] DoReadAi(int offset, int size)
  761. {
  762. _batchCommand.DeviceCode = (byte)MelsecElement.ChooseMelsecElement(_aiStoragename).m_nBinCode;
  763. _batchCommand.Command = MCProtocol.MC_COMMAND_BATCH_READ;
  764. _batchCommand.SubCommand = MCProtocol.MC_SUBCOMMAND_WORD_UNITS;
  765. _batchCommand.HeadAddr = (ushort)(offset & 0xFFFF);
  766. _batchCommand.Reserved = (byte)(offset >> 16);
  767. _batchCommand.DevicePoints = (ushort)size;
  768. _header.RequestDataLen = (ushort)(MCProtocol.MC_BATCH_COMMAND_SIZE + MCProtocol.JUNK_SIZE);
  769. byte[] buffer = MCProtocol.Struct2Bytes(_header);
  770. byte[] command = MCProtocol.Struct2Bytes(_batchCommand);
  771. Array.Copy(buffer, 0, _bufferOut, 0, buffer.Length);
  772. Array.Copy(command, 0, _bufferOut, MCProtocol.MC_QHEADER_COMMAND_SIZE, command.Length);
  773. if (!WriteData(_bufferOut, MCProtocol.MC_QHEADER_COMMAND_SIZE + MCProtocol.MC_BATCH_COMMAND_SIZE))
  774. {
  775. return null;
  776. }
  777. //receiveLen < MCProtocol.MC_QHEADER_RESPONSE_SIZE means no data in bufferIn
  778. int receiveLen = ReceiveData(_bufferIn);
  779. int dataLength = receiveLen - MCProtocol.MC_QHEADER_RESPONSE_SIZE;
  780. if (dataLength < 0)
  781. {
  782. return null;
  783. }
  784. MCProtocol.MC_RESPONSE_HEADER responseHeader =
  785. MCProtocol.ToStruct<MCProtocol.MC_RESPONSE_HEADER>(_bufferIn);
  786. if (responseHeader.ProtocolId != 0x00d0 && responseHeader.CompleteCode != MCProtocol.MC_COMPLETE_CODE_SUCCESS)
  787. {
  788. return null;
  789. }
  790. short[] datas = new short[size];
  791. byte[] dataBuff = new byte[dataLength];
  792. Array.Copy(_bufferIn, MCProtocol.MC_QHEADER_RESPONSE_SIZE, dataBuff, 0, dataBuff.Length);
  793. for (int i = 0, dataBuffIndex = 0; i < datas.Length & dataBuffIndex < dataBuff.Length; i++, dataBuffIndex += 2)
  794. {
  795. datas[i] = (short)BitConverter.ToUInt16(dataBuff, 0 + i * 2);
  796. }
  797. return datas;
  798. }
  799. private short[] DoReadAo(int offset, int size)
  800. {
  801. _batchCommand.DeviceCode = (byte)MelsecElement.ChooseMelsecElement(_aiStoragename).m_nBinCode;
  802. _batchCommand.Command = MCProtocol.MC_COMMAND_BATCH_READ;
  803. _batchCommand.SubCommand = MCProtocol.MC_SUBCOMMAND_WORD_UNITS;
  804. _batchCommand.HeadAddr = (ushort)(offset & 0xFFFF);
  805. _batchCommand.Reserved = (byte)(offset >> 16);
  806. _batchCommand.DevicePoints = (ushort)size;
  807. _header.RequestDataLen = (ushort)(MCProtocol.MC_BATCH_COMMAND_SIZE + MCProtocol.JUNK_SIZE);
  808. byte[] buffer = MCProtocol.Struct2Bytes(_header);
  809. byte[] command = MCProtocol.Struct2Bytes(_batchCommand);
  810. Array.Copy(buffer, 0, _bufferOut, 0, buffer.Length);
  811. Array.Copy(command, 0, _bufferOut, MCProtocol.MC_QHEADER_COMMAND_SIZE, command.Length);
  812. if (!WriteData(_bufferOut, MCProtocol.MC_QHEADER_COMMAND_SIZE + MCProtocol.MC_BATCH_COMMAND_SIZE))
  813. {
  814. return null;
  815. }
  816. //receiveLen < MCProtocol.MC_QHEADER_RESPONSE_SIZE means no data in bufferIn
  817. int receiveLen = ReceiveData(_bufferIn);
  818. int dataLength = receiveLen - MCProtocol.MC_QHEADER_RESPONSE_SIZE;
  819. if (dataLength < 0)
  820. {
  821. return null;
  822. }
  823. MCProtocol.MC_RESPONSE_HEADER responseHeader =
  824. MCProtocol.ToStruct<MCProtocol.MC_RESPONSE_HEADER>(_bufferIn);
  825. if (responseHeader.ProtocolId != 0x00d0 && responseHeader.CompleteCode != MCProtocol.MC_COMPLETE_CODE_SUCCESS)
  826. {
  827. return null;
  828. }
  829. short[] datas = new short[size];
  830. byte[] dataBuff = new byte[dataLength];
  831. Array.Copy(_bufferIn, MCProtocol.MC_QHEADER_RESPONSE_SIZE, dataBuff, 0, dataBuff.Length);
  832. for (int i = 0, dataBuffIndex = 0; i < datas.Length & dataBuffIndex < dataBuff.Length; i++, dataBuffIndex += 2)
  833. {
  834. datas[i] = (short)BitConverter.ToUInt16(dataBuff, 0 + i * 2);
  835. }
  836. return datas;
  837. }
  838. private void DoWriteAo(int offset, short[] data, int total = 0, int count = 100)
  839. {
  840. ushort[] uData = new ushort[data.Length];
  841. uData = data.Select(x => (ushort)x).ToArray();
  842. _batchCommand.DeviceCode = (byte)MelsecElement.ChooseMelsecElement(_aoStoragename).m_nBinCode;
  843. _batchCommand.Command = MCProtocol.MC_COMMAND_BATCH_WRITE;
  844. _batchCommand.SubCommand = MCProtocol.MC_SUBCOMMAND_WORD_UNITS;
  845. _batchCommand.HeadAddr = (ushort)(offset & 0xFFFF);
  846. _batchCommand.Reserved = (byte)(offset >> 16);
  847. _batchCommand.DevicePoints = (ushort)data.Length;
  848. _header.RequestDataLen = (ushort)(MCProtocol.MC_BATCH_COMMAND_SIZE + MCProtocol.JUNK_SIZE + MCProtocol.ShortSize * data.Length);
  849. byte[] header = MCProtocol.Struct2Bytes(_header);
  850. byte[] command = MCProtocol.Struct2Bytes(_batchCommand);
  851. byte[] byteData = MCProtocol.Ushort2Byte(uData);
  852. Array.Copy(header, 0, _bufferOut, 0, header.Length);
  853. Array.Copy(command, 0, _bufferOut, MCProtocol.MC_QHEADER_COMMAND_SIZE, command.Length);
  854. Array.Copy(byteData, 0, _bufferOut, MCProtocol.MC_QHEADER_COMMAND_SIZE + MCProtocol.MC_BATCH_COMMAND_SIZE, byteData.Length);
  855. if (!WriteData(_bufferOut, MCProtocol.MC_QHEADER_COMMAND_SIZE + MCProtocol.MC_BATCH_COMMAND_SIZE + MCProtocol.ShortSize * data.Length))
  856. {
  857. return;
  858. }
  859. if (ReceiveData(_bufferIn) <= 0)
  860. {
  861. return;
  862. }
  863. MCProtocol.MC_RESPONSE_HEADER responseHeader =
  864. MCProtocol.ToStruct<MCProtocol.MC_RESPONSE_HEADER>(_bufferIn);
  865. if (responseHeader.ProtocolId != 0x00d0 && responseHeader.CompleteCode != MCProtocol.MC_COMPLETE_CODE_SUCCESS)
  866. {
  867. LOG.Write(" failed with code," + responseHeader.CompleteCode);
  868. return;
  869. }
  870. }
  871. private bool[] DoReadDi(int offset, int size)
  872. {
  873. _batchCommand.DeviceCode = (byte)MelsecElement.ChooseMelsecElement(_diStoragename).m_nBinCode;
  874. _batchCommand.Command = MCProtocol.MC_COMMAND_BATCH_READ;
  875. _batchCommand.SubCommand = MCProtocol.MC_SUBCOMMAND_WORD_UNITS;
  876. _batchCommand.HeadAddr = (ushort)(offset & 0xFFFF);
  877. _batchCommand.Reserved = (byte)(offset >> 16);
  878. _batchCommand.DevicePoints = (ushort)(size % 16 > 0 ? size / 16 + 1 : size / 16);
  879. _header.RequestDataLen = (ushort)(MCProtocol.MC_BATCH_COMMAND_SIZE + MCProtocol.JUNK_SIZE);
  880. byte[] buffer = MCProtocol.Struct2Bytes(_header);
  881. byte[] command = MCProtocol.Struct2Bytes(_batchCommand);
  882. Array.Copy(buffer, 0, _bufferOut, 0, buffer.Length);
  883. Array.Copy(command, 0, _bufferOut, MCProtocol.MC_QHEADER_COMMAND_SIZE, command.Length);
  884. //50 00 00 ff ff 03 00 0c 00 10 00 01 04 00 00 00 00 00 a0 02 00 00 00 00 00 00 00 00 00 00 00 00 00
  885. if (!WriteData(_bufferOut, MCProtocol.MC_QHEADER_COMMAND_SIZE + MCProtocol.MC_BATCH_COMMAND_SIZE))
  886. {
  887. return null;
  888. }
  889. //receiveLen < MCProtocol.MC_QHEADER_RESPONSE_SIZE means no data in bufferIn
  890. int receiveLen = ReceiveData(_bufferIn);
  891. int dataLength = receiveLen - MCProtocol.MC_QHEADER_RESPONSE_SIZE;
  892. if (dataLength < 0)
  893. {
  894. return null;
  895. }
  896. MCProtocol.MC_RESPONSE_HEADER responseHeader =
  897. MCProtocol.ToStruct<MCProtocol.MC_RESPONSE_HEADER>(_bufferIn);
  898. if (responseHeader.ProtocolId != 0x00d0 && responseHeader.CompleteCode != MCProtocol.MC_COMPLETE_CODE_SUCCESS)
  899. {
  900. return null;
  901. }
  902. bool[] datas = new bool[dataLength * 8];
  903. byte[] dataBuff = new byte[dataLength];
  904. Array.Copy(_bufferIn, MCProtocol.MC_QHEADER_RESPONSE_SIZE, dataBuff, 0, dataBuff.Length);
  905. var bitArray = new BitArray(dataBuff);
  906. for (int i = 0; i < datas.Length; i++)
  907. {
  908. datas[i] = bitArray.Get(i);
  909. }
  910. return datas.Select(x => x).Take(size).ToArray();
  911. }
  912. private bool[] DoReadDo(int offset, int size)
  913. {
  914. _batchCommand.DeviceCode = (byte)MelsecElement.ChooseMelsecElement(_diStoragename).m_nBinCode;
  915. _batchCommand.Command = MCProtocol.MC_COMMAND_BATCH_READ;
  916. _batchCommand.SubCommand = MCProtocol.MC_SUBCOMMAND_WORD_UNITS;
  917. _batchCommand.HeadAddr = (ushort)(offset & 0xFFFF);
  918. _batchCommand.Reserved = (byte)(offset >> 16);
  919. _batchCommand.DevicePoints = (ushort)(size % 16 > 0 ? size / 16 + 1 : size / 16);
  920. _header.RequestDataLen = (ushort)(MCProtocol.MC_BATCH_COMMAND_SIZE + MCProtocol.JUNK_SIZE);
  921. byte[] buffer = MCProtocol.Struct2Bytes(_header);
  922. byte[] command = MCProtocol.Struct2Bytes(_batchCommand);
  923. Array.Copy(buffer, 0, _bufferOut, 0, buffer.Length);
  924. Array.Copy(command, 0, _bufferOut, MCProtocol.MC_QHEADER_COMMAND_SIZE, command.Length);
  925. //50 00 00 ff ff 03 00 0c 00 10 00 01 04 00 00 00 00 00 a0 02 00 00 00 00 00 00 00 00 00 00 00 00 00
  926. if (!WriteData(_bufferOut, MCProtocol.MC_QHEADER_COMMAND_SIZE + MCProtocol.MC_BATCH_COMMAND_SIZE))
  927. {
  928. return null;
  929. }
  930. //receiveLen < MCProtocol.MC_QHEADER_RESPONSE_SIZE means no data in bufferIn
  931. int receiveLen = ReceiveData(_bufferIn);
  932. int dataLength = receiveLen - MCProtocol.MC_QHEADER_RESPONSE_SIZE;
  933. if (dataLength < 0)
  934. {
  935. return null;
  936. }
  937. MCProtocol.MC_RESPONSE_HEADER responseHeader =
  938. MCProtocol.ToStruct<MCProtocol.MC_RESPONSE_HEADER>(_bufferIn);
  939. if (responseHeader.ProtocolId != 0x00d0 && responseHeader.CompleteCode != MCProtocol.MC_COMPLETE_CODE_SUCCESS)
  940. {
  941. return null;
  942. }
  943. bool[] datas = new bool[dataLength * 8];
  944. byte[] dataBuff = new byte[dataLength];
  945. Array.Copy(_bufferIn, MCProtocol.MC_QHEADER_RESPONSE_SIZE, dataBuff, 0, dataBuff.Length);
  946. var bitArray = new BitArray(dataBuff);
  947. for (int i = 0; i < datas.Length; i++)
  948. {
  949. datas[i] = bitArray.Get(i);
  950. }
  951. return datas.Select(x => x).Take(size).ToArray();
  952. }
  953. private void DoWriteDo(int offset, bool[] data)
  954. {
  955. _batchCommand.DeviceCode = (byte)MelsecElement.ChooseMelsecElement(_doStoragename).m_nBinCode;
  956. _batchCommand.Command = MCProtocol.MC_COMMAND_BATCH_WRITE;
  957. _batchCommand.SubCommand = MCProtocol.MC_SUBCOMMAND_WORD_UNITS;
  958. _batchCommand.HeadAddr = (ushort)(offset & 0xFFFF);
  959. _batchCommand.Reserved = (byte)(offset >> 16);
  960. _batchCommand.DevicePoints = (ushort)(data.Length % 16 > 0 ? data.Length / 16 + 1 : data.Length / 16);
  961. _header.RequestDataLen = (ushort)(MCProtocol.MC_BATCH_COMMAND_SIZE + MCProtocol.JUNK_SIZE + _batchCommand.DevicePoints * 2);
  962. //one word = two bytes , so _batchCommand.DevicePoints * 2
  963. byte[] byteData = new byte[_batchCommand.DevicePoints * 2];
  964. for (int i = 0, byteDataIndex = 0; i < data.Length & byteDataIndex < byteData.Length; i += 8, byteDataIndex++)
  965. {
  966. byteData[byteDataIndex] += (byte)(data[i] ? 1 : 0);
  967. byteData[byteDataIndex] += (byte)(i + 1 < data.Length && data[i + 1] ? 1 << 1 : 0);
  968. byteData[byteDataIndex] += (byte)(i + 2 < data.Length && data[i + 2] ? 1 << 2 : 0);
  969. byteData[byteDataIndex] += (byte)(i + 3 < data.Length && data[i + 3] ? 1 << 3 : 0);
  970. byteData[byteDataIndex] += (byte)(i + 4 < data.Length && data[i + 4] ? 1 << 4 : 0);
  971. byteData[byteDataIndex] += (byte)(i + 5 < data.Length && data[i + 5] ? 1 << 5 : 0);
  972. byteData[byteDataIndex] += (byte)(i + 6 < data.Length && data[i + 6] ? 1 << 6 : 0);
  973. byteData[byteDataIndex] += (byte)(i + 7 < data.Length && data[i + 7] ? 1 << 7 : 0);
  974. }
  975. byte[] header = MCProtocol.Struct2Bytes(_header);
  976. byte[] command = MCProtocol.Struct2Bytes(_batchCommand);
  977. Array.Copy(header, 0, _bufferOut, 0, header.Length);
  978. Array.Copy(command, 0, _bufferOut, MCProtocol.MC_QHEADER_COMMAND_SIZE, command.Length);
  979. Array.Copy(byteData, 0, _bufferOut, MCProtocol.MC_QHEADER_COMMAND_SIZE + MCProtocol.MC_BATCH_COMMAND_SIZE, byteData.Length);
  980. if (!WriteData(_bufferOut, MCProtocol.MC_QHEADER_COMMAND_SIZE + MCProtocol.MC_BATCH_COMMAND_SIZE + byteData.Length))
  981. {
  982. return;
  983. }
  984. if (ReceiveData(_bufferIn) <= 0)
  985. {
  986. return;
  987. }
  988. MCProtocol.MC_RESPONSE_HEADER responseHeader =
  989. MCProtocol.ToStruct<MCProtocol.MC_RESPONSE_HEADER>(_bufferIn);
  990. if (responseHeader.CompleteCode != MCProtocol.MC_COMPLETE_CODE_SUCCESS)
  991. {
  992. LOG.Write("Write PLC failed with code," + responseHeader.CompleteCode);
  993. return;
  994. }
  995. }
  996. }
  997. class MCSocket
  998. {
  999. private int m_nPort;
  1000. private string m_strAddress;
  1001. private Socket m_socket;
  1002. private int m_nTimeOut;
  1003. public MCSocket()
  1004. {
  1005. m_nTimeOut = 30000;
  1006. }
  1007. ~MCSocket()
  1008. {
  1009. }
  1010. public bool Connected
  1011. {
  1012. get { return (m_socket != null && m_socket.Connected); }
  1013. }
  1014. public bool Open(string strAddress, int nPort, string strLocalAddress)
  1015. {
  1016. if (Connected)
  1017. return true;
  1018. Close();
  1019. m_strAddress = strAddress;
  1020. m_nPort = nPort;
  1021. m_socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
  1022. try
  1023. {
  1024. // Bind to specific local endpoint
  1025. IPEndPoint localEndPoint = new IPEndPoint(IPAddress.Any, 0);
  1026. m_socket.Bind(localEndPoint);
  1027. IPAddress ipAddress = IPAddress.Parse(m_strAddress);
  1028. IPEndPoint ipEndPoint = new IPEndPoint(ipAddress, m_nPort);
  1029. //m_socket = new Socket(ipEndPoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
  1030. m_socket.SendTimeout = m_nTimeOut;
  1031. m_socket.ReceiveTimeout = m_nTimeOut;
  1032. m_socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive, true);
  1033. m_socket.Connect(ipEndPoint);
  1034. }
  1035. catch (Exception ex)
  1036. {
  1037. LOG.Write(ex);
  1038. return false;
  1039. }
  1040. return true;
  1041. }
  1042. public bool Close()
  1043. {
  1044. if (m_socket == null) return true;
  1045. try
  1046. {
  1047. m_socket.Shutdown(SocketShutdown.Both);
  1048. m_socket.Close();
  1049. m_socket = null;
  1050. }
  1051. catch (Exception ex)
  1052. {
  1053. LOG.Write(ex);
  1054. return false;
  1055. }
  1056. return true;
  1057. }
  1058. public int Read(byte[] buffer)
  1059. {
  1060. int ret = -1;
  1061. if (!Connected) return ret;
  1062. return m_socket.Receive(buffer);
  1063. //try
  1064. //{
  1065. // ret = m_socket.Receive(buffer);
  1066. //}
  1067. //catch (Exception ex)
  1068. //{
  1069. // LOG.Write(ex);
  1070. //}
  1071. //return ret;
  1072. }
  1073. public bool Read(byte[] buffer, int length)
  1074. {
  1075. if (!Connected) return false;
  1076. try
  1077. {
  1078. m_socket.Receive(buffer, SocketFlags.None);
  1079. }
  1080. catch (Exception ex)
  1081. {
  1082. LOG.Write(ex);
  1083. return false;
  1084. }
  1085. return true;
  1086. }
  1087. public bool Write(byte[] buffer)
  1088. {
  1089. if (!Connected) return false;
  1090. try
  1091. {
  1092. m_socket.Send(buffer, SocketFlags.None);
  1093. }
  1094. catch (Exception ex)
  1095. {
  1096. LOG.Write(ex);
  1097. return false;
  1098. }
  1099. return true;
  1100. }
  1101. public bool Write(byte[] buffer, int length)
  1102. {
  1103. if (!Connected) return false;
  1104. return m_socket.Send(buffer, length, SocketFlags.None) >= 0;
  1105. //try
  1106. //{
  1107. // m_socket.Send(buffer, length, SocketFlags.None);
  1108. //}
  1109. //catch (Exception ex)
  1110. //{
  1111. // LOG.Write(ex);
  1112. // return false;
  1113. //}
  1114. //return true;
  1115. }
  1116. }
  1117. }