WagoSocketSimulator.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  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.Wago;
  6. using MECF.Framework.Common.Net;
  7. using MECF.Framework.Simulator.Core.Driver;
  8. using System;
  9. using System.Collections.Generic;
  10. using System.IO;
  11. namespace CyberX8_Simulator.Devices
  12. {
  13. public class WagoSocketSimulator : SocketDeviceSimulator
  14. {
  15. private const short WRITE_DO_STARTADDRESS = 0x0200;
  16. private const short WRITE_AO_STARTADDRESS = 0x0200;
  17. //键是名字,值是对应数据所在的位置 注意:要和WagoControlCfg里面的地址顺序对上
  18. public Dictionary<string, int> DONameIndexDic;
  19. public Dictionary<string, int> DINameIndexDic;
  20. public Dictionary<string, int> AINameIndexDic;
  21. public Dictionary<string, int> AONameIndexDic;
  22. private IByteTransform byteTransform = new BigEndianByteTransformBase();
  23. //存储模拟器数据的数组
  24. public byte[] DOBytes = new byte[100];
  25. public short[] AOShorts = new short[50];
  26. public byte[] DIBytes = new byte[100];
  27. public short[] AIShorts = new short[50];
  28. /// <summary>
  29. /// 写DO锁
  30. /// </summary>
  31. private object _writeDOLocker = new object();
  32. /// <summary>
  33. /// 写AO锁
  34. /// </summary>
  35. private object _writeAOLocker = new object();
  36. public WagoSocketSimulator(int port):base(port)
  37. {
  38. SimulatorCommManager.Instance.OnUpdateVariableValueChanged += UpdataDataCausedByOtherModule;
  39. InitializeData(port);
  40. }
  41. private void UpdataDataCausedByOtherModule(string name,bool value)
  42. {
  43. if (AINameIndexDic.ContainsKey(name))
  44. {
  45. switch (name)
  46. {
  47. case "r_LoaderA_LS_Vacuum_anlg":
  48. case "r_LoaderB_LS_Vacuum_anlg":
  49. AIShorts[AINameIndexDic[name]] = value ? (short)0x2AF8 : (short)0x32C8;
  50. break;
  51. case "r_LOADERA_BERNOULLI_PRESSURE":
  52. case "r_LOADERB_BERNOULLI_PRESSURE":
  53. AIShorts[AINameIndexDic[name]] = value ? (short)0x2AF8 : (short)0x00;
  54. break;
  55. default:
  56. break;
  57. }
  58. }
  59. }
  60. /// <summary>
  61. /// 初始化字典
  62. /// </summary>
  63. private void InitializeData(int port) //端口用于初始化不同Wago设备的字典
  64. {
  65. //加载对应配置文件 WagoControllerCfg-Simulator.xml
  66. try
  67. {
  68. string oldXmlPath = PathManager.GetCfgDir();
  69. string newXmlPath = oldXmlPath.Replace("CyberX8_Simulator", "CyberX8_RT") + "Devices\\WagoControllerCfg-Simulator.xml";
  70. WagoControllerCfg cfg = CustomXmlSerializer.Deserialize<WagoControllerCfg>(new FileInfo(newXmlPath));
  71. if (cfg != null)
  72. {
  73. foreach (WagoDeviceConfig config in cfg.WagoDeviceConfigs)
  74. {
  75. if (port == config.Port)
  76. {
  77. //加载DO
  78. int i = 0;
  79. DONameIndexDic = new Dictionary<string, int>();
  80. foreach (var group in config.WagoDigOut.WagoDOGroups)
  81. {
  82. foreach (var item in group.WagoDOs)
  83. {
  84. DONameIndexDic[item.Name] = i;
  85. }
  86. }
  87. //加载DI
  88. i = 0;
  89. DINameIndexDic = new Dictionary<string, int>();
  90. foreach (var group in config.WagoDigIn.WagoDIGroups)
  91. {
  92. foreach(var item in group.WagoDIs)
  93. {
  94. DINameIndexDic[item.Name] = i;
  95. i++;
  96. }
  97. }
  98. //加载AO
  99. i = 0;
  100. AONameIndexDic = new Dictionary<string, int>();
  101. foreach (var group in config.WagoAnoOut.WagoAOGroups)
  102. {
  103. foreach (var item in group.WagoAOs)
  104. {
  105. AONameIndexDic[item.Name] = i;
  106. i++;
  107. }
  108. }
  109. //加载AI
  110. i = 0;
  111. AINameIndexDic = new Dictionary<string, int>();
  112. foreach (var group in config.WagoAnoIn.WagoAIGroups)
  113. {
  114. foreach (var item in group.WagoAIs)
  115. {
  116. AINameIndexDic[item.Name] = i;
  117. i++;
  118. }
  119. }
  120. }
  121. }
  122. }
  123. }
  124. catch
  125. {
  126. LOG.WriteLog(eEvent.ERR_WAGO, "Wago", "Load wago WagoControllerCfg-Simulator.xml failed");
  127. }
  128. //设置IO变量默认值
  129. if(AINameIndexDic.ContainsKey("r_LoaderA_LS_Vacuum_anlg")) AIShorts[AINameIndexDic["r_LoaderA_LS_Vacuum_anlg"]] = 0x32C8;
  130. if(AINameIndexDic.ContainsKey("r_LoaderB_LS_Vacuum_anlg")) AIShorts[AINameIndexDic["r_LoaderB_LS_Vacuum_anlg"]] = 0x32C8;
  131. }
  132. #region 公共方法
  133. public void UpdataDOBytes(string name,int value)
  134. {
  135. if (DONameIndexDic.ContainsKey(name))
  136. {
  137. if (DONameIndexDic[name] < DOBytes.Length)
  138. {
  139. DOBytes[DONameIndexDic[name]] = value == 0 ? (byte)0 : (byte)1;
  140. }
  141. }
  142. }
  143. public void UpdataDIBytes(string name, int value)
  144. {
  145. if (DINameIndexDic.ContainsKey(name))
  146. {
  147. if (DINameIndexDic[name] < DIBytes.Length)
  148. {
  149. DIBytes[DINameIndexDic[name]] = value == 0 ? (byte)0 : (byte)1;
  150. }
  151. }
  152. }
  153. public void UpdataAOShorts(string name, int value)
  154. {
  155. if (AONameIndexDic.ContainsKey(name))
  156. {
  157. string hexValue = value.ToString("X2");
  158. try
  159. {
  160. short result = Convert.ToInt16(hexValue, 16);
  161. if (AONameIndexDic[name] < AOShorts.Length)
  162. {
  163. AOShorts[AONameIndexDic[name]] = result;
  164. }
  165. }
  166. catch (FormatException)
  167. {
  168. }
  169. }
  170. }
  171. public void UpdataAIShorts(string name, int value)
  172. {
  173. if (AINameIndexDic.ContainsKey(name))
  174. {
  175. string hexValue = value.ToString("X2");
  176. try
  177. {
  178. short result = Convert.ToInt16(hexValue, 16);
  179. if(AINameIndexDic[name] < AIShorts.Length)
  180. {
  181. AIShorts[AINameIndexDic[name]] = result;
  182. }
  183. }
  184. catch (FormatException)
  185. {
  186. }
  187. }
  188. }
  189. #endregion
  190. #region 功能方法
  191. /// <summary>
  192. /// 将长度为8的二进制byte数组转成对应十六进制byte值(大端模式)
  193. /// </summary>
  194. /// <param name="byteArray"></param>
  195. /// <returns></returns>
  196. public byte ConvertByteArrayToHex(byte[] byteArray)
  197. {
  198. byte result = 0;
  199. // 先将 byte 数组转换为二进制数
  200. int binaryValue = 0;
  201. for (int i = 0; i < 8; i++)
  202. {
  203. binaryValue |= (byteArray[i] << (7 - i));
  204. }
  205. // 逆转二进制数
  206. int reversedValue = 0;
  207. for (int i = 0; i < 8; i++)
  208. {
  209. reversedValue |= ((binaryValue >> i) & 1) << (7 - i);
  210. }
  211. // 转换为十六进制byte
  212. if (byte.TryParse(reversedValue.ToString("X2"), System.Globalization.NumberStyles.HexNumber, null, out result))
  213. {
  214. return result;
  215. }
  216. return 0;
  217. }
  218. /// <summary>
  219. /// 将short数组转成长度两倍的byte数组
  220. /// </summary>
  221. /// <param name="shortArray"></param>
  222. /// <returns></returns>
  223. private byte[] ConvertShortArrayToByteArray(short[] shortArray)
  224. {
  225. byte[] byteArray = new byte[shortArray.Length * 2];
  226. for (int i = 0; i < shortArray.Length; i++)
  227. {
  228. byte[] tempBytes = BitConverter.GetBytes(shortArray[i]);
  229. Array.Reverse(tempBytes);
  230. Array.Copy(tempBytes, 0, byteArray, i * 2, 2);
  231. }
  232. return byteArray;
  233. }
  234. #endregion
  235. protected override void ProcessUnsplitMessage(byte[] data)
  236. {
  237. byte command = data[7];
  238. if (command == 0x01) //读DO
  239. {
  240. short flag = byteTransform.TransInt16(data, 0);
  241. byte channel = data[6];
  242. short startAddress = byteTransform.TransInt16(data, 8);
  243. short bitCount = byteTransform.TransInt16(data, 10);
  244. byte byteCount = (byte)(bitCount / 8 + 1);
  245. byte[] bytes = new byte[byteCount];
  246. for(int i = 0; i < byteCount;i++)
  247. {
  248. byte[] tempbytes = new byte[8];
  249. Array.Copy(DOBytes,8 * i, tempbytes, 0, 8);
  250. bytes[i] = ConvertByteArrayToHex(tempbytes);
  251. }
  252. OnWriteMessage(CreateReadDigitalResponse(flag, channel, command, byteCount, bytes));
  253. return;
  254. }
  255. else if(command == 0x03)//读AO
  256. {
  257. short flag = byteTransform.TransInt16(data, 0);
  258. byte channel = data[6];
  259. short startAddress = byteTransform.TransInt16(data, 8);
  260. short registerCount = byteTransform.TransInt16(data, 10);
  261. short[] shorts = new short[registerCount];//获取指定寄存器里的内容
  262. Array.Copy(AOShorts, 0, shorts, 0, registerCount);
  263. byte[] bytes = new byte[registerCount * 2];
  264. bytes = ConvertShortArrayToByteArray(shorts); //转入长度为shorts数组长度两倍的bytes数组中
  265. OnWriteMessage(CreateReadAnalogyResponse(flag, channel, command, (byte)registerCount, bytes));
  266. return;
  267. }
  268. else if (command == 0x02)//读DI
  269. {
  270. short flag = byteTransform.TransInt16(data, 0);
  271. byte channel = data[6];
  272. short startAddress = byteTransform.TransInt16(data, 8);
  273. short bitCount = byteTransform.TransInt16(data, 10);
  274. byte byteCount = (byte)(bitCount / 8 + 1);
  275. byte[] bytes = new byte[byteCount];
  276. for (int i = 0; i < byteCount; i++)
  277. {
  278. byte[] tempbytes = new byte[8];
  279. Array.Copy(DIBytes, 8 * i, tempbytes, 0, 8);
  280. bytes[i] = ConvertByteArrayToHex(tempbytes);
  281. }
  282. OnWriteMessage(CreateReadDigitalResponse(flag, channel, command, byteCount, bytes));
  283. return;
  284. }
  285. else if (command == 0x04)//读AI
  286. {
  287. short flag = byteTransform.TransInt16(data, 0);
  288. byte channel = data[6];
  289. short startAddress = byteTransform.TransInt16(data, 8);
  290. short registerCount = byteTransform.TransInt16(data, 10);
  291. short[] shorts = new short[registerCount];//获取指定寄存器里的内容
  292. Array.Copy(AIShorts, 0, shorts, 0, registerCount);
  293. byte[] bytes = new byte[registerCount * 2];
  294. bytes = ConvertShortArrayToByteArray(shorts); //转入长度为shorts数组两倍的bytes数组中
  295. OnWriteMessage(CreateReadAnalogyResponse(flag, channel, command, (byte)registerCount, bytes));
  296. return;
  297. }
  298. else if (command == 0x05)//写DO
  299. {
  300. short startAddress = byteTransform.TransInt16(data, 8);
  301. if (startAddress > 0x03FF || startAddress < WRITE_DO_STARTADDRESS)
  302. {
  303. short flag = byteTransform.TransInt16(data, 0);
  304. byte channel = data[6];
  305. OnWriteMessage(CreateError(flag, channel, command, 0x02)); //地址错误
  306. return;
  307. }
  308. int position = startAddress - WRITE_DO_STARTADDRESS;
  309. bool status = data[10] == 0xFF ? true : false;
  310. lock (_writeDOLocker)
  311. {
  312. DOBytes[position] = status ? (byte)1 : (byte)0;
  313. }
  314. OnWriteMessage(data); //原消息返回
  315. return;
  316. }
  317. else if (command == 0x06)//写AO
  318. {
  319. short startAddress = byteTransform.TransInt16(data, 8);
  320. if(startAddress > 0x02FF || startAddress < WRITE_AO_STARTADDRESS)
  321. {
  322. short flag = byteTransform.TransInt16(data, 0);
  323. byte channel = data[6];
  324. OnWriteMessage(CreateError(flag, channel, command, 0x02)); //地址错误
  325. return;
  326. }
  327. int position = startAddress - WRITE_AO_STARTADDRESS;
  328. short value = byteTransform.TransInt16(data, 10);
  329. lock (_writeAOLocker)
  330. {
  331. AOShorts[position] = value;
  332. }
  333. OnWriteMessage(data); //原消息返回
  334. return;
  335. }
  336. else
  337. {
  338. short flag = byteTransform.TransInt16(data, 0);
  339. byte channel = data[6];
  340. OnWriteMessage(CreateError(flag, channel, command, 0x01)); //指令错误
  341. return;
  342. }
  343. }
  344. /// <summary>
  345. /// 回复读数字量
  346. /// </summary>
  347. /// <param name="flag"></param>
  348. /// <param name="channel"></param>
  349. /// <param name="command"></param>
  350. /// <param name="byteCount"></param>
  351. /// <param name="values"></param>
  352. /// <returns></returns>
  353. private byte[] CreateReadDigitalResponse(short flag, byte channel, byte command, byte byteCount, byte[] values)
  354. {
  355. byte[] bytes = new byte[7 + 2 + values.Length]; //回复字节长度,前面7个字节固定长度 + functionCode一个字节 + byteCount一个字节+values.length个字节
  356. Array.Copy(byteTransform.GetBytes(flag), 0, bytes, 0, 2);
  357. bytes[2] = 0x00;
  358. bytes[3] = 0x00;
  359. short dataLength = (short)(3 + values.Length);
  360. Array.Copy(byteTransform.GetBytes(dataLength), 0, bytes, 4, 2);
  361. bytes[6] = channel;
  362. bytes[7] = command;
  363. bytes[8] = byteCount;
  364. Array.Copy(values, 0, bytes, 9, values.Length);
  365. return bytes;
  366. }
  367. /// <summary>
  368. /// 回复读模拟量
  369. /// </summary>
  370. /// <param name="flag"></param>
  371. /// <param name="channel"></param>
  372. /// <param name="command"></param>
  373. /// <param name="registerCount"></param>
  374. /// <param name="values"></param>
  375. /// <returns></returns>
  376. private byte[] CreateReadAnalogyResponse(short flag, byte channel, byte command, byte registerCount, byte[] values)
  377. {
  378. byte[] bytes = new byte[7 + 2 + 2 * registerCount]; //回复字节长度,前面7个字节固定长度 + functionCode一个字节 + byteCount一个字节+registerCount*2个字节(一个寄存器占两个字节)
  379. Array.Copy(byteTransform.GetBytes(flag), 0, bytes, 0, 2);
  380. bytes[2] = 0x00;
  381. bytes[3] = 0x00;
  382. short dataLength = (short)(3 + 2 * registerCount);
  383. Array.Copy(byteTransform.GetBytes(dataLength), 0, bytes, 4, 2);
  384. bytes[6] = channel;
  385. bytes[7] = command;
  386. bytes[8] = (byte)(2 * registerCount);
  387. Array.Copy(values, 0, bytes, 9, values.Length);
  388. return bytes;
  389. }
  390. /// <summary>
  391. /// 错误回复
  392. /// </summary>
  393. /// <param name="flag"></param>
  394. /// <param name="channel"></param>
  395. /// <param name="command"></param>
  396. /// <param name="error"></param>
  397. /// <returns></returns>
  398. private byte[] CreateError(short flag, byte channel, byte command, byte error)
  399. {
  400. byte[] bytes = new byte[9];
  401. Array.Copy(byteTransform.GetBytes(flag), 0, bytes, 0, 2);
  402. bytes[2] = 0x00;
  403. bytes[3] = 0x00;
  404. bytes[4] = 0x00;
  405. bytes[5] = 0x03;
  406. bytes[6] = channel;
  407. bytes[7] = (byte)(command | 0x80);
  408. bytes[8] = error;
  409. return bytes;
  410. }
  411. }
  412. }