FinsPlcAdapter.cs 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800
  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.Profinet.Omron;
  10. using MECF.Framework.Common.Communications;
  11. using MECF.Framework.Common.PLC;
  12. using MECF.Framework.RT.Core.IoProviders;
  13. using MECF.Framework.RT.Core.IoProviders.Mitsubishis;
  14. using System;
  15. using System.Collections;
  16. using System.Collections.Generic;
  17. using System.Diagnostics;
  18. using System.Linq;
  19. using System.Net;
  20. using System.Net.Sockets;
  21. using System.Text;
  22. using System.Threading;
  23. using System.Threading.Tasks;
  24. using System.Xml;
  25. namespace FurnaceRT.Equipments.Systems
  26. {
  27. public class FinsPlcAdapter : IoProvider, IConnection
  28. {
  29. public string Address
  30. {
  31. get { return $"{_ip}:{_port}"; }
  32. }
  33. public bool IsConnected
  34. {
  35. get { return IsOpened; }
  36. }
  37. public bool Connect()
  38. {
  39. return true;
  40. }
  41. public bool Disconnect()
  42. {
  43. return true;
  44. }
  45. private int _heartBeat;
  46. public int HeartBeat
  47. {
  48. get
  49. {
  50. {
  51. if (_heartBeat > 65535)
  52. {
  53. _heartBeat = 0;
  54. }
  55. return _heartBeat++;
  56. }
  57. }
  58. }
  59. //private string _localIp = "127.0.0.1";
  60. //private int _socketId = 101;
  61. //private int _stationId = 102;
  62. private IPlc _plc = null;
  63. //private bool _isOpened = false;
  64. private string _ip = "192.168.10.10";
  65. private int _port = 9600;
  66. private int _aoBlockStartPosition = 1000;
  67. private int _aiBlockStartPosition = 2000;
  68. private int _doBlockStartPosition = 0;
  69. private int _diBlockStartPosition = 20;
  70. private string _diBlockType = "W";
  71. private string _doBlockType = "W";
  72. private string _aiBlockType = "D";
  73. private string _aoBlockType = "D";
  74. R_TRIG _failedTrigger = new R_TRIG();
  75. private Stopwatch stopwatchDi = new Stopwatch();
  76. private Stopwatch stopwatchDo = new Stopwatch();
  77. private Stopwatch stopwatchAi = new Stopwatch();
  78. private Stopwatch stopwatchAo = new Stopwatch();
  79. //private int comunicationSpanDi;
  80. //private int comunicationSpanDo;
  81. //private int comunicationSpanAi;
  82. //private int comunicationSpanAo;
  83. protected int comunicationSpanTotal;
  84. private Stopwatch stopwatchTotal = new Stopwatch();
  85. private R_TRIG _trigConnected = new R_TRIG();
  86. private Dictionary<string, R_TRIG> _logTrigDic = new Dictionary<string, R_TRIG>();
  87. private int plcCollectionInterval;
  88. private string _ioModule;
  89. public override void Initialize(string module, string name, List<IoBlockItem> lstBuffers, IIoBuffer buffer, XmlElement nodeParameter, string ioMappingPathFile, string ioModule)
  90. {
  91. Module = module;
  92. Name = name;
  93. _source = module + "." + name;
  94. _buffer = buffer;
  95. _ioModule = ioModule;
  96. _nodeParameter = nodeParameter;
  97. _blockSections = lstBuffers;
  98. buffer.SetBufferBlock(_source, lstBuffers);
  99. buffer.SetIoMapByModule(_source, 0, ioMappingPathFile, ioModule);
  100. SetParameter(nodeParameter);
  101. State = IoProviderStateEnum.Uninitialized;
  102. plcCollectionInterval = SC.ContainsItem("System.PlcCollectionInterval") ? SC.GetValue<int>("System.PlcCollectionInterval") : 50;
  103. _thread = new PeriodicJob(plcCollectionInterval, OnTimer, name);
  104. ConnectionManager.Instance.Subscribe(Name, this);
  105. DATA.Subscribe($"{Module}.{Name}.IsConnected", () => _plc == null ? false : _plc.CheckIsConnected());
  106. DATA.Subscribe($"{Module}.{Name}.Address", () => $"{_ip}:{_port}");
  107. DATA.Subscribe($"{Module}.{Name}.ComunicationSpanTotal", () => comunicationSpanTotal);
  108. OP.Subscribe($"{Module}.{Name}.Reconnect", (string cmd, object[] args) =>
  109. {
  110. Close();
  111. Open();
  112. return true;
  113. });
  114. //if (SC.GetValue<bool>("System.IsSimulatorMode"))
  115. //{
  116. // Open();
  117. //}
  118. }
  119. protected override bool OnTimer()
  120. {
  121. if (_plc == null)
  122. {
  123. Open();
  124. }
  125. if (_plc == null || !_plc.CheckIsConnected())
  126. return true;
  127. _trigConnected.CLK = _plc.CheckIsConnected();
  128. try
  129. {
  130. stopwatchTotal.Start();
  131. foreach (var bufferSection in _blockSections)
  132. {
  133. if (bufferSection.Type == IoType.DI)
  134. {
  135. stopwatchDi.Start();
  136. bool[] diBuffer = ReadDi(bufferSection.Offset, bufferSection.Size);
  137. if (diBuffer != null)
  138. {
  139. _buffer.SetDiBuffer(_source, bufferSection.Offset, diBuffer);
  140. }
  141. stopwatchDi.Stop();
  142. }
  143. else if (bufferSection.Type == IoType.AI)
  144. {
  145. stopwatchAi.Start();
  146. if (bufferSection.AIOType == typeof(float))
  147. {
  148. float[] aiBuffer = ReadAiFloat(bufferSection.Offset, bufferSection.Size);
  149. if (aiBuffer != null)
  150. {
  151. _buffer.SetAiBufferFloat(_source, bufferSection.Offset, aiBuffer);
  152. }
  153. }
  154. else
  155. {
  156. short[] aiBuffer = ReadAi(bufferSection.Offset, bufferSection.Size);
  157. if (aiBuffer != null)
  158. {
  159. _buffer.SetAiBuffer(_source, bufferSection.Offset, aiBuffer);
  160. }
  161. }
  162. stopwatchAi.Stop();
  163. }
  164. else if (bufferSection.Type == IoType.DO)
  165. {
  166. if (_trigConnected.Q)
  167. {
  168. bool[] doBuffer = ReadDo(bufferSection.Offset, bufferSection.Size);
  169. if (doBuffer != null)
  170. {
  171. _buffer.SetDoBuffer(_source, bufferSection.Offset, doBuffer);
  172. }
  173. }
  174. //if (SC.GetValue<bool>("System.IsSimulatorMode"))
  175. {
  176. Dictionary<int, bool[]> dos = _buffer.GetDoBuffer(_source);
  177. if (dos != null)
  178. {
  179. foreach (var doo in dos)
  180. {
  181. WriteDo(bufferSection.Offset, doo.Value);
  182. }
  183. }
  184. continue;
  185. }
  186. }
  187. else if (bufferSection.Type == IoType.AO)
  188. {
  189. if (bufferSection.AIOType == typeof(float))
  190. {
  191. if (_trigConnected.Q)
  192. {
  193. float[] aoBuffer = ReadAoFloat(bufferSection.Offset, bufferSection.Size);
  194. if (aoBuffer != null)
  195. {
  196. _buffer.SetAoBufferFloat(_source, bufferSection.Offset, aoBuffer);
  197. }
  198. }
  199. //if (SC.GetValue<bool>("System.IsSimulatorMode"))
  200. {
  201. Dictionary<int, float[]> aos = _buffer.GetAoBufferFloat(_source);
  202. if (aos != null)
  203. {
  204. foreach (var ao in aos)
  205. {
  206. WriteAoFloat(bufferSection.Offset, ao.Value);
  207. }
  208. }
  209. continue;
  210. }
  211. }
  212. else
  213. {
  214. if (_trigConnected.Q)
  215. {
  216. short[] aoBuffer = ReadAo(bufferSection.Offset, bufferSection.Size);
  217. if (aoBuffer != null)
  218. {
  219. _buffer.SetAoBuffer(_source, bufferSection.Offset, aoBuffer);
  220. }
  221. }
  222. //if (SC.GetValue<bool>("System.IsSimulatorMode"))
  223. {
  224. Dictionary<int, short[]> aos = _buffer.GetAoBuffer(_source);
  225. if (aos != null)
  226. {
  227. foreach (var ao in aos)
  228. {
  229. WriteAo(bufferSection.Offset, ao.Value);
  230. }
  231. }
  232. continue;
  233. }
  234. }
  235. }
  236. }
  237. //comunicationSpanDi = (int)stopwatchDi.ElapsedMilliseconds;
  238. stopwatchDi.Reset();
  239. //comunicationSpanAi = (int)stopwatchAi.ElapsedMilliseconds;
  240. stopwatchAi.Reset();
  241. comunicationSpanTotal = (int)stopwatchTotal.ElapsedMilliseconds;
  242. stopwatchTotal.Restart();
  243. }
  244. catch (Exception ex)
  245. {
  246. LOG.Error($"{Name} {ex}");
  247. //SetState(IoProviderStateEnum.Error);
  248. Thread.Sleep(1000);
  249. Close();
  250. Open();
  251. }
  252. _trigError.CLK = State == IoProviderStateEnum.Error;
  253. if (_trigError.Q)
  254. {
  255. EV.PostAlarmLog(Module, $"{_source} PLC {_ip}:{_port} error");
  256. }
  257. _trigNotConnected.CLK = State != IoProviderStateEnum.Opened;
  258. if (_trigNotConnected.T)
  259. {
  260. EV.PostInfoLog(Module, $"{_source} connected");
  261. }
  262. if (_trigNotConnected.R)
  263. {
  264. EV.PostAlarmLog(Module, $"{_source} PLC {_ip}:{_port} not connected");
  265. }
  266. return true;
  267. }
  268. protected override void SetParameter(XmlElement nodeParameter)
  269. {
  270. string strIp = nodeParameter.GetAttribute("ip");
  271. string strPort = nodeParameter.GetAttribute("port");
  272. string diBlockType = nodeParameter.GetAttribute("diStoragename");
  273. string doBlockType = nodeParameter.GetAttribute("doStoragename");
  274. string aiBlockType = nodeParameter.GetAttribute("aiStoragename");
  275. string aoBlockType = nodeParameter.GetAttribute("aoStoragename");
  276. string diStartPosition = nodeParameter.GetAttribute("diStartAddress");
  277. string doStartPosition = nodeParameter.GetAttribute("doStartAddress");
  278. string aiStartPosition = nodeParameter.GetAttribute("aiStartAddress");
  279. string aoStartPosition = nodeParameter.GetAttribute("aoStartAddress");
  280. _port = int.Parse(strPort);
  281. _ip = strIp;
  282. _diBlockType = nodeParameter.GetAttribute("diStoragename").Substring(0, 1);
  283. _doBlockType = nodeParameter.GetAttribute("doStoragename").Substring(0, 1);
  284. _aiBlockType = nodeParameter.GetAttribute("aiStoragename").Substring(0, 1);
  285. _aoBlockType = nodeParameter.GetAttribute("aoStoragename").Substring(0, 1);
  286. if (!int.TryParse(diStartPosition, out _diBlockStartPosition))
  287. {
  288. LOG.Error($"plc config error, start position {diStartPosition} not valid");
  289. }
  290. if (!int.TryParse(doStartPosition, out _doBlockStartPosition))
  291. {
  292. LOG.Error($"plc config error, start position {doStartPosition} not valid");
  293. }
  294. if (!int.TryParse(aiStartPosition, out _aiBlockStartPosition))
  295. {
  296. LOG.Error($"plc config error, start position {aiStartPosition} not valid");
  297. }
  298. if (!int.TryParse(aoStartPosition, out _aoBlockStartPosition))
  299. {
  300. LOG.Error($"plc config error, start position {aoStartPosition} not valid");
  301. }
  302. }
  303. public override void Reset()
  304. {
  305. _trigError.RST = true;
  306. _trigNotConnected.RST = true;
  307. foreach (var trig in _logTrigDic.Values)
  308. {
  309. if (trig != null)
  310. trig.RST = true;
  311. }
  312. if (!_plc.CheckIsConnected())
  313. {
  314. Close();
  315. Open();
  316. }
  317. }
  318. protected override void Open()
  319. {
  320. if (SC.GetValue<bool>("System.IsSimulatorMode"))
  321. {
  322. _plc = new WcfPlc(Module, Name, $"WcfPlc_{Name}");
  323. _plc.Initialize();
  324. }
  325. else
  326. {
  327. _plc = new FinsPlc(_ip, _port);
  328. }
  329. try
  330. {
  331. OperateResult connect = _plc.Connect() ? OperateResult.CreateSuccessResult() : new OperateResult();
  332. if (connect.IsSuccess)
  333. {
  334. SetState(IoProviderStateEnum.Opened);
  335. EV.PostInfoLog(Module, $"{Name} plc connect success");
  336. }
  337. else
  338. {
  339. EV.PostInfoLog(Module, $"{Name} plc connect fail");
  340. }
  341. }
  342. catch (Exception ex)
  343. {
  344. LOG.Error(ex.Message);
  345. }
  346. }
  347. protected override void Close()
  348. {
  349. _plc.Disconnect();
  350. SetState(IoProviderStateEnum.Closed);
  351. }
  352. protected override bool[] ReadDi(int offset, int size)
  353. {
  354. bool[] buff = DoReadDi(offset, (ushort)size);
  355. return buff;
  356. }
  357. protected bool[] ReadDo(int offset, int size)
  358. {
  359. bool[] buff = DoReadDo(offset, (ushort)size);
  360. return buff;
  361. }
  362. protected override short[] ReadAi(int offset, int size)
  363. {
  364. short[] buff = DoReadAi(offset, (ushort)size);
  365. return buff;
  366. }
  367. protected override float[] ReadAiFloat(int offset, int size)
  368. {
  369. float[] buff = DoReadAiFloat(offset, (ushort)size);
  370. return buff;
  371. }
  372. protected float[] ReadAoFloat(int offset, int size)
  373. {
  374. float[] buff = DoReadAoFloat(offset, (ushort)size);
  375. return buff;
  376. }
  377. protected short[] ReadAo(int offset, int size)
  378. {
  379. short[] buff = DoReadAo(offset, (ushort)size);
  380. return buff;
  381. }
  382. protected override void WriteDo(int offset, bool[] data)
  383. {
  384. DoWriteDo(offset, data);
  385. }
  386. protected override void WriteAo(int offset, short[] data)
  387. {
  388. DoWriteAo(offset, data);
  389. }
  390. protected override void WriteAoFloat(int offset, float[] data)
  391. {
  392. DoWriteAoFloat(offset, data);
  393. }
  394. public override bool SetValue(AOAccessor aoItem, short value)
  395. {
  396. if (!SC.GetValue<bool>("System.IsSimulatorMode"))
  397. return true;
  398. if (State != IoProviderStateEnum.Opened)
  399. return false;
  400. return DoWriteAo(aoItem.Index, new[] { value });
  401. }
  402. public override bool SetValueFloat(AOAccessor aoItem, float value)
  403. {
  404. if (!SC.GetValue<bool>("System.IsSimulatorMode"))
  405. return true;
  406. if (State != IoProviderStateEnum.Opened)
  407. return false;
  408. return DoWriteAoFloat(aoItem.Index, new[] { value });
  409. }
  410. public override bool SetValue(DOAccessor doItem, bool value)
  411. {
  412. if (!SC.GetValue<bool>("System.IsSimulatorMode"))
  413. return true;
  414. var offset = doItem.Index / 16;
  415. var index = doItem.Index % 16;
  416. int data = 0;
  417. for (int i = 0; i < 16; i++)
  418. {
  419. if (i == index)
  420. data += (value ? (1 << i) : (0 << i));
  421. else
  422. data += (doItem.Buffer[offset * 16 + i] ? (1 << i) : (0 << i));
  423. }
  424. if (_plc.WriteInt16($"{_doBlockType}{_doBlockStartPosition + offset}", new[] { (short)data }, out string reason))
  425. {
  426. return true;
  427. }
  428. var key = "DoWriteAo";
  429. if (!_logTrigDic.ContainsKey(key))
  430. _logTrigDic.Add(key, new R_TRIG());
  431. _logTrigDic[key].CLK = true;
  432. if (_logTrigDic[key].Q)
  433. LOG.Write(reason);
  434. return false;
  435. }
  436. #region PLC read write
  437. private short[] DoReadAi(int offset, ushort size)
  438. {
  439. if (_plc.ReadInt16($"{_aiBlockType}{_aiBlockStartPosition + offset}", out short[] result, size, out string reason))
  440. {
  441. return result;
  442. }
  443. var key = "DoReadAi";
  444. if (!_logTrigDic.ContainsKey(key))
  445. _logTrigDic.Add(key, new R_TRIG());
  446. _logTrigDic[key].CLK = true;
  447. if (_logTrigDic[key].Q)
  448. LOG.Write(reason);
  449. return null;
  450. }
  451. private float[] DoReadAiFloat(int offset, ushort size)
  452. {
  453. if (_plc.ReadFloat($"{_aiBlockType}{_aiBlockStartPosition + offset}", out float[] result, size, out string reason))
  454. {
  455. return result;
  456. }
  457. var key = "DoReadAiFloat";
  458. if (!_logTrigDic.ContainsKey(key))
  459. _logTrigDic.Add(key, new R_TRIG());
  460. _logTrigDic[key].CLK = true;
  461. if (_logTrigDic[key].Q)
  462. LOG.Write(reason);
  463. return null;
  464. }
  465. private short[] DoReadAo(int offset, ushort size)
  466. {
  467. if (_plc.ReadInt16($"{_aoBlockType}{_aoBlockStartPosition + offset}", out short[] result, size, out string reason))
  468. {
  469. return result;
  470. }
  471. var key = "DoReadAo";
  472. if (!_logTrigDic.ContainsKey(key))
  473. _logTrigDic.Add(key, new R_TRIG());
  474. _logTrigDic[key].CLK = true;
  475. if (_logTrigDic[key].Q)
  476. LOG.Write(reason);
  477. return null;
  478. }
  479. private float[] DoReadAoFloat(int offset, ushort size)
  480. {
  481. if (_plc.ReadFloat($"{_aoBlockType}{_aoBlockStartPosition + offset}", out float[] result, size, out string reason))
  482. {
  483. return result;
  484. }
  485. var key = "DoReadAoFloat";
  486. if (!_logTrigDic.ContainsKey(key))
  487. _logTrigDic.Add(key, new R_TRIG());
  488. _logTrigDic[key].CLK = true;
  489. if (_logTrigDic[key].Q)
  490. LOG.Write(reason);
  491. return null;
  492. }
  493. private bool DoWriteAoFloat(int offset, float[] data, int total = 0, int count = 100)
  494. {
  495. var reason = "";
  496. if (SC.GetValue<bool>("System.IsSimulatorMode"))
  497. {
  498. if (_plc.WriteFloat($"{_aoBlockType}{_aoBlockStartPosition + offset}", data, out reason))
  499. {
  500. return true;
  501. }
  502. }
  503. int size = 450;
  504. float[] temp;
  505. int i = 0;
  506. for (; i< data.Length/ size; i++)
  507. {
  508. temp = new float[size];
  509. Array.Copy(data, i*size,temp,0,size);
  510. if (!_plc.WriteFloat($"{_aoBlockType}{_aoBlockStartPosition + i * size * 2}", temp, out reason))
  511. {
  512. return false;
  513. }
  514. }
  515. if(data.Length - i * size > 0)
  516. {
  517. temp = new float[data.Length - i * size];
  518. Array.Copy(data, i * size, temp, 0, data.Length - i * size);
  519. if (!_plc.WriteFloat($"{_aoBlockType}{_aoBlockStartPosition + i * size * 2}", temp, out reason))
  520. {
  521. return false;
  522. }
  523. }
  524. return true;
  525. //if (_plc.WriteFloat($"{_aoBlockType}{_aoBlockStartPosition + offset}", data, out reason))
  526. //{
  527. // return true;
  528. //}
  529. var key = "DoWriteAoFloat";
  530. if (!_logTrigDic.ContainsKey(key))
  531. _logTrigDic.Add(key, new R_TRIG());
  532. _logTrigDic[key].CLK = true;
  533. if (_logTrigDic[key].Q)
  534. LOG.Write(reason);
  535. return false;
  536. }
  537. private bool DoWriteAo(int offset, short[] data, int total = 0, int count = 100)
  538. {
  539. if (_plc.WriteInt16($"{_aoBlockType}{_aoBlockStartPosition + offset}", data, out string reason))
  540. {
  541. return true;
  542. }
  543. var key = "DoWriteAo";
  544. if (!_logTrigDic.ContainsKey(key))
  545. _logTrigDic.Add(key, new R_TRIG());
  546. _logTrigDic[key].CLK = true;
  547. if (_logTrigDic[key].Q)
  548. LOG.Write(reason);
  549. return false;
  550. }
  551. private bool[] DoReadDi(int offset, ushort size)
  552. {
  553. if (SC.GetValue<bool>("System.IsSimulatorMode"))
  554. {
  555. if (_plc.ReadBool($"{_diBlockType}{_diBlockStartPosition + (offset >> 4)}.{offset & 0x0f}", out bool[] result, size, out string reason))
  556. {
  557. return result;
  558. }
  559. var key = "DoReadDi";
  560. if (!_logTrigDic.ContainsKey(key))
  561. _logTrigDic.Add(key, new R_TRIG());
  562. _logTrigDic[key].CLK = true;
  563. if (_logTrigDic[key].Q)
  564. LOG.Write(reason);
  565. }
  566. else
  567. {
  568. var shortSize = size / 16;
  569. if (_plc.ReadInt16($"{_diBlockType}{_diBlockStartPosition + offset}", out short[] result, shortSize, out string reason))
  570. {
  571. bool[] boolResult = new bool[Math.Min(size, result.Length * 16)];
  572. for (int i = 0; i < shortSize && i < result.Length; i++)
  573. {
  574. var data = (ushort)result[i];
  575. for (int j = 0; j < 16; j++)
  576. {
  577. boolResult[i * 16 + j] = Converter.GetBit(data, j);
  578. }
  579. }
  580. return boolResult;
  581. }
  582. var key = "DoReadDi";
  583. if (!_logTrigDic.ContainsKey(key))
  584. _logTrigDic.Add(key, new R_TRIG());
  585. _logTrigDic[key].CLK = true;
  586. if (_logTrigDic[key].Q)
  587. LOG.Write(reason);
  588. }
  589. return null;
  590. }
  591. private bool[] DoReadDo(int offset, ushort size)
  592. {
  593. if (SC.GetValue<bool>("System.IsSimulatorMode"))
  594. {
  595. return null;
  596. //if (_plc.ReadBool($"{_doBlockType}{_doBlockStartPosition + (offset >> 4)}.{offset & 0x0f}", out bool[] result, size, out string reason))
  597. //{
  598. // return result;
  599. //}
  600. //var key = "DoReadDo";
  601. //if (!_logTrigDic.ContainsKey(key))
  602. // _logTrigDic.Add(key, new R_TRIG());
  603. //_logTrigDic[key].CLK = true;
  604. //if (_logTrigDic[key].Q)
  605. // LOG.Write(reason);
  606. }
  607. else
  608. {
  609. var shortSize = size / 16;
  610. if (_plc.ReadInt16($"{_doBlockType}{_doBlockStartPosition + offset}", out short[] result, shortSize, out string reason))
  611. {
  612. bool[] boolResult = new bool[Math.Min(size, result.Length * 16)];
  613. for (int i = 0; i < shortSize && i < result.Length; i++)
  614. {
  615. var data = (ushort)result[i];
  616. for (int j = 0; j < 16; j++)
  617. {
  618. boolResult[i * 16 + j] = Converter.GetBit(data, j);
  619. }
  620. }
  621. return boolResult;
  622. }
  623. var key = "DoReadDo";
  624. if (!_logTrigDic.ContainsKey(key))
  625. _logTrigDic.Add(key, new R_TRIG());
  626. _logTrigDic[key].CLK = true;
  627. if (_logTrigDic[key].Q)
  628. LOG.Write(reason);
  629. }
  630. return null;
  631. }
  632. private bool DoWriteDo(int offset, bool[] data)
  633. {
  634. if (SC.GetValue<bool>("System.IsSimulatorMode"))
  635. {
  636. if (_plc.WriteBool($"{_doBlockType}{_doBlockStartPosition + (offset >> 4)}.{offset & 0x0f}", data, out string reason))
  637. {
  638. return true;
  639. }
  640. var key = "DoWriteDo";
  641. if (!_logTrigDic.ContainsKey(key))
  642. _logTrigDic.Add(key, new R_TRIG());
  643. _logTrigDic[key].CLK = true;
  644. if (_logTrigDic[key].Q)
  645. LOG.Write(reason);
  646. }
  647. else
  648. {
  649. short[] shortValue = new short[data.Length / 16];
  650. for (int i = 0; i < shortValue.Length; i++)
  651. {
  652. short value = shortValue[i];
  653. for (int j = 0; j < 16; j++)
  654. {
  655. value = (short)Converter.SetBit((ushort)value, j, data[i * 16 + j]);
  656. }
  657. shortValue[i] = value;
  658. }
  659. if (_plc.WriteInt16($"{_doBlockType}{_doBlockStartPosition + offset}", shortValue, out string reason))
  660. {
  661. return true;
  662. }
  663. var key = "DoWriteDo";
  664. if (!_logTrigDic.ContainsKey(key))
  665. _logTrigDic.Add(key, new R_TRIG());
  666. _logTrigDic[key].CLK = true;
  667. if (_logTrigDic[key].Q)
  668. LOG.Write(reason);
  669. }
  670. return false;
  671. }
  672. #endregion
  673. }
  674. }