WagoSocketSimulator.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  1. using Aitex.Core.RT.Device;
  2. using MECF.Framework.Common.Net;
  3. using MECF.Framework.Simulator.Core.Driver;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Diagnostics.Eventing.Reader;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading;
  10. using System.Threading.Tasks;
  11. using System.Timers;
  12. namespace CyberX8_Simulator.Devices
  13. {
  14. public class WagoSocketSimulator : SocketDeviceSimulator
  15. {
  16. private const short WRITE_DO_STARTADDRESS = 0x0200;
  17. private const short WRITE_AO_STARTADDRESS = 0x0200;
  18. //键是名字,值是对应数据所在的位置 注意:要和WagoControlCfg里面的地址顺序对上
  19. public Dictionary<string, int> DONameIndexDic;
  20. public Dictionary<string, int> DINameIndexDic;
  21. public Dictionary<string, int> AINameIndexDic;
  22. public Dictionary<string, int> AONameIndexDic;
  23. private IByteTransform byteTransform = new BigEndianByteTransformBase();
  24. //存储模拟器数据的数组
  25. public byte[] DOBytes = new byte[100];
  26. public short[] AOShorts = new short[50];
  27. public byte[] DIBytes = new byte[100];
  28. public short[] AIShorts = new short[50];
  29. /// <summary>
  30. /// 写DO锁
  31. /// </summary>
  32. private object _writeDOLocker = new object();
  33. /// <summary>
  34. /// 写AO锁
  35. /// </summary>
  36. private object _writeAOLocker = new object();
  37. public WagoSocketSimulator(int port):base(port)
  38. {
  39. SimulatorCommManager.Instance.OnUpdateVariableValueChanged += UpdataDataCausedByOtherModule;
  40. InitializeData(port);
  41. }
  42. private void UpdataDataCausedByOtherModule(string name,bool value)
  43. {
  44. if (AINameIndexDic.ContainsKey(name))
  45. {
  46. switch (name)
  47. {
  48. case "r_LoaderA_LS_Vacuum_anlg":
  49. case "r_LoaderB_LS_Vacuum_anlg":
  50. AIShorts[AINameIndexDic[name]] = value ? (short)0x2AF8 : (short)0x32C8;
  51. break;
  52. case "r_LOADERA_BERNOULLI_PRESSURE":
  53. case "r_LOADERB_BERNOULLI_PRESSURE":
  54. AIShorts[AINameIndexDic[name]] = value ? (short)0x2AF8 : (short)0x00;
  55. break;
  56. default:
  57. break;
  58. }
  59. }
  60. }
  61. /// <summary>
  62. /// 初始化字典
  63. /// </summary>
  64. private void InitializeData(int port) //端口用于初始化不同Wago设备的字典
  65. {
  66. switch (port)
  67. {
  68. case 501:
  69. DONameIndexDic = new Dictionary<string, int>
  70. {{"c_System_Alarm",0 },
  71. {"c_Pole_Red",1},
  72. {"c_Pole_Amber",2},
  73. {"c_Pole_Green",3},
  74. {"c_Pole_Blue",4},
  75. {"c_System_Alarm2",5},
  76. {"c_BACKSIDE_PRESSURE_TEST",6},
  77. {"c_VACUUM_TEST",7},
  78. {"DO8",8},
  79. {"DO9",9},
  80. {"DO10",10},
  81. {"DO11",11},
  82. {"DO12",12},
  83. {"DO13",13},
  84. {"DO14",14},
  85. {"DO15",15},
  86. {"DO16",16},
  87. {"DO17",17},
  88. {"DO18",18},
  89. {"DO19",19},
  90. {"DO20",20},
  91. {"DO21",21}};
  92. DINameIndexDic = new Dictionary<string, int>
  93. {{"r_Cassette_1_150",0 },
  94. {"r_Cassette_1_100",1},
  95. {"r_Cassette_1_200",2},
  96. {"r_Cassette_2_150",3},
  97. {"r_Cassette_2_100",4},
  98. {"r_Cassette_2_200",5},
  99. {"r_Cassette_3_150",6},
  100. {"r_Cassette_3_100",7},
  101. {"r_Cassette_3_200",8 },
  102. {"r_Dummy_1_150",9},
  103. {"r_Dummy_1_100",10},
  104. {"r_Dummy_1_200",11},
  105. {"r_Dummy_2_150",12},
  106. {"r_Dummy_2_100",13},
  107. {"r_Dummy_2_200",14},
  108. {"DI15",15},
  109. {"r_LoaderA_Wafer_Present",16},
  110. {"r_LoaderB_Wafer_Present",17},
  111. {"r_Cathode_Present",18},
  112. {"DI19",19},
  113. {"DI20",20},
  114. {"DI21",21},
  115. {"DI22",22},
  116. {"DI23",23},
  117. {"DI24",24},
  118. {"DI25",25},
  119. {"r_LOADERA_CRS_CURTAIN_1",26},
  120. {"r_LOADERA_CRS_CURTAIN_2",27},
  121. {"r_LOADERA_CRS_CURTAIN_3",28},
  122. {"r_LOADERA_CRS_CURTAIN_4",29},
  123. {"r_LOADERA_CRS_CURTAIN_5",30},
  124. {"r_LOADERA_CRS_CURTAIN_6",31}};
  125. AINameIndexDic = new Dictionary<string, int>
  126. {{"AI1",0 },
  127. {"AI2",1},
  128. {"AI3",2},
  129. {"AI4",3},
  130. {"r_LoaderA_LS_Vacuum_anlg",4},
  131. {"r_LoaderB_LS_Vacuum_anlg",5},
  132. {"AI6",6},
  133. {"r_LOADER_GasFlowSensor_FLOW",7},
  134. {"r_LOADERA_BERNOULLI_PRESSURE",8},
  135. {"r_LOADERB_BERNOULLI_PRESSURE",9},
  136. {"r_LOADERA_CHUCK_BLADDER",10},
  137. {"r_LOADERB_CHUCK_BLADDER",11},
  138. {"r_LOADERA_WS_BLADDER_PRESSURE",12},
  139. {"r_LOADERB_WS_BLADDER_PRESSURE",13},
  140. {"r_SPUF_VAC",14},
  141. {"r_LOADER_GasFlowSensor_VACUUM",15},};
  142. AONameIndexDic = new Dictionary<string, int>
  143. {{"AO1",0},
  144. {"AO2",1},
  145. {"AO3",2},
  146. {"AO4",3},
  147. {"AO5",4},
  148. {"AO6",5},
  149. {"AO7",6},
  150. {"AO8",7},
  151. {"AO9",8},
  152. {"AO10",9},
  153. {"AO11",10},
  154. {"AO12",11},
  155. {"AO13",12},
  156. {"AO14",13},
  157. {"AO15",14},
  158. {"AO16",15}};
  159. //设置IO变量默认值
  160. AIShorts[AINameIndexDic["r_LoaderA_LS_Vacuum_anlg"]] = 0x32C8;
  161. AIShorts[AINameIndexDic["r_LoaderB_LS_Vacuum_anlg"]] = 0x32C8;
  162. break;
  163. case 550:
  164. DONameIndexDic = new Dictionary<string, int>
  165. {{"DO0",0 }, {"DO1",1 }};
  166. DINameIndexDic = new Dictionary<string, int>
  167. {{"DI0",0 }, {"DI1",1 }};
  168. AONameIndexDic = new Dictionary<string, int>
  169. {{"AO0",0 }, {"AO1",0 }};
  170. AINameIndexDic = new Dictionary<string, int>
  171. {{"AI0",0 }, {"AI1",0 }};
  172. break;
  173. default:
  174. break;
  175. }
  176. }
  177. #region 公共方法
  178. public void UpdataDOBytes(string name,int value)
  179. {
  180. if (DONameIndexDic.ContainsKey(name))
  181. {
  182. if (DONameIndexDic[name] < DOBytes.Length)
  183. {
  184. DOBytes[DONameIndexDic[name]] = value == 0 ? (byte)0 : (byte)1;
  185. }
  186. }
  187. }
  188. public void UpdataDIBytes(string name, int value)
  189. {
  190. if (DINameIndexDic.ContainsKey(name))
  191. {
  192. if (DINameIndexDic[name] < DIBytes.Length)
  193. {
  194. DIBytes[DINameIndexDic[name]] = value == 0 ? (byte)0 : (byte)1;
  195. }
  196. }
  197. }
  198. public void UpdataAOShorts(string name, int value)
  199. {
  200. if (AONameIndexDic.ContainsKey(name))
  201. {
  202. string hexValue = value.ToString("X2");
  203. try
  204. {
  205. short result = Convert.ToInt16(hexValue, 16);
  206. if (AONameIndexDic[name] < AOShorts.Length)
  207. {
  208. AOShorts[AONameIndexDic[name]] = result;
  209. }
  210. }
  211. catch (FormatException)
  212. {
  213. }
  214. }
  215. }
  216. public void UpdataAIShorts(string name, int value)
  217. {
  218. if (AINameIndexDic.ContainsKey(name))
  219. {
  220. string hexValue = value.ToString("X2");
  221. try
  222. {
  223. short result = Convert.ToInt16(hexValue, 16);
  224. if(AINameIndexDic[name] < AIShorts.Length)
  225. {
  226. AIShorts[AINameIndexDic[name]] = result;
  227. }
  228. }
  229. catch (FormatException)
  230. {
  231. }
  232. }
  233. }
  234. #endregion
  235. #region 功能方法
  236. /// <summary>
  237. /// 将长度为8的二进制byte数组转成对应十六进制byte值(大端模式)
  238. /// </summary>
  239. /// <param name="byteArray"></param>
  240. /// <returns></returns>
  241. public byte ConvertByteArrayToHex(byte[] byteArray)
  242. {
  243. byte result = 0;
  244. // 先将 byte 数组转换为二进制数
  245. int binaryValue = 0;
  246. for (int i = 0; i < 8; i++)
  247. {
  248. binaryValue |= (byteArray[i] << (7 - i));
  249. }
  250. // 逆转二进制数
  251. int reversedValue = 0;
  252. for (int i = 0; i < 8; i++)
  253. {
  254. reversedValue |= ((binaryValue >> i) & 1) << (7 - i);
  255. }
  256. // 转换为十六进制byte
  257. if (byte.TryParse(reversedValue.ToString("X2"), System.Globalization.NumberStyles.HexNumber, null, out result))
  258. {
  259. return result;
  260. }
  261. return 0;
  262. }
  263. /// <summary>
  264. /// 将short数组转成长度两倍的byte数组
  265. /// </summary>
  266. /// <param name="shortArray"></param>
  267. /// <returns></returns>
  268. private byte[] ConvertShortArrayToByteArray(short[] shortArray)
  269. {
  270. byte[] byteArray = new byte[shortArray.Length * 2];
  271. for (int i = 0; i < shortArray.Length; i++)
  272. {
  273. byte[] tempBytes = BitConverter.GetBytes(shortArray[i]);
  274. Array.Reverse(tempBytes);
  275. Array.Copy(tempBytes, 0, byteArray, i * 2, 2);
  276. }
  277. return byteArray;
  278. }
  279. #endregion
  280. protected override void ProcessUnsplitMessage(byte[] data)
  281. {
  282. byte command = data[7];
  283. if (command == 0x01) //读DO
  284. {
  285. short flag = byteTransform.TransInt16(data, 0);
  286. byte channel = data[6];
  287. short startAddress = byteTransform.TransInt16(data, 8);
  288. short bitCount = byteTransform.TransInt16(data, 10);
  289. byte byteCount = (byte)(bitCount / 8 + 1);
  290. byte[] bytes = new byte[byteCount];
  291. for(int i = 0; i < byteCount;i++)
  292. {
  293. byte[] tempbytes = new byte[8];
  294. Array.Copy(DOBytes,8 * i, tempbytes, 0, 8);
  295. bytes[i] = ConvertByteArrayToHex(tempbytes);
  296. }
  297. OnWriteMessage(CreateReadDigitalResponse(flag, channel, command, byteCount, bytes));
  298. return;
  299. }
  300. else if(command == 0x03)//读AO
  301. {
  302. short flag = byteTransform.TransInt16(data, 0);
  303. byte channel = data[6];
  304. short startAddress = byteTransform.TransInt16(data, 8);
  305. short registerCount = byteTransform.TransInt16(data, 10);
  306. short[] shorts = new short[registerCount];//获取指定寄存器里的内容
  307. Array.Copy(AOShorts, 0, shorts, 0, registerCount);
  308. byte[] bytes = new byte[registerCount * 2];
  309. bytes = ConvertShortArrayToByteArray(shorts); //转入长度为shorts数组长度两倍的bytes数组中
  310. OnWriteMessage(CreateReadAnalogyResponse(flag, channel, command, (byte)registerCount, bytes));
  311. return;
  312. }
  313. else if (command == 0x02)//读DI
  314. {
  315. short flag = byteTransform.TransInt16(data, 0);
  316. byte channel = data[6];
  317. short startAddress = byteTransform.TransInt16(data, 8);
  318. short bitCount = byteTransform.TransInt16(data, 10);
  319. byte byteCount = (byte)(bitCount / 8 + 1);
  320. byte[] bytes = new byte[byteCount];
  321. for (int i = 0; i < byteCount; i++)
  322. {
  323. byte[] tempbytes = new byte[8];
  324. Array.Copy(DIBytes, 8 * i, tempbytes, 0, 8);
  325. bytes[i] = ConvertByteArrayToHex(tempbytes);
  326. }
  327. OnWriteMessage(CreateReadDigitalResponse(flag, channel, command, byteCount, bytes));
  328. return;
  329. }
  330. else if (command == 0x04)//读AI
  331. {
  332. short flag = byteTransform.TransInt16(data, 0);
  333. byte channel = data[6];
  334. short startAddress = byteTransform.TransInt16(data, 8);
  335. short registerCount = byteTransform.TransInt16(data, 10);
  336. short[] shorts = new short[registerCount];//获取指定寄存器里的内容
  337. Array.Copy(AIShorts, 0, shorts, 0, registerCount);
  338. byte[] bytes = new byte[registerCount * 2];
  339. bytes = ConvertShortArrayToByteArray(shorts); //转入长度为shorts数组两倍的bytes数组中
  340. OnWriteMessage(CreateReadAnalogyResponse(flag, channel, command, (byte)registerCount, bytes));
  341. return;
  342. }
  343. else if (command == 0x05)//写DO
  344. {
  345. short startAddress = byteTransform.TransInt16(data, 8);
  346. if (startAddress > 0x03FF || startAddress < WRITE_DO_STARTADDRESS)
  347. {
  348. short flag = byteTransform.TransInt16(data, 0);
  349. byte channel = data[6];
  350. OnWriteMessage(CreateError(flag, channel, command, 0x02)); //地址错误
  351. return;
  352. }
  353. int position = startAddress - WRITE_DO_STARTADDRESS;
  354. bool status = data[10] == 0xFF ? true : false;
  355. lock (_writeDOLocker)
  356. {
  357. DOBytes[position] = status ? (byte)1 : (byte)0;
  358. }
  359. OnWriteMessage(data); //原消息返回
  360. return;
  361. }
  362. else if (command == 0x06)//写AO
  363. {
  364. short startAddress = byteTransform.TransInt16(data, 8);
  365. if(startAddress > 0x02FF || startAddress < WRITE_AO_STARTADDRESS)
  366. {
  367. short flag = byteTransform.TransInt16(data, 0);
  368. byte channel = data[6];
  369. OnWriteMessage(CreateError(flag, channel, command, 0x02)); //地址错误
  370. return;
  371. }
  372. int position = startAddress - WRITE_AO_STARTADDRESS;
  373. short value = byteTransform.TransInt16(data, 10);
  374. lock (_writeAOLocker)
  375. {
  376. AOShorts[position] = value;
  377. }
  378. OnWriteMessage(data); //原消息返回
  379. return;
  380. }
  381. else
  382. {
  383. short flag = byteTransform.TransInt16(data, 0);
  384. byte channel = data[6];
  385. OnWriteMessage(CreateError(flag, channel, command, 0x01)); //指令错误
  386. return;
  387. }
  388. }
  389. /// <summary>
  390. /// 回复读数字量
  391. /// </summary>
  392. /// <param name="flag"></param>
  393. /// <param name="channel"></param>
  394. /// <param name="command"></param>
  395. /// <param name="byteCount"></param>
  396. /// <param name="values"></param>
  397. /// <returns></returns>
  398. private byte[] CreateReadDigitalResponse(short flag, byte channel, byte command, byte byteCount, byte[] values)
  399. {
  400. byte[] bytes = new byte[7 + 2 + values.Length]; //回复字节长度,前面7个字节固定长度 + functionCode一个字节 + byteCount一个字节+values.length个字节
  401. Array.Copy(byteTransform.GetBytes(flag), 0, bytes, 0, 2);
  402. bytes[2] = 0x00;
  403. bytes[3] = 0x00;
  404. short dataLength = (short)(3 + values.Length);
  405. Array.Copy(byteTransform.GetBytes(dataLength), 0, bytes, 4, 2);
  406. bytes[6] = channel;
  407. bytes[7] = command;
  408. bytes[8] = byteCount;
  409. Array.Copy(values, 0, bytes, 9, values.Length);
  410. return bytes;
  411. }
  412. /// <summary>
  413. /// 回复读模拟量
  414. /// </summary>
  415. /// <param name="flag"></param>
  416. /// <param name="channel"></param>
  417. /// <param name="command"></param>
  418. /// <param name="registerCount"></param>
  419. /// <param name="values"></param>
  420. /// <returns></returns>
  421. private byte[] CreateReadAnalogyResponse(short flag, byte channel, byte command, byte registerCount, byte[] values)
  422. {
  423. byte[] bytes = new byte[7 + 2 + 2 * registerCount]; //回复字节长度,前面7个字节固定长度 + functionCode一个字节 + byteCount一个字节+registerCount*2个字节(一个寄存器占两个字节)
  424. Array.Copy(byteTransform.GetBytes(flag), 0, bytes, 0, 2);
  425. bytes[2] = 0x00;
  426. bytes[3] = 0x00;
  427. short dataLength = (short)(3 + 2 * registerCount);
  428. Array.Copy(byteTransform.GetBytes(dataLength), 0, bytes, 4, 2);
  429. bytes[6] = channel;
  430. bytes[7] = command;
  431. bytes[8] = (byte)(2 * registerCount);
  432. Array.Copy(values, 0, bytes, 9, values.Length);
  433. return bytes;
  434. }
  435. /// <summary>
  436. /// 错误回复
  437. /// </summary>
  438. /// <param name="flag"></param>
  439. /// <param name="channel"></param>
  440. /// <param name="command"></param>
  441. /// <param name="error"></param>
  442. /// <returns></returns>
  443. private byte[] CreateError(short flag, byte channel, byte command, byte error)
  444. {
  445. byte[] bytes = new byte[9];
  446. Array.Copy(byteTransform.GetBytes(flag), 0, bytes, 0, 2);
  447. bytes[2] = 0x00;
  448. bytes[3] = 0x00;
  449. bytes[4] = 0x00;
  450. bytes[5] = 0x03;
  451. bytes[6] = channel;
  452. bytes[7] = (byte)(command | 0x80);
  453. bytes[8] = error;
  454. return bytes;
  455. }
  456. }
  457. }