WagoSocketSimulator.cs 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693
  1. using Aitex.Common.Util;
  2. using Aitex.Core.RT.Device;
  3. using Aitex.Core.RT.Log;
  4. using Aitex.Core.Util;
  5. using MECF.Framework.Common.Device.Festo;
  6. using MECF.Framework.Common.Device.Wago;
  7. using MECF.Framework.Common.Net;
  8. using MECF.Framework.Common.Simulator;
  9. using MECF.Framework.Simulator.Core.Driver;
  10. using System;
  11. using System.Collections.Generic;
  12. using System.IO;
  13. namespace CyberX8_Simulator.Devices
  14. {
  15. public class WagoSocketSimulator : SocketDeviceSimulator
  16. {
  17. private const short WRITE_DO_STARTADDRESS = 0x0200;
  18. private const short WRITE_AO_STARTADDRESS = 0x0200;
  19. //键是名字,值是对应数据所在的位置 注意:要和WagoControlCfg里面的地址顺序对上
  20. public Dictionary<string, int> DONameIndexDic;
  21. public Dictionary<string, int> DINameIndexDic;
  22. public Dictionary<string, int> AINameIndexDic;
  23. public Dictionary<string, int> AONameIndexDic;
  24. private IByteTransform byteTransform = new BigEndianByteTransformBase();
  25. //存储模拟器数据的数组
  26. public byte[] DOBytes = new byte[200];
  27. public short[] AOShorts = new short[200];
  28. public byte[] DIBytes = new byte[200];
  29. public short[] AIShorts = new short[200];
  30. /// <summary>
  31. /// 写DO锁
  32. /// </summary>
  33. private object _writeDOLocker = new object();
  34. /// <summary>
  35. /// 写AO锁
  36. /// </summary>
  37. private object _writeAOLocker = new object();
  38. /// <summary>
  39. /// Festo Data Buffer
  40. /// </summary>
  41. private Dictionary<string, bool> _festoDataBuffer = new Dictionary<string, bool>();
  42. /// <summary>
  43. /// 定时器
  44. /// </summary>
  45. private PeriodicJob _LoaderPeriodicJob;
  46. private PeriodicJob _Wago2PeriodicJob;
  47. private bool _rinse1FillValve = false;
  48. private bool _rinse1DumpValve = false;
  49. private bool _rinse2FillValve = false;
  50. private bool _rinse2DumpValve = false;
  51. private bool _rinse3FillValve = false;
  52. private bool _rinse3DumpValve = false;
  53. private bool _rinse4FillValve = false;
  54. private bool _rinse4DumpValve = false;
  55. //delegate
  56. #region Delegate
  57. public delegate void VariableValueChanged(object obj);
  58. #endregion
  59. #region 事件
  60. /// <summary>
  61. /// 变量变更事件
  62. /// </summary>
  63. public event VariableValueChanged OnDIVariableValueChanged;
  64. public event VariableValueChanged OnAIVariableValueChanged;
  65. public event VariableValueChanged OnDOVariableValueChanged;
  66. public event VariableValueChanged OnAOVariableValueChanged;
  67. #endregion
  68. public WagoSocketSimulator(int port):base(port)
  69. {
  70. SimulatorCommManager.Instance.OnUpdateVariableValueChanged += UpdataDataCausedByOtherModule;
  71. MotorSimulator.Instance.OnUpdateWagoDatasChanged += UpdataDataCausedByOtherModule;
  72. InitializeData(port);
  73. }
  74. private void UpdataDataCausedByOtherModule(string sourceName,string name, bool value, bool invert)
  75. {
  76. value = invert ? !value : value;
  77. //AI Data
  78. if (AINameIndexDic.ContainsKey(name))
  79. {
  80. switch (name)
  81. {
  82. case "r_LoaderA_LS_Vacuum_anlg":
  83. case "r_LoaderB_LS_Vacuum_anlg":
  84. AIShorts[AINameIndexDic[name]] = value ? (short)0x32C8 : (short)0x2AF8;
  85. break;
  86. case "r_DPUF_A_CHUCK_A_VAC":
  87. case "r_DPUF_A_CHUCK_B_VAC":
  88. AIShorts[AINameIndexDic[name]] = value ? (short)0x0C80 : (short)0x32C8;
  89. break;
  90. case "r_LOADERA_BERNOULLI_PRESSURE":
  91. case "r_LOADERB_BERNOULLI_PRESSURE":
  92. case "r_LOADERA_CHUCK_BLADDER":
  93. case "r_LOADERB_CHUCK_BLADDER":
  94. case "r_LOADERA_WS_BLADDER_PRESSURE":
  95. case "r_LOADERB_WS_BLADDER_PRESSURE":
  96. AIShorts[AINameIndexDic[name]] = value ? (short)0x2AF8 : (short)0x00;
  97. break;
  98. default:
  99. break;
  100. }
  101. }
  102. if (!string.IsNullOrEmpty(sourceName))
  103. {
  104. switch (sourceName)
  105. {
  106. case "c_QDRD1_DI_FILL":
  107. _rinse1FillValve = value;
  108. break;
  109. case "c_QDRD1_DUMP":
  110. _rinse1DumpValve = value;
  111. break;
  112. case "c_QDRD2_DI_FILL":
  113. _rinse2FillValve = value;
  114. break;
  115. case "c_QDRD2_DUMP":
  116. _rinse2DumpValve = value;
  117. break;
  118. case "c_QDRD3_DI_FILL":
  119. _rinse3FillValve = value;
  120. break;
  121. case "c_QDRD3_DUMP":
  122. _rinse3DumpValve = value;
  123. break;
  124. case "c_QDRD4_DI_FILL":
  125. _rinse4FillValve = value;
  126. break;
  127. case "c_QDRD4_DUMP":
  128. _rinse4DumpValve = value;
  129. break;
  130. default:
  131. break;
  132. }
  133. }
  134. //DI Data
  135. UpdataDIBytes(name, value ? 1 : 0);
  136. //Festo Data
  137. if (name == "FlowTestClamp") _festoDataBuffer[name] = value;
  138. }
  139. /// <summary>
  140. /// 触发Wago对应数据更新
  141. /// </summary>
  142. /// <param name="position"></param>
  143. /// <param name="value"></param>
  144. private void UpdateDataCausedByWago(int position, bool value)
  145. {
  146. if (DONameIndexDic.ContainsKey("c_PUF_CHUCK") && position == DONameIndexDic["c_PUF_CHUCK"])
  147. {
  148. UpdataDIBytes("r_PUF_A_CHUCK_OUT", value ? 1 : 0);
  149. UpdataDIBytes("r_PUF_B_CHUCK_OUT", value ? 1 : 0);
  150. UpdataDIBytes("r_PUF_A_CHUCK_IN", !value ? 1 : 0);
  151. UpdataDIBytes("r_PUF_B_CHUCK_IN", !value ? 1 : 0);
  152. }
  153. if (DONameIndexDic.ContainsKey("c_HVD_1_HIGH") && position == DONameIndexDic["c_HVD_1_HIGH"])
  154. {
  155. UpdataAIShorts("r_HVD_1_ANALOG", value ? 4500 : 0);
  156. }
  157. if (DONameIndexDic.ContainsKey("c_HVD_2_HIGH") && position == DONameIndexDic["c_HVD_2_HIGH"])
  158. {
  159. UpdataAIShorts("r_HVD_2_ANALOG", value ? 4500 : 0);
  160. }
  161. }
  162. /// <summary>
  163. /// 初始化字典
  164. /// </summary>
  165. private void InitializeData(int port) //端口用于初始化不同Wago设备的字典
  166. {
  167. //加载对应配置文件 WagoControllerCfg-Simulator.xml
  168. try
  169. {
  170. string oldXmlPath = PathManager.GetCfgDir();
  171. string newXmlPath = oldXmlPath.Replace("CyberX8_Simulator", "CyberX8_RT") + "Devices\\WagoControllerCfg-Simulator.xml";
  172. WagoControllerCfg cfg = CustomXmlSerializer.Deserialize<WagoControllerCfg>(new FileInfo(newXmlPath));
  173. if (cfg != null)
  174. {
  175. foreach (WagoDeviceConfig config in cfg.WagoDeviceConfigs)
  176. {
  177. if (port == config.Port)
  178. {
  179. if(config.Module == "Loader" && _LoaderPeriodicJob == null)
  180. {
  181. _LoaderPeriodicJob = new PeriodicJob(100, OnTimer, $"Wago {config.Module} timer",true);
  182. }
  183. if (config.Module == "Wago2" && _Wago2PeriodicJob == null)
  184. {
  185. _Wago2PeriodicJob = new PeriodicJob(100, OnTimer1, $"Wago {config.Module} timer", true);
  186. }
  187. //加载DO
  188. int i = 0;
  189. DONameIndexDic = new Dictionary<string, int>();
  190. if(config.WagoDigOut != null && config.WagoDigOut.WagoDOGroups != null)
  191. {
  192. foreach (var group in config.WagoDigOut.WagoDOGroups)
  193. {
  194. foreach (var item in group.WagoDOs)
  195. {
  196. DONameIndexDic[item.Name] = i;
  197. i++;
  198. }
  199. }
  200. }
  201. //加载DI
  202. i = 0;
  203. DINameIndexDic = new Dictionary<string, int>();
  204. if(config.WagoDigIn != null && config.WagoDigIn.WagoDIGroups != null)
  205. {
  206. foreach (var group in config.WagoDigIn.WagoDIGroups)
  207. {
  208. foreach (var item in group.WagoDIs)
  209. {
  210. DINameIndexDic[item.Name] = i;
  211. i++;
  212. }
  213. }
  214. }
  215. //加载AO
  216. i = 0;
  217. AONameIndexDic = new Dictionary<string, int>();
  218. if (config.WagoAnoOut != null && config.WagoAnoOut.WagoAOGroups != null)
  219. {
  220. foreach (var group in config.WagoAnoOut.WagoAOGroups)
  221. {
  222. foreach (var item in group.WagoAOs)
  223. {
  224. AONameIndexDic[item.Name] = i;
  225. i++;
  226. }
  227. }
  228. }
  229. //加载AI
  230. i = 0;
  231. AINameIndexDic = new Dictionary<string, int>();
  232. if (config.WagoAnoIn != null && config.WagoAnoIn.WagoAIGroups != null)
  233. {
  234. foreach (var group in config.WagoAnoIn.WagoAIGroups)
  235. {
  236. foreach (var item in group.WagoAIs)
  237. {
  238. AINameIndexDic[item.Name] = i;
  239. i++;
  240. }
  241. }
  242. }
  243. }
  244. }
  245. }
  246. }
  247. catch
  248. {
  249. LOG.WriteLog(eEvent.ERR_WAGO, "Wago", "Load wago WagoControllerCfg-Simulator.xml failed");
  250. }
  251. //设置IO变量默认值
  252. if (AINameIndexDic.ContainsKey("r_LoaderA_LS_Vacuum_anlg")) AIShorts[AINameIndexDic["r_LoaderA_LS_Vacuum_anlg"]] = 0x32C8;
  253. if (AINameIndexDic.ContainsKey("r_LoaderB_LS_Vacuum_anlg")) AIShorts[AINameIndexDic["r_LoaderB_LS_Vacuum_anlg"]] = 0x32C8;
  254. if (AINameIndexDic.ContainsKey("r_DPUF_A_CHUCK_A_VAC")) AIShorts[AINameIndexDic["r_DPUF_A_CHUCK_A_VAC"]] = 0x32C8;
  255. if (AINameIndexDic.ContainsKey("r_DPUF_A_CHUCK_B_VAC")) AIShorts[AINameIndexDic["r_DPUF_A_CHUCK_B_VAC"]] = 0x32C8;
  256. if (AINameIndexDic.ContainsKey("r_SYSTEM_EXHAUST")) AIShorts[AINameIndexDic["r_SYSTEM_EXHAUST"]] = 0x3A98;
  257. if (DONameIndexDic.ContainsKey("c_HVD_1_ENABLE")) DOBytes[DONameIndexDic["c_HVD_1_ENABLE"]] = 1;
  258. if (DONameIndexDic.ContainsKey("c_HVD_2_ENABLE")) DOBytes[DONameIndexDic["c_HVD_2_ENABLE"]] = 1;
  259. if (AINameIndexDic.ContainsKey("r_QDRD1_WATER_LEVEL")) AIShorts[AINameIndexDic["r_QDRD1_WATER_LEVEL"]] = 4000;
  260. if (AINameIndexDic.ContainsKey("r_QDRD2_WATER_LEVEL")) AIShorts[AINameIndexDic["r_QDRD2_WATER_LEVEL"]] = 4000;
  261. if (AINameIndexDic.ContainsKey("r_QDRD3_WATER_LEVEL")) AIShorts[AINameIndexDic["r_QDRD3_WATER_LEVEL"]] = 4000;
  262. if (AINameIndexDic.ContainsKey("r_QDRD4_WATER_LEVEL")) AIShorts[AINameIndexDic["r_QDRD4_WATER_LEVEL"]] = 4000;
  263. }
  264. #region 公共方法
  265. public void UpdataDOBytes(string name,int value)
  266. {
  267. if (OnDOVariableValueChanged != null)
  268. {
  269. OnDOVariableValueChanged(name);
  270. }
  271. if (DONameIndexDic.ContainsKey(name))
  272. {
  273. if (DONameIndexDic[name] < DOBytes.Length)
  274. {
  275. DOBytes[DONameIndexDic[name]] = value == 0 ? (byte)0 : (byte)1;
  276. }
  277. }
  278. }
  279. public void UpdataDIBytes(string name, int value)
  280. {
  281. if (OnDIVariableValueChanged != null)
  282. {
  283. OnDIVariableValueChanged(name);
  284. }
  285. if (DINameIndexDic.ContainsKey(name))
  286. {
  287. if (DINameIndexDic[name] < DIBytes.Length)
  288. {
  289. DIBytes[DINameIndexDic[name]] = value == 0 ? (byte)0 : (byte)1;
  290. }
  291. }
  292. }
  293. public void UpdataAOShorts(string name, int value)
  294. {
  295. if (OnAOVariableValueChanged != null)
  296. {
  297. OnAOVariableValueChanged(name);
  298. }
  299. if (AONameIndexDic.ContainsKey(name))
  300. {
  301. string hexValue = value.ToString("X2");
  302. try
  303. {
  304. short result = Convert.ToInt16(hexValue, 16);
  305. if (AONameIndexDic[name] < AOShorts.Length)
  306. {
  307. AOShorts[AONameIndexDic[name]] = result;
  308. }
  309. }
  310. catch (FormatException)
  311. {
  312. }
  313. }
  314. }
  315. public void UpdataAIShorts(string name, int value)
  316. {
  317. if (OnAIVariableValueChanged != null)
  318. {
  319. OnAIVariableValueChanged(name);
  320. }
  321. if (AINameIndexDic.ContainsKey(name))
  322. {
  323. string hexValue = value.ToString("X2");
  324. try
  325. {
  326. short result = Convert.ToInt16(hexValue, 16);
  327. if(AINameIndexDic[name] < AIShorts.Length)
  328. {
  329. AIShorts[AINameIndexDic[name]] = result;
  330. }
  331. }
  332. catch (FormatException)
  333. {
  334. }
  335. }
  336. }
  337. #endregion
  338. #region 功能方法
  339. /// <summary>
  340. /// 将长度为8的二进制byte数组转成对应十六进制byte值(大端模式)
  341. /// </summary>
  342. /// <param name="byteArray"></param>
  343. /// <returns></returns>
  344. public byte ConvertByteArrayToHex(byte[] byteArray)
  345. {
  346. byte result = 0;
  347. // 先将 byte 数组转换为二进制数
  348. int binaryValue = 0;
  349. for (int i = 0; i < 8; i++)
  350. {
  351. binaryValue |= (byteArray[i] << (7 - i));
  352. }
  353. // 逆转二进制数
  354. int reversedValue = 0;
  355. for (int i = 0; i < 8; i++)
  356. {
  357. reversedValue |= ((binaryValue >> i) & 1) << (7 - i);
  358. }
  359. // 转换为十六进制byte
  360. if (byte.TryParse(reversedValue.ToString("X2"), System.Globalization.NumberStyles.HexNumber, null, out result))
  361. {
  362. return result;
  363. }
  364. return 0;
  365. }
  366. /// <summary>
  367. /// 将short数组转成长度两倍的byte数组
  368. /// </summary>
  369. /// <param name="shortArray"></param>
  370. /// <returns></returns>
  371. private byte[] ConvertShortArrayToByteArray(short[] shortArray)
  372. {
  373. byte[] byteArray = new byte[shortArray.Length * 2];
  374. for (int i = 0; i < shortArray.Length; i++)
  375. {
  376. byte[] tempBytes = BitConverter.GetBytes(shortArray[i]);
  377. Array.Reverse(tempBytes);
  378. Array.Copy(tempBytes, 0, byteArray, i * 2, 2);
  379. }
  380. return byteArray;
  381. }
  382. #endregion
  383. protected override void ProcessUnsplitMessage(byte[] data)
  384. {
  385. byte command = data[7];
  386. if (command == 0x01) //读DO
  387. {
  388. short flag = byteTransform.TransInt16(data, 0);
  389. byte channel = data[6];
  390. short startAddress = byteTransform.TransInt16(data, 8);
  391. short bitCount = byteTransform.TransInt16(data, 10);
  392. byte byteCount = (byte)(bitCount / 8 + 1);
  393. byte[] bytes = new byte[byteCount];
  394. for(int i = 0; i < byteCount;i++)
  395. {
  396. byte[] tempbytes = new byte[8];
  397. Array.Copy(DOBytes,8 * i, tempbytes, 0, 8);
  398. bytes[i] = ConvertByteArrayToHex(tempbytes);
  399. }
  400. OnWriteMessage(CreateReadDigitalResponse(flag, channel, command, byteCount, bytes));
  401. return;
  402. }
  403. else if(command == 0x03)//读AO
  404. {
  405. short flag = byteTransform.TransInt16(data, 0);
  406. byte channel = data[6];
  407. short startAddress = byteTransform.TransInt16(data, 8);
  408. short registerCount = byteTransform.TransInt16(data, 10);
  409. short[] shorts = new short[registerCount];//获取指定寄存器里的内容
  410. Array.Copy(AOShorts, 0, shorts, 0, registerCount);
  411. byte[] bytes = new byte[registerCount * 2];
  412. bytes = ConvertShortArrayToByteArray(shorts); //转入长度为shorts数组长度两倍的bytes数组中
  413. OnWriteMessage(CreateReadAnalogyResponse(flag, channel, command, (byte)registerCount, bytes));
  414. return;
  415. }
  416. else if (command == 0x02)//读DI
  417. {
  418. short flag = byteTransform.TransInt16(data, 0);
  419. byte channel = data[6];
  420. short startAddress = byteTransform.TransInt16(data, 8);
  421. short bitCount = byteTransform.TransInt16(data, 10);
  422. byte byteCount = (byte)(bitCount / 8 + 1);
  423. byte[] bytes = new byte[byteCount];
  424. for (int i = 0; i < byteCount; i++)
  425. {
  426. byte[] tempbytes = new byte[8];
  427. Array.Copy(DIBytes, 8 * i, tempbytes, 0, 8);
  428. bytes[i] = ConvertByteArrayToHex(tempbytes);
  429. }
  430. OnWriteMessage(CreateReadDigitalResponse(flag, channel, command, byteCount, bytes));
  431. return;
  432. }
  433. else if (command == 0x04)//读AI
  434. {
  435. short flag = byteTransform.TransInt16(data, 0);
  436. byte channel = data[6];
  437. short startAddress = byteTransform.TransInt16(data, 8);
  438. short registerCount = byteTransform.TransInt16(data, 10);
  439. short[] shorts = new short[registerCount];//获取指定寄存器里的内容
  440. Array.Copy(AIShorts, 0, shorts, 0, registerCount);
  441. byte[] bytes = new byte[registerCount * 2];
  442. bytes = ConvertShortArrayToByteArray(shorts); //转入长度为shorts数组两倍的bytes数组中
  443. OnWriteMessage(CreateReadAnalogyResponse(flag, channel, command, (byte)registerCount, bytes));
  444. return;
  445. }
  446. else if (command == 0x05)//写DO
  447. {
  448. short startAddress = byteTransform.TransInt16(data, 8);
  449. if (startAddress > 0x03FF || startAddress < WRITE_DO_STARTADDRESS)
  450. {
  451. short flag = byteTransform.TransInt16(data, 0);
  452. byte channel = data[6];
  453. OnWriteMessage(CreateError(flag, channel, command, 0x02)); //地址错误
  454. return;
  455. }
  456. int position = startAddress - WRITE_DO_STARTADDRESS;
  457. bool status = data[10] == 0xFF ? true : false;
  458. lock (_writeDOLocker)
  459. {
  460. DOBytes[position] = status ? (byte)1 : (byte)0;
  461. }
  462. OnWriteMessage(data); //原消息返回
  463. //触发Wago对应数据更新
  464. UpdateDataCausedByWago(position, status);
  465. return;
  466. }
  467. else if (command == 0x06)//写AO
  468. {
  469. short startAddress = byteTransform.TransInt16(data, 8);
  470. if(startAddress > 0x02FF || startAddress < WRITE_AO_STARTADDRESS)
  471. {
  472. short flag = byteTransform.TransInt16(data, 0);
  473. byte channel = data[6];
  474. OnWriteMessage(CreateError(flag, channel, command, 0x02)); //地址错误
  475. return;
  476. }
  477. int position = startAddress - WRITE_AO_STARTADDRESS;
  478. short value = byteTransform.TransInt16(data, 10);
  479. lock (_writeAOLocker)
  480. {
  481. AOShorts[position] = value;
  482. }
  483. OnWriteMessage(data); //原消息返回
  484. return;
  485. }
  486. else
  487. {
  488. short flag = byteTransform.TransInt16(data, 0);
  489. byte channel = data[6];
  490. OnWriteMessage(CreateError(flag, channel, command, 0x01)); //指令错误
  491. return;
  492. }
  493. }
  494. /// <summary>
  495. /// 回复读数字量
  496. /// </summary>
  497. /// <param name="flag"></param>
  498. /// <param name="channel"></param>
  499. /// <param name="command"></param>
  500. /// <param name="byteCount"></param>
  501. /// <param name="values"></param>
  502. /// <returns></returns>
  503. private byte[] CreateReadDigitalResponse(short flag, byte channel, byte command, byte byteCount, byte[] values)
  504. {
  505. byte[] bytes = new byte[7 + 2 + values.Length]; //回复字节长度,前面7个字节固定长度 + functionCode一个字节 + byteCount一个字节+values.length个字节
  506. Array.Copy(byteTransform.GetBytes(flag), 0, bytes, 0, 2);
  507. bytes[2] = 0x00;
  508. bytes[3] = 0x00;
  509. short dataLength = (short)(3 + values.Length);
  510. Array.Copy(byteTransform.GetBytes(dataLength), 0, bytes, 4, 2);
  511. bytes[6] = channel;
  512. bytes[7] = command;
  513. bytes[8] = byteCount;
  514. Array.Copy(values, 0, bytes, 9, values.Length);
  515. return bytes;
  516. }
  517. /// <summary>
  518. /// 回复读模拟量
  519. /// </summary>
  520. /// <param name="flag"></param>
  521. /// <param name="channel"></param>
  522. /// <param name="command"></param>
  523. /// <param name="registerCount"></param>
  524. /// <param name="values"></param>
  525. /// <returns></returns>
  526. private byte[] CreateReadAnalogyResponse(short flag, byte channel, byte command, byte registerCount, byte[] values)
  527. {
  528. byte[] bytes = new byte[7 + 2 + 2 * registerCount]; //回复字节长度,前面7个字节固定长度 + functionCode一个字节 + byteCount一个字节+registerCount*2个字节(一个寄存器占两个字节)
  529. Array.Copy(byteTransform.GetBytes(flag), 0, bytes, 0, 2);
  530. bytes[2] = 0x00;
  531. bytes[3] = 0x00;
  532. short dataLength = (short)(3 + 2 * registerCount);
  533. Array.Copy(byteTransform.GetBytes(dataLength), 0, bytes, 4, 2);
  534. bytes[6] = channel;
  535. bytes[7] = command;
  536. bytes[8] = (byte)(2 * registerCount);
  537. Array.Copy(values, 0, bytes, 9, values.Length);
  538. return bytes;
  539. }
  540. /// <summary>
  541. /// 错误回复
  542. /// </summary>
  543. /// <param name="flag"></param>
  544. /// <param name="channel"></param>
  545. /// <param name="command"></param>
  546. /// <param name="error"></param>
  547. /// <returns></returns>
  548. private byte[] CreateError(short flag, byte channel, byte command, byte error)
  549. {
  550. byte[] bytes = new byte[9];
  551. Array.Copy(byteTransform.GetBytes(flag), 0, bytes, 0, 2);
  552. bytes[2] = 0x00;
  553. bytes[3] = 0x00;
  554. bytes[4] = 0x00;
  555. bytes[5] = 0x03;
  556. bytes[6] = channel;
  557. bytes[7] = (byte)(command | 0x80);
  558. bytes[8] = error;
  559. return bytes;
  560. }
  561. /// <summary>
  562. /// 定时器
  563. /// </summary>
  564. /// <returns></returns>
  565. private bool OnTimer()
  566. {
  567. LeakTestSimulator();
  568. return true;
  569. }
  570. /// <summary>
  571. /// Wago2定时器
  572. /// </summary>
  573. /// <returns></returns>
  574. private bool OnTimer1()
  575. {
  576. RinseWaterLelveSimulator();
  577. return true;
  578. }
  579. #region 模拟方法
  580. /// <summary>
  581. /// Loader LeakTest模拟
  582. /// </summary>
  583. private void LeakTestSimulator()
  584. {
  585. if (DOBytes[DONameIndexDic["c_VACUUM_TEST"]] == 1 && _festoDataBuffer["FlowTestClamp"] && AIShorts[AINameIndexDic["r_LOADER_GasFlowSensor_FLOW"]] == 0)
  586. {
  587. AIShorts[AINameIndexDic["r_LOADER_GasFlowSensor_FLOW"]] = 15000;
  588. }
  589. else if (DOBytes[DONameIndexDic["c_VACUUM_TEST"]] == 0 && !_festoDataBuffer["FlowTestClamp"])
  590. {
  591. AIShorts[AINameIndexDic["r_LOADER_GasFlowSensor_FLOW"]] = 0;
  592. }
  593. if (DOBytes[DONameIndexDic["c_VACUUM_TEST"]] == 1 && _festoDataBuffer["FlowTestClamp"] && AIShorts[AINameIndexDic["r_LOADER_GasFlowSensor_FLOW"]] > 3500)
  594. {
  595. AIShorts[AINameIndexDic["r_LOADER_GasFlowSensor_FLOW"]] -= 40;
  596. if (AIShorts[AINameIndexDic["r_LOADER_GasFlowSensor_FLOW"]] < 14000)
  597. {
  598. AIShorts[AINameIndexDic["r_LOADER_GasFlowSensor_FLOW"]] -= 140;
  599. }
  600. }
  601. }
  602. /// <summary>
  603. /// QDR水位模拟
  604. /// </summary>
  605. private void RinseWaterLelveSimulator()
  606. {
  607. //QDR1
  608. if (_rinse1FillValve && AIShorts[AINameIndexDic["r_QDRD1_WATER_LEVEL"]] < 25000)
  609. {
  610. AIShorts[AINameIndexDic["r_QDRD1_WATER_LEVEL"]] += 250;
  611. }
  612. if (_rinse1DumpValve && AIShorts[AINameIndexDic["r_QDRD1_WATER_LEVEL"]] > 3500) //快排
  613. {
  614. AIShorts[AINameIndexDic["r_QDRD1_WATER_LEVEL"]] -= 350;
  615. }
  616. if (AIShorts[AINameIndexDic["r_QDRD1_WATER_LEVEL"]] > 3500)//慢排
  617. {
  618. AIShorts[AINameIndexDic["r_QDRD1_WATER_LEVEL"]] -= 2;
  619. }
  620. //QDR2
  621. if (_rinse2FillValve && AIShorts[AINameIndexDic["r_QDRD2_WATER_LEVEL"]] < 25000)
  622. {
  623. AIShorts[AINameIndexDic["r_QDRD2_WATER_LEVEL"]] += 250;
  624. }
  625. if (_rinse2DumpValve && AIShorts[AINameIndexDic["r_QDRD2_WATER_LEVEL"]] > 3500)
  626. {
  627. AIShorts[AINameIndexDic["r_QDRD2_WATER_LEVEL"]] -= 300;
  628. }
  629. if (AIShorts[AINameIndexDic["r_QDRD2_WATER_LEVEL"]] > 3500)//慢排
  630. {
  631. AIShorts[AINameIndexDic["r_QDRD2_WATER_LEVEL"]] -= 2;
  632. }
  633. //QDR3
  634. if (_rinse3FillValve && AIShorts[AINameIndexDic["r_QDRD3_WATER_LEVEL"]] < 25000)
  635. {
  636. AIShorts[AINameIndexDic["r_QDRD3_WATER_LEVEL"]] += 250;
  637. }
  638. if (_rinse3DumpValve && AIShorts[AINameIndexDic["r_QDRD3_WATER_LEVEL"]] > 3500)
  639. {
  640. AIShorts[AINameIndexDic["r_QDRD3_WATER_LEVEL"]] -= 300;
  641. }
  642. if (AIShorts[AINameIndexDic["r_QDRD3_WATER_LEVEL"]] > 3500)//慢排
  643. {
  644. AIShorts[AINameIndexDic["r_QDRD3_WATER_LEVEL"]] -= 2;
  645. }
  646. //QDR4
  647. if (_rinse4FillValve && AIShorts[AINameIndexDic["r_QDRD4_WATER_LEVEL"]] < 25000)
  648. {
  649. AIShorts[AINameIndexDic["r_QDRD4_WATER_LEVEL"]] += 250;
  650. }
  651. if (_rinse4DumpValve && AIShorts[AINameIndexDic["r_QDRD4_WATER_LEVEL"]] > 3500)
  652. {
  653. AIShorts[AINameIndexDic["r_QDRD4_WATER_LEVEL"]] -= 300;
  654. }
  655. if (AIShorts[AINameIndexDic["r_QDRD4_WATER_LEVEL"]] > 3500)//慢排
  656. {
  657. AIShorts[AINameIndexDic["r_QDRD4_WATER_LEVEL"]] -= 2;
  658. }
  659. }
  660. #endregion
  661. }
  662. }