ModbusTCP.cs 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995
  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 HslCommunication;
  9. using HslCommunication.ModBus;
  10. using MECF.Framework.Common.IOCore;
  11. using MECF.Framework.Common.PLC;
  12. using MECF.Framework.RT.Core.IoProviders;
  13. using System;
  14. using System.Collections.Generic;
  15. using System.Diagnostics;
  16. using System.Linq;
  17. using System.Text;
  18. using System.Threading.Tasks;
  19. using System.Xml;
  20. namespace FurnaceRT.Equipments.Systems
  21. {
  22. public class ModbusTCP : IoProvider
  23. {
  24. private string _address;
  25. private Int32 _port;
  26. private byte _station;
  27. public int _diStartAddress;
  28. public int _doStartAddress;
  29. public int _aiStartAddress;
  30. public int _aoStartAddress;
  31. public string _dataformat;
  32. public bool _addressstartwithzero;
  33. public bool _stringReverse;
  34. private ModbusTcpNet _readerClient = null;
  35. private WcfPlc _wcfPlc = null;
  36. public bool IsCommunicationError { get; set; } = false;
  37. private R_TRIG _trigRetryConnect = new R_TRIG();
  38. private Stopwatch _stopwatchTotal = new Stopwatch();
  39. private bool _enableLog = true;
  40. private int _failCount = 0;
  41. private object _locker = new object();
  42. protected override void SetParameter(XmlElement nodeParameter)
  43. {
  44. _address = nodeParameter.GetAttribute("ip");
  45. _port = Convert.ToInt32(nodeParameter.GetAttribute("port"));
  46. _station = (byte)Convert.ToInt32(nodeParameter.GetAttribute("station_id"));
  47. //_stringReverse = Convert.ToBoolean(nodeParameter.GetAttribute("stringReverse"));
  48. _diStartAddress = int.Parse(nodeParameter.GetAttribute("diStartAddress"));
  49. _aiStartAddress = int.Parse(nodeParameter.GetAttribute("aiStartAddress"));
  50. _doStartAddress = int.Parse(nodeParameter.GetAttribute("doStartAddress"));
  51. _aoStartAddress = int.Parse(nodeParameter.GetAttribute("aoStartAddress"));
  52. int interval = nodeParameter.HasAttribute("interval") ? int.Parse(nodeParameter.GetAttribute("interval")) : 50;
  53. //_dataformat = nodeParameter.GetAttribute("dataformat");
  54. //DisconnectAlarm = SubscribeAlarm($"{Name}.DisconnectAlarm", "", ResetAlarm);
  55. //DisconnectAlarm.Id = 1030;
  56. //CommunicationErrorAlarm = SubscribeAlarm($"{Name}.DisconnectAlarm", "", ResetAlarm);
  57. //CommunicationErrorAlarm.Id = 1031;
  58. _thread = new PeriodicJob(interval, OnTimer, Name);
  59. _enableLog = SC.GetValue<bool>($"System.ModbusCommunicationEnableLogMessage");
  60. OP.Subscribe($"Sysytem.{Name}.ModbusPLC", InvokeReset);
  61. if (SC.GetValue<bool>("System.IsSimulatorMode"))
  62. {
  63. _wcfPlc = new WcfPlc("System", Name, $"WcfPlc_{Name}");
  64. _wcfPlc.Initialize();
  65. }
  66. else
  67. {
  68. _readerClient = new ModbusTcpNet(_address, _port, _station);
  69. Open();
  70. }
  71. }
  72. public int _connecteTimes { get; set; }
  73. private R_TRIG _trigConnected = new R_TRIG();
  74. private R_TRIG _trigReadDoAndAo = new R_TRIG();
  75. //public override bool ResetAlarm()
  76. //{
  77. // Reset();
  78. // return true;
  79. //}
  80. public override void Initialize(string module, string name, List<IoBlockItem> lstBuffers, IIoBuffer buffer, XmlElement nodeParameter, string ioMappingPathFile, string ioModule)
  81. {
  82. //if (SC.GetValue<bool>("System.IsSimulatorMode"))
  83. // _wcfPlc.Initialize();
  84. Module = module;
  85. Name = name;
  86. _source = module + "." + name;
  87. _buffer = buffer;
  88. _nodeParameter = nodeParameter;
  89. _blockSections = lstBuffers;
  90. buffer.SetBufferBlock(_source, lstBuffers);
  91. buffer.SetIoMapByModule(_source, 0, ioMappingPathFile, ioModule);
  92. SetParameter(nodeParameter);
  93. State = IoProviderStateEnum.Uninitialized;
  94. }
  95. protected override bool OnTimer()
  96. {
  97. try
  98. {
  99. _stopwatchTotal.Start();
  100. foreach (var bufferSection in _blockSections)
  101. {
  102. if (bufferSection.Type == IoType.DI)
  103. {
  104. bool[] diBuffer = ReadDi(bufferSection.Offset, bufferSection.Size);
  105. if (diBuffer != null)
  106. {
  107. _buffer.SetDiBuffer(_source, bufferSection.Offset, diBuffer);
  108. //TraceArray(diBuffer);
  109. }
  110. }
  111. else if (bufferSection.Type == IoType.DO)
  112. {
  113. if (_trigConnected.Q && !_trigReadDoAndAo.M)
  114. {
  115. bool[] doBuffer = ReadDo(bufferSection.Offset, bufferSection.Size);
  116. if (doBuffer != null)
  117. {
  118. _buffer.SetDoBuffer(_source, bufferSection.Offset, doBuffer);
  119. }
  120. }
  121. }
  122. else if (bufferSection.Type == IoType.AI)
  123. {
  124. if (bufferSection.AIOType == typeof(float))
  125. {
  126. float[] aiBuffer = ReadAiFloat(bufferSection.Offset, bufferSection.Size);
  127. if (aiBuffer != null)
  128. {
  129. _buffer.SetAiBufferFloat(_source, bufferSection.Offset, aiBuffer);
  130. }
  131. }
  132. else
  133. {
  134. short[] aiBuffer = ReadAi(bufferSection.Offset, bufferSection.Size);
  135. if (aiBuffer != null)
  136. {
  137. _buffer.SetAiBuffer(_source, bufferSection.Offset, aiBuffer);
  138. }
  139. }
  140. }
  141. else if (bufferSection.Type == IoType.AO)
  142. {
  143. if (bufferSection.AIOType == typeof(float))
  144. {
  145. if (_trigConnected.Q && !_trigReadDoAndAo.M)
  146. {
  147. float[] aoBuffer = ReadAoFloat(bufferSection.Offset, bufferSection.Size);
  148. if (aoBuffer != null)
  149. {
  150. _buffer.SetAoBufferFloat(_source, bufferSection.Offset, aoBuffer);
  151. }
  152. }
  153. }
  154. else
  155. {
  156. if (_trigConnected.Q && !_trigReadDoAndAo.M)
  157. {
  158. short[] aoBuffer = ReadAo(bufferSection.Offset, bufferSection.Size);
  159. if (aoBuffer != null)
  160. {
  161. _buffer.SetAoBuffer(_source, bufferSection.Offset, aoBuffer);
  162. }
  163. }
  164. }
  165. }
  166. }
  167. if (!_trigReadDoAndAo.M)
  168. _trigReadDoAndAo.CLK = true;
  169. //if (SC.GetValue<bool>("System.IsSimulatorMode"))
  170. {
  171. Dictionary<int, bool[]> dos = _buffer.GetDoBuffer(_source);
  172. if (dos != null)
  173. {
  174. foreach (var doo in dos)
  175. {
  176. WriteDo(doo.Key, doo.Value);
  177. }
  178. }
  179. var aoSection = _blockSections.FirstOrDefault(P => P.Type == IoType.AO);
  180. if (aoSection != null)
  181. {
  182. if (aoSection.AIOType == typeof(float))
  183. {
  184. Dictionary<int, float[]> aos = _buffer.GetAoBufferFloat(_source);
  185. if (aos != null)
  186. {
  187. foreach (var ao in aos)
  188. {
  189. WriteAoFloat(ao.Key, ao.Value);
  190. }
  191. }
  192. }
  193. else
  194. {
  195. Dictionary<int, short[]> aos = _buffer.GetAoBuffer(_source);
  196. if (aos != null)
  197. {
  198. foreach (var ao in aos)
  199. {
  200. WriteAo(ao.Key, ao.Value);
  201. }
  202. }
  203. }
  204. }
  205. }
  206. var comunicationSpanTotal = (int)_stopwatchTotal.ElapsedMilliseconds;
  207. //LOG.Write($"Modbus {comunicationSpanTotal}");
  208. _stopwatchTotal.Restart();
  209. }
  210. catch (Exception ex)
  211. {
  212. LOG.Write(ex);
  213. Close();
  214. }
  215. return true;
  216. }
  217. public override void Reset()
  218. {
  219. _trigError.RST = true;
  220. _trigNotConnected.RST = true;
  221. if (!_trigConnected.M)
  222. {
  223. if (!SC.GetValue<bool>("System.IsSimulatorMode"))
  224. {
  225. Close();
  226. Open();
  227. }
  228. }
  229. }
  230. private void TrigDisconnectAlarm()
  231. {
  232. _trigNotConnected.CLK = true;
  233. if (_trigNotConnected.T)
  234. {
  235. EV.PostInfoLog(Module, $"{Module} {_address}:{_port} connected");
  236. }
  237. if (_trigNotConnected.R)
  238. {
  239. EV.PostAlarmLog(Module, $"{Module} {_address}:{_port} not connected");
  240. }
  241. }
  242. protected override bool[] ReadDi(int offset, int size)
  243. {
  244. if (SC.GetValue<bool>("System.IsSimulatorMode"))
  245. {
  246. int address = offset + _aiStartAddress;
  247. var ret = _wcfPlc.ReadBool(address.ToString(), out bool[] data, size, out _);
  248. return data;
  249. }
  250. else
  251. {
  252. if (_trigNotConnected.M)
  253. return null;
  254. var dataDi = DATA.Poll($"System.{Name}.DIItemList");
  255. bool[] data = new bool[size];
  256. if (dataDi != null)
  257. {
  258. List<NotifiableIoItem> item = (List<NotifiableIoItem>)dataDi;
  259. int address = _diStartAddress;
  260. int iOldAddress = _diStartAddress;
  261. ushort iCount = 0;
  262. int iIndex = 0;
  263. for (int i = 0; i < item.Count; i++)
  264. {
  265. int.TryParse(item[i].Address, out int iNewAddress);
  266. if (iNewAddress == iOldAddress)
  267. {
  268. iCount++;
  269. continue;
  270. }
  271. else
  272. {
  273. if (iNewAddress == iOldAddress + 1)
  274. {
  275. iCount++;
  276. iOldAddress = iNewAddress;
  277. }
  278. else
  279. {
  280. OperateResult<short[]> ret;
  281. lock (_locker)
  282. {
  283. ret = _readerClient.ReadInt16(address.ToString(), iCount);
  284. }
  285. if (ret != null && ret.IsSuccess)
  286. {
  287. for (int j = 0; j < ret.Content.Length; j++)
  288. {
  289. data[iIndex + j] = ret.Content[j] > 0 ? true : false;
  290. }
  291. _failCount = 0;
  292. }
  293. else
  294. {
  295. _failCount++;
  296. }
  297. iIndex = iNewAddress - _diStartAddress;
  298. address = iNewAddress;
  299. iOldAddress = iNewAddress;
  300. iCount = 1;
  301. }
  302. }
  303. if (i == item.Count - 1)
  304. {
  305. OperateResult<short[]> ret;
  306. lock (_locker)
  307. {
  308. ret = _readerClient.ReadInt16(address.ToString(), iCount);
  309. }
  310. if (ret != null && ret.IsSuccess)
  311. {
  312. for (int j = 0; j < ret.Content.Length; j++)
  313. {
  314. data[iIndex + j] = ret.Content[j] > 0 ? true : false;
  315. }
  316. _failCount = 0;
  317. }
  318. else
  319. {
  320. _failCount++;
  321. }
  322. }
  323. }
  324. }
  325. if (_failCount >= 5)
  326. TrigDisconnectAlarm();
  327. return data;
  328. }
  329. }
  330. protected override float[] ReadAiFloat(int offset, int size)
  331. {
  332. if (SC.GetValue<bool>("System.IsSimulatorMode"))
  333. {
  334. int address = offset + _aiStartAddress;
  335. var ret = _wcfPlc.ReadFloat(address.ToString(), out float[] data, size, out _);
  336. return data;
  337. }
  338. else
  339. {
  340. if (_trigNotConnected.M)
  341. return null;
  342. var dataAi = DATA.Poll($"System.{Name}.AIItemList");
  343. float[] data = new float[size];
  344. if (dataAi != null)
  345. {
  346. List<NotifiableIoItem> item = (List<NotifiableIoItem>)dataAi;
  347. int address = _aiStartAddress;
  348. int iOldAddress = _aiStartAddress;
  349. ushort iCount = 0;
  350. int iIndex = 0;
  351. for (int i = 0; i < item.Count; i++)
  352. {
  353. int.TryParse(item[i].Address, out int iNewAddress);
  354. if (iNewAddress == iOldAddress)
  355. {
  356. iCount++;
  357. continue;
  358. }
  359. else
  360. {
  361. if (iNewAddress == iOldAddress + 2)
  362. {
  363. iCount++;
  364. iOldAddress = iNewAddress;
  365. }
  366. else
  367. {
  368. Task<OperateResult<float[]>> ret;
  369. lock (_locker)
  370. {
  371. ret = _readerClient.ReadFloatAsync(address.ToString(), iCount);
  372. }
  373. if (ret != null && ret.Result.IsSuccess)
  374. {
  375. for (int j = 0; j < ret.Result.Content.Length; j++)
  376. {
  377. data[iIndex + j * 2] = ret.Result.Content[j];
  378. }
  379. }
  380. iIndex = iNewAddress - _aiStartAddress;
  381. address = iNewAddress;
  382. iOldAddress = iNewAddress;
  383. iCount = 1;
  384. }
  385. }
  386. if (i == item.Count - 1)
  387. {
  388. Task<OperateResult<float[]>> ret;
  389. lock (_locker)
  390. {
  391. ret = _readerClient.ReadFloatAsync(address.ToString(), iCount);
  392. }
  393. if (ret != null && ret.Result.IsSuccess)
  394. {
  395. for (int j = 0; j < ret.Result.Content.Length; j++)
  396. {
  397. data[iIndex + j * 2] = ret.Result.Content[j];
  398. }
  399. }
  400. }
  401. }
  402. }
  403. return data;
  404. }
  405. }
  406. protected bool[] ReadDo(int offset, int size)
  407. {
  408. if (SC.GetValue<bool>("System.IsSimulatorMode"))
  409. {
  410. return null;
  411. }
  412. else
  413. {
  414. var dataDo = DATA.Poll($"System.{Name}.DOItemList");
  415. bool[] data = new bool[size];
  416. if (dataDo != null)
  417. {
  418. List<NotifiableIoItem> item = (List<NotifiableIoItem>)dataDo;
  419. int address = _doStartAddress;
  420. int iOldAddress = _doStartAddress;
  421. ushort iCount = 0;
  422. int iIndex = 0;
  423. for (int i = 0; i < item.Count; i++)
  424. {
  425. int.TryParse(item[i].Address, out int iNewAddress);
  426. if (iNewAddress == iOldAddress)
  427. {
  428. iCount++;
  429. continue;
  430. }
  431. else
  432. {
  433. if (iNewAddress == iOldAddress + 1)
  434. {
  435. iCount++;
  436. iOldAddress = iNewAddress;
  437. }
  438. else
  439. {
  440. var ret = _readerClient.ReadInt16(address.ToString(), iCount);
  441. if (ret != null && ret.IsSuccess)
  442. {
  443. for (int j = 0; j < ret.Content.Length; j++)
  444. {
  445. data[iIndex + j] = ret.Content[j] > 0 ? true : false;
  446. }
  447. }
  448. iIndex = iNewAddress - _doStartAddress;
  449. address = iNewAddress;
  450. iOldAddress = iNewAddress;
  451. iCount = 1;
  452. }
  453. }
  454. if (i == item.Count - 1)
  455. {
  456. var ret = _readerClient.ReadInt16(address.ToString(), iCount);
  457. if (ret != null && ret.IsSuccess)
  458. {
  459. for (int j = 0; j < ret.Content.Length; j++)
  460. {
  461. data[iIndex + j] = ret.Content[j] > 0 ? true : false;
  462. }
  463. }
  464. }
  465. }
  466. }
  467. return data;
  468. }
  469. }
  470. protected override short[] ReadAi(int offset, int size)
  471. {
  472. if (SC.GetValue<bool>("System.IsSimulatorMode"))
  473. {
  474. int address = offset + _aiStartAddress;
  475. var ret = _wcfPlc.ReadInt16(address.ToString(), out short[] data, size, out _);
  476. return data;
  477. }
  478. else
  479. {
  480. if (_trigNotConnected.M)
  481. return null;
  482. var dataAi = DATA.Poll($"System.{Name}.AIItemList");
  483. short[] data = new short[size];
  484. if (dataAi != null)
  485. {
  486. List<NotifiableIoItem> item = (List<NotifiableIoItem>)dataAi;
  487. int address = _aiStartAddress;
  488. int iOldAddress = _aiStartAddress;
  489. ushort iCount = 0;
  490. int iIndex = 0;
  491. for (int i = 0; i < item.Count; i++)
  492. {
  493. int.TryParse(item[i].Address, out int iNewAddress);
  494. if (iNewAddress == iOldAddress)
  495. {
  496. iCount++;
  497. continue;
  498. }
  499. else
  500. {
  501. if (iNewAddress == iOldAddress + 1)
  502. {
  503. iCount++;
  504. iOldAddress = iNewAddress;
  505. }
  506. else
  507. {
  508. OperateResult<short[]> ret;
  509. lock (_locker)
  510. {
  511. ret = _readerClient.ReadInt16(address.ToString(), iCount);
  512. }
  513. if (ret != null && ret.IsSuccess)
  514. {
  515. for (int j = 0; j < ret.Content.Length; j++)
  516. {
  517. data[iIndex + j] = ret.Content[j];
  518. }
  519. }
  520. iIndex = iNewAddress - _aiStartAddress;
  521. address = iNewAddress;
  522. iOldAddress = iNewAddress;
  523. iCount = 1;
  524. }
  525. }
  526. if (i == item.Count - 1)
  527. {
  528. OperateResult<short[]> ret;
  529. lock (_locker)
  530. {
  531. ret = _readerClient.ReadInt16(address.ToString(), iCount);
  532. }
  533. if (ret != null && ret.IsSuccess)
  534. {
  535. for (int j = 0; j < ret.Content.Length; j++)
  536. {
  537. data[iIndex + j] = ret.Content[j];
  538. }
  539. }
  540. }
  541. }
  542. }
  543. return data;
  544. }
  545. }
  546. protected float[] ReadAoFloat(int offset, int size)
  547. {
  548. if (SC.GetValue<bool>("System.IsSimulatorMode"))
  549. {
  550. return null;
  551. }
  552. else
  553. {
  554. var dataAo = DATA.Poll($"System.{Name}.AOItemList");
  555. float[] data = new float[size];
  556. if (dataAo != null)
  557. {
  558. List<NotifiableIoItem> item = (List<NotifiableIoItem>)dataAo;
  559. int address = _aoStartAddress;
  560. int iOldAddress = _aoStartAddress;
  561. ushort iCount = 0;
  562. int iIndex = 0;
  563. for (int i = 0; i < item.Count; i++)
  564. {
  565. int.TryParse(item[i].Address, out int iNewAddress);
  566. if (iNewAddress == iOldAddress)
  567. {
  568. iCount++;
  569. continue;
  570. }
  571. else
  572. {
  573. if (iNewAddress == iOldAddress + 2)
  574. {
  575. iCount++;
  576. iOldAddress = iNewAddress;
  577. }
  578. else
  579. {
  580. OperateResult<float[]> ret;
  581. lock (_locker)
  582. {
  583. ret = _readerClient.ReadFloat(address.ToString(), iCount);
  584. }
  585. if (ret != null && ret.IsSuccess)
  586. {
  587. for (int j = 0; j < ret.Content.Length; j++)
  588. {
  589. data[iIndex + j * 2] = ret.Content[j];
  590. }
  591. }
  592. iIndex = iNewAddress - _aoStartAddress;
  593. address = iNewAddress;
  594. iOldAddress = iNewAddress;
  595. iCount = 1;
  596. }
  597. }
  598. if (i == item.Count - 1)
  599. {
  600. OperateResult<float[]> ret;
  601. lock (_locker)
  602. {
  603. ret = _readerClient.ReadFloat(address.ToString(), iCount);
  604. }
  605. if (ret != null && ret.IsSuccess)
  606. {
  607. for (int j = 0; j < ret.Content.Length; j++)
  608. {
  609. data[iIndex + j * 2] = ret.Content[j];
  610. }
  611. }
  612. }
  613. }
  614. }
  615. return data;
  616. }
  617. }
  618. protected short[] ReadAo(int offset, int size)
  619. {
  620. if (SC.GetValue<bool>("System.IsSimulatorMode"))
  621. {
  622. return null;
  623. }
  624. else
  625. {
  626. var dataAo = DATA.Poll($"System.{Name}.AOItemList");
  627. short[] data = new short[size];
  628. if (dataAo != null)
  629. {
  630. List<NotifiableIoItem> item = (List<NotifiableIoItem>)dataAo;
  631. int address = _aoStartAddress;
  632. int iOldAddress = _aoStartAddress;
  633. ushort iCount = 0;
  634. int iIndex = 0;
  635. for (int i = 0; i < item.Count; i++)
  636. {
  637. int.TryParse(item[i].Address, out int iNewAddress);
  638. if (iNewAddress == iOldAddress)
  639. {
  640. iCount++;
  641. continue;
  642. }
  643. else
  644. {
  645. if (iNewAddress == iOldAddress + 1)
  646. {
  647. iCount++;
  648. iOldAddress = iNewAddress;
  649. }
  650. else
  651. {
  652. OperateResult<short[]> ret;
  653. lock (_locker)
  654. {
  655. ret = _readerClient.ReadInt16(address.ToString(), iCount);
  656. }
  657. if (ret != null && ret.IsSuccess)
  658. {
  659. for (int j = 0; j < ret.Content.Length; j++)
  660. {
  661. data[iIndex + j] = ret.Content[j];
  662. }
  663. }
  664. iIndex = iNewAddress - _aoStartAddress;
  665. address = iNewAddress;
  666. iOldAddress = iNewAddress;
  667. iCount = 1;
  668. }
  669. }
  670. if (i == item.Count - 1)
  671. {
  672. OperateResult<short[]> ret;
  673. lock (_locker)
  674. {
  675. ret = _readerClient.ReadInt16(address.ToString(), iCount);
  676. }
  677. if (ret != null && ret.IsSuccess)
  678. {
  679. for (int j = 0; j < ret.Content.Length; j++)
  680. {
  681. data[iIndex + j] = ret.Content[j];
  682. }
  683. }
  684. }
  685. }
  686. }
  687. return data;
  688. }
  689. }
  690. public override bool SetValueFloat(AOAccessor aoItem, float value)
  691. {
  692. if (SC.GetValue<bool>("System.IsSimulatorMode"))
  693. return true;
  694. if (!_trigConnected.M)
  695. return false;
  696. if (_trigNotConnected.M)
  697. return false;
  698. try
  699. {
  700. int address = aoItem.Index + _aoStartAddress;
  701. OperateResult ret;
  702. lock (_locker)
  703. {
  704. ret = _readerClient.Write(address.ToString(), new float[] { value });
  705. }
  706. if (ret != null)
  707. {
  708. return ret.IsSuccess;
  709. }
  710. else
  711. {
  712. return false;
  713. }
  714. }
  715. catch (Exception)
  716. {
  717. _trigConnected.CLK = false;
  718. return false;
  719. }
  720. }
  721. public override bool SetValue(DOAccessor doItem, bool value)
  722. {
  723. if (SC.GetValue<bool>("System.IsSimulatorMode"))
  724. return true;
  725. if (!_trigConnected.M)
  726. return false;
  727. if (_trigNotConnected.M)
  728. return false;
  729. try
  730. {
  731. int address = doItem.Index + _doStartAddress;
  732. var ret2 = _readerClient.Write(address.ToString(), (short)(value ? 1 : 0));
  733. return ret2.IsSuccess;
  734. }
  735. catch (Exception)
  736. {
  737. _trigConnected.CLK = false;
  738. return false;
  739. }
  740. }
  741. protected override void Close()
  742. {
  743. try
  744. {
  745. _readerClient.ConnectClose();
  746. _readerClient.Dispose();
  747. _trigConnected.RST = true;
  748. }
  749. catch (Exception ex)
  750. {
  751. LOG.Write(ex.Message);
  752. }
  753. }
  754. protected override void Open()
  755. {
  756. _readerClient.IpAddress = _address;
  757. _readerClient.Port = _port;
  758. _readerClient?.ConnectClose();
  759. try
  760. {
  761. var ret = _readerClient.ConnectServer();
  762. if (ret.IsSuccess)
  763. _trigConnected.CLK = true;
  764. else
  765. {
  766. //DisconnectAlarm.Description = ret.Message;
  767. //DisconnectAlarm.Set();
  768. LOG.Write(ret.Message);
  769. }
  770. }
  771. catch (Exception ex)
  772. {
  773. IsCommunicationError = true;
  774. LOG.Write(ex.Message);
  775. //CommunicationErrorAlarm.Description = ex.Message;
  776. //CommunicationErrorAlarm.Set();
  777. }
  778. }
  779. public byte[] ReadResultRender<T>(OperateResult<T> result, string address)
  780. {
  781. byte[] buffer = new byte[32];
  782. if (result.IsSuccess)
  783. {
  784. _connecteTimes = 0;
  785. IsCommunicationError = false;
  786. var dat2 = (Convert.ToUInt32(result.Content.ToString()) & 0xFFFF);
  787. var dat1 = (Convert.ToUInt32(result.Content.ToString()) >> 16 & 0xFFFF);
  788. uint[] data = new uint[] { dat1, dat2 };
  789. for (int j = 0; j < 2; j++)
  790. {
  791. for (int i = 0; i < 16; i++)
  792. {
  793. buffer[j * 16 + i] = Convert.ToByte((data[j] >> i) & 0x01);
  794. }
  795. }
  796. string str = string.Join("", Array.ConvertAll(buffer, x => x.ToString()));
  797. if (_enableLog)
  798. LOG.Info($"Read Success,Data:{ str} ({ result.Content.ToString()}:{dat1}、{dat2})");
  799. return buffer;
  800. }
  801. else
  802. {
  803. IsCommunicationError = true;
  804. EV.PostAlarmLog("System.PLC", $"[{address}] Read Failed {Environment.NewLine}Reason:{result.ToMessageShowString()}");
  805. }
  806. return buffer;
  807. }
  808. public void WriteResultRender(OperateResult result, string address, int data, string str)
  809. {
  810. if (result.IsSuccess)
  811. {
  812. _connecteTimes = 0;
  813. IsCommunicationError = false;
  814. if (_enableLog)
  815. LOG.Info("Write Success,Data" + str + "(" + data.ToString() + ")");
  816. }
  817. else
  818. {
  819. IsCommunicationError = true;
  820. EV.PostAlarmLog("System.PLC", $"[{address}] Write Failed {Environment.NewLine}Reason:{result.ToMessageShowString()}");
  821. }
  822. }
  823. public bool InvokeReset(string arg1, object[] arg2)
  824. {
  825. _connecteTimes = 0;
  826. IsCommunicationError = false;
  827. _trigRetryConnect.RST = true;
  828. return true;
  829. }
  830. protected override void WriteDo(int offset, bool[] buffer)
  831. {
  832. if (_readerClient != null)
  833. {
  834. return;
  835. int address = offset + _doStartAddress;
  836. List<short> temp = new List<short>();
  837. for (int i = 0; i < buffer.Length; i++)
  838. {
  839. temp.Add((short)(buffer[i] ? 1 : 0));
  840. }
  841. lock (_locker)
  842. {
  843. var ret = _readerClient.Write(address.ToString(), temp.ToArray());
  844. }
  845. }
  846. else
  847. {
  848. int address = offset + _doStartAddress;
  849. _wcfPlc.WriteBool(address.ToString(), buffer, out string reason);
  850. }
  851. }
  852. protected override void WriteAo(int offset, short[] buffer)
  853. {
  854. if (_readerClient != null)
  855. {
  856. int address = offset + _aoStartAddress;
  857. int size = 1;
  858. short[] temp;
  859. int i = 0;
  860. for (; i < buffer.Length / size; i++)
  861. {
  862. temp = new short[size];
  863. Array.Copy(buffer, i * size, temp, 0, size);
  864. OperateResult ret;
  865. lock (_locker)
  866. {
  867. ret = _readerClient.Write((address + i * size).ToString(), temp);
  868. }
  869. if (ret != null && !ret.IsSuccess)
  870. {
  871. return;
  872. }
  873. }
  874. if (buffer.Length - i * size > 0)
  875. {
  876. temp = new short[buffer.Length - i * size];
  877. Array.Copy(buffer, i * size, temp, 0, buffer.Length - i * size);
  878. OperateResult ret;
  879. lock (_locker)
  880. {
  881. ret = _readerClient.Write((address + i * size).ToString(), temp);
  882. }
  883. if (ret != null && !ret.IsSuccess)
  884. {
  885. return;
  886. }
  887. }
  888. }
  889. else
  890. {
  891. int address = offset + _aoStartAddress;
  892. _wcfPlc?.WriteInt16(address.ToString(), buffer, out _);
  893. }
  894. }
  895. protected override void WriteAoFloat(int offset, float[] data)
  896. {
  897. if (_readerClient != null)
  898. {
  899. return;
  900. int address = offset + _aoStartAddress;
  901. int size = 2;
  902. float[] temp;
  903. int i = 0;
  904. for (; i < data.Length / size; i++)
  905. {
  906. temp = new float[size];
  907. Array.Copy(data, i * size, temp, 0, size);
  908. OperateResult ret;
  909. lock (_locker)
  910. {
  911. ret = _readerClient.Write((address + i * size).ToString(), temp);
  912. }
  913. if (ret != null && !ret.IsSuccess)
  914. {
  915. return;
  916. }
  917. }
  918. if (data.Length - i * size > 0)
  919. {
  920. temp = new float[data.Length - i * size];
  921. Array.Copy(data, i * size, temp, 0, data.Length - i * size);
  922. OperateResult ret;
  923. lock (_locker)
  924. {
  925. ret = _readerClient.Write((address + i * size).ToString(), temp);
  926. }
  927. if (ret != null && !ret.IsSuccess)
  928. {
  929. return;
  930. }
  931. }
  932. }
  933. else
  934. {
  935. int address = offset + _aoStartAddress;
  936. _wcfPlc?.WriteFloat(address.ToString(), data, out _);
  937. }
  938. }
  939. }
  940. }