WagoControllerCfgManager.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542
  1. using Aitex.Common.Util;
  2. using Aitex.Core.RT.Log;
  3. using Aitex.Core.RT.SCCore;
  4. using Aitex.Core.Util;
  5. using DocumentFormat.OpenXml.Wordprocessing;
  6. using MECF.Framework.Common.Device.Festo;
  7. using MECF.Framework.Common.Equipment;
  8. using MECF.Framework.Common.IOCore;
  9. using MECF.Framework.Common.TwinCat;
  10. using System;
  11. using System.Collections.Concurrent;
  12. using System.Collections.Generic;
  13. using System.IO;
  14. using System.Linq;
  15. using System.Reflection;
  16. using System.Text;
  17. using System.Threading.Tasks;
  18. using System.Xml.Linq;
  19. namespace MECF.Framework.Common.Device.Wago
  20. {
  21. public class WagoControllerCfgManager : Singleton<WagoControllerCfgManager>
  22. {
  23. #region 常量
  24. private const string STOPCODE = "StopCode";
  25. private const string REFERENCE_POSITION = "ReferencePosition";
  26. private const string TORQUE = "Torque";
  27. private const string VELOCITY = "Velocity";
  28. private const string POSITION_ERROR = "PositionError";
  29. private const string AUXILIARY_POSITION = "AuxiliaryPosition";
  30. #endregion
  31. #region 内部变量
  32. /// <summary>
  33. /// do--Modbus设备字典(key-DO名称,value-Modbus设备)
  34. /// </summary>
  35. private Dictionary<string, WagoModbusDevice> _ioModbusDictionary = new Dictionary<string, WagoModbusDevice>();
  36. /// <summary>
  37. /// 索引DI名称字典(key-DI索引,value-DI名称)
  38. /// </summary>
  39. private Dictionary<int, string> _indexDIDictionary = new Dictionary<int, string>();
  40. /// <summary>
  41. /// 索引DO名称字典(key-DI索引,value-DO名称)
  42. /// </summary>
  43. private Dictionary<int, string> _indexDODictionary = new Dictionary<int, string>();
  44. /// <summary>
  45. /// 索引AI名称字典(key-DI索引,value-AI名称)
  46. /// </summary>
  47. private Dictionary<int, string> _indexAIDictionary = new Dictionary<int, string>();
  48. /// <summary>
  49. /// 索引AO名称字典(key-DI索引,value-AO名称)
  50. /// </summary>
  51. private Dictionary<int, string> _indexAODictionary = new Dictionary<int, string>();
  52. /// <summary>
  53. /// DI名称索引字典(key-DI名称,value-(byte数组索引,bit索引))
  54. /// </summary>
  55. private Dictionary<string, (int, int)> _diNameIndexDictionary = new Dictionary<string, (int, int)>();
  56. /// <summary>
  57. /// DO名称索引字典(key-DO名称,value-(byte数组索引,bit索引))
  58. /// </summary>
  59. private Dictionary<string, (int, int)> _doNameIndexDictionary = new Dictionary<string, (int, int)>();
  60. /// <summary>
  61. /// 名称数据数组字典(key-festo名称,value-每个festo的byte数组)
  62. /// </summary>
  63. private ConcurrentDictionary<string, byte[]> _moduleDatasDictionary = new ConcurrentDictionary<string, byte[]>();
  64. /// <summary>
  65. /// 模块DI字典(key-模块,value-DI名称集合)
  66. /// </summary>
  67. private Dictionary<string, List<string>> _moduleDINameListDictionary = new Dictionary<string, List<string>>();
  68. /// <summary>
  69. /// 模块DO字典(key-模块,value-DO名称集合)
  70. /// </summary>
  71. private Dictionary<string, List<string>> _moduleDONameListDictionary = new Dictionary<string, List<string>>();
  72. /// <summary>
  73. /// DO-模块字典(key-DO名称,value-模块名称)
  74. /// </summary>
  75. private Dictionary<string, string> _doModuleDictionary = new Dictionary<string, string>();
  76. /// <summary>
  77. /// 模块AI字典(key-模块,value-AI名称集合)
  78. /// </summary>
  79. private Dictionary<string, List<WagoAI>> _moduleAIListDictionary = new Dictionary<string, List<WagoAI>>();
  80. /// <summary>
  81. /// 模块AO字典(key-模块,value-AO名称集合)
  82. /// </summary>
  83. private Dictionary<string, List<WagoAO>> _moduleAOListDictionary = new Dictionary<string, List<WagoAO>>();
  84. /// <summary>
  85. /// DO字典(key-DO名称,value-DO对象)
  86. /// </summary>
  87. private Dictionary<string,WagoDO> _moduleNameDODictionary = new Dictionary<string, WagoDO>();
  88. /// <summary>
  89. /// AO字典(key-AO名称,value-AO对象)
  90. /// </summary>
  91. private Dictionary<string, WagoAO> _moduleNameAODictionary = new Dictionary<string, WagoAO>();
  92. /// <summary>
  93. /// 模块名称数值字典(key-模块名称,value-数值)
  94. /// </summary>
  95. private ConcurrentDictionary<string, object> _moduleNameValueDictionary = new ConcurrentDictionary<string, object>();
  96. #endregion
  97. /// <summary>
  98. /// 初始化
  99. /// </summary>
  100. public void Initialize(List<string> lst)
  101. {
  102. bool isSimulate = SC.GetValue<bool>("System.IsSimulatorMode");
  103. string xmlPath = "";
  104. try
  105. {
  106. if (isSimulate)
  107. {
  108. xmlPath = PathManager.GetCfgDir() + "Devices\\WagoControllerCfg-Simulator.xml";
  109. }
  110. else
  111. {
  112. xmlPath = PathManager.GetCfgDir() + "Devices\\WagoControllerCfg.xml";
  113. }
  114. WagoControllerCfg cfg = CustomXmlSerializer.Deserialize<WagoControllerCfg>(new FileInfo(xmlPath));
  115. if (cfg != null)
  116. {
  117. foreach (var config in cfg.WagoDeviceConfigs)
  118. {
  119. WagoModbusDevice modbusDevice = new WagoModbusDevice(config.Module, config.IpAddress, config.Port,
  120. (byte)config.Channel);
  121. modbusDevice.ReceiveTimeout = config.RecvTimeout;
  122. modbusDevice.SendTimeout = config.SendTimeout;
  123. InitializeWagoDeviceConfig(config, modbusDevice, lst);
  124. }
  125. }
  126. }
  127. catch
  128. {
  129. LOG.WriteLog(eEvent.ERR_GALIL, "Galil", "Load galil xml failed");
  130. }
  131. }
  132. /// <summary>
  133. /// 初始化设备
  134. /// </summary>
  135. /// <param name="deviceConfig"></param>
  136. private void InitializeWagoDeviceConfig(WagoDeviceConfig deviceConfig,WagoModbusDevice modbusDevice,List<string> lst)
  137. {
  138. //DI
  139. ushort diCount = (ushort)deviceConfig.WagoDigIn.WagoDIs.Count;
  140. ushort diStartAddress = (ushort)deviceConfig.WagoDigIn.WagoDIs[0].Address;
  141. List<string> diLst = new List<string>();
  142. List<int> diAddressLst = new List<int>();
  143. int tmpCount = -1;
  144. for (int i = 0; i < deviceConfig.WagoDigIn.WagoDIs.Count; i++)
  145. {
  146. WagoDI item = deviceConfig.WagoDigIn.WagoDIs[i];
  147. _indexDIDictionary[i] = item.Name;
  148. if (!diAddressLst.Contains(item.Address))
  149. {
  150. diAddressLst.Add(item.Address);
  151. tmpCount++;
  152. _diNameIndexDictionary[item.Name] = (tmpCount, item.Bit);
  153. }
  154. else
  155. {
  156. _diNameIndexDictionary[item.Name] = (tmpCount, item.Bit);
  157. }
  158. diLst.Add(item.Name);
  159. }
  160. _moduleDINameListDictionary[deviceConfig.Module] = diLst;
  161. //DO
  162. ushort doCount = (ushort)deviceConfig.WagoDigOut.WagoDOs.Count;
  163. ushort doStartAddress = (ushort)deviceConfig.WagoDigOut.WagoDOs[0].Address;
  164. List<string> doList = new List<string>();
  165. List<int> doAddressLst = new List<int>();
  166. int count = -1;
  167. for (int i = 0; i < deviceConfig.WagoDigOut.WagoDOs.Count; i++)
  168. {
  169. WagoDO item = deviceConfig.WagoDigOut.WagoDOs[i];
  170. _ioModbusDictionary[item.Name] = modbusDevice;
  171. _indexDODictionary[i] = item.Name;
  172. if (!doAddressLst.Contains(item.Address))
  173. {
  174. doAddressLst.Add(item.Address);
  175. count++;
  176. _doNameIndexDictionary[item.Name] = (count, item.Bit);
  177. }
  178. else
  179. {
  180. _doNameIndexDictionary[item.Name] = (count, item.Bit);
  181. }
  182. doList.Add(item.Name);
  183. _moduleNameDODictionary[item.Name] = item;
  184. _doModuleDictionary[item.Name] = deviceConfig.Module;
  185. }
  186. _moduleDONameListDictionary[deviceConfig.Module] = doList;
  187. if (doAddressLst.Count > 0)
  188. {
  189. _moduleDatasDictionary[deviceConfig.Module] = new byte[doAddressLst.Count];
  190. }
  191. //AI
  192. ushort aiCount = (ushort)deviceConfig.WagoAnoIn.WagoAIs.Count;
  193. ushort aiStartAddress = (ushort)deviceConfig.WagoAnoIn.WagoAIs[0].Address;
  194. for (int i = 0; i < deviceConfig.WagoAnoIn.WagoAIs.Count; i++)
  195. {
  196. WagoAI item = deviceConfig.WagoAnoIn.WagoAIs[i];
  197. _indexAIDictionary[i] = item.Name;
  198. if (!string.IsNullOrEmpty(item.Scaling))
  199. {
  200. ScalingManager.Instance.Initialize(item.Name, item.Scaling);
  201. }
  202. }
  203. _moduleAIListDictionary[deviceConfig.Module] = deviceConfig.WagoAnoIn.WagoAIs;
  204. //AO
  205. ushort aoCount = (ushort)deviceConfig.WagoAnoOut.WagoAOs.Count;
  206. ushort aoStartAddress = (ushort)deviceConfig.WagoAnoOut.WagoAOs[0].Address;
  207. for (int i = 0; i < deviceConfig.WagoAnoIn.WagoAIs.Count; i++)
  208. {
  209. WagoAO item = deviceConfig.WagoAnoOut.WagoAOs[i];
  210. _ioModbusDictionary[item.Name] = modbusDevice;
  211. _indexAODictionary[i] = item.Name;
  212. if (!string.IsNullOrEmpty(item.Scaling))
  213. {
  214. ScalingManager.Instance.Initialize(item.Name, item.Scaling);
  215. }
  216. _moduleNameAODictionary[item.Name] = item;
  217. }
  218. _moduleAOListDictionary[deviceConfig.Module] = deviceConfig.WagoAnoOut.WagoAOs;
  219. modbusDevice.Initialize(diCount, diStartAddress, doCount, doStartAddress, aiCount, aiStartAddress, aoCount, aoStartAddress);
  220. }
  221. /// <summary>
  222. /// 更新DI数组
  223. /// </summary>
  224. /// <param name="name"></param>
  225. /// <param name="datas"></param>
  226. public void UpdateWagoDIData(string name, bool[] datas)
  227. {
  228. if (_moduleDINameListDictionary.ContainsKey(name))
  229. {
  230. List<string> diLst = _moduleDINameListDictionary[name];
  231. UpdateWagoDigitalData(diLst, _diNameIndexDictionary, datas);
  232. }
  233. }
  234. /// <summary>
  235. /// 更新DO数组
  236. /// </summary>
  237. /// <param name="name"></param>
  238. /// <param name="datas"></param>
  239. public void UpdateWagoDOData(string name, bool[] datas)
  240. {
  241. if (_moduleDONameListDictionary.ContainsKey(name))
  242. {
  243. UpdateWagoDigitalData(_moduleDONameListDictionary[name], _doNameIndexDictionary, datas);
  244. }
  245. }
  246. /// <summary>
  247. /// 更新Wago数字数值
  248. /// </summary>
  249. /// <param name="lst"></param>
  250. /// <param name="dic"></param>
  251. /// <param name="datas"></param>
  252. private void UpdateWagoDigitalData(List<string> lst,Dictionary<string,(int,int)> dic, bool[] datas)
  253. {
  254. foreach (var item in lst)
  255. {
  256. if (!dic.ContainsKey(item))
  257. {
  258. continue;
  259. }
  260. var varIndex = dic[item];
  261. int boolIndex = varIndex.Item1 * 8 + varIndex.Item2;
  262. if (boolIndex >= datas.Length)
  263. {
  264. continue;
  265. }
  266. bool value = datas[boolIndex];
  267. if (!_moduleNameValueDictionary.ContainsKey(item))
  268. {
  269. _moduleNameValueDictionary[item] = value;
  270. IOModuleManager.Instance.UpdateIoValue(item, value);
  271. }
  272. else
  273. {
  274. if (value != (bool)_moduleNameValueDictionary[item])
  275. {
  276. _moduleNameValueDictionary[item] = value;
  277. IOModuleManager.Instance.UpdateIoValue(item, value);
  278. }
  279. }
  280. }
  281. }
  282. /// <summary>
  283. /// 更新AI数组
  284. /// </summary>
  285. /// <param name="name"></param>
  286. /// <param name="datas"></param>
  287. public void UpdateWagoAIData(string name, byte[] datas)
  288. {
  289. if (_moduleAIListDictionary.ContainsKey(name))
  290. {
  291. List<WagoAI> wagoAIs = _moduleAIListDictionary[name];
  292. for(int i = 0; i < wagoAIs.Count; i++)
  293. {
  294. WagoAI wagoAI = wagoAIs[i];
  295. if (i * 2 >= datas.Length)
  296. {
  297. break;
  298. }
  299. UpdateWagoAnalogData(wagoAI.Name, wagoAI.Scaling, wagoAI.DataType, datas, i);
  300. }
  301. }
  302. }
  303. /// <summary>
  304. /// 更新AO数组
  305. /// </summary>
  306. /// <param name="name"></param>
  307. /// <param name="datas"></param>
  308. public void UpdateWagoAOData(string name, byte[] datas)
  309. {
  310. if (_moduleAOListDictionary.ContainsKey(name))
  311. {
  312. List<WagoAO> wagoAOs = _moduleAOListDictionary[name];
  313. for (int i = 0; i < wagoAOs.Count; i++)
  314. {
  315. WagoAO wagoAO = wagoAOs[i];
  316. if (i * 2 >= datas.Length)
  317. {
  318. break;
  319. }
  320. UpdateWagoAnalogData(wagoAO.Name, wagoAO.Scaling, wagoAO.DataType, datas, i);
  321. }
  322. }
  323. }
  324. /// <summary>
  325. /// 更新模拟量数据
  326. /// </summary>
  327. /// <param name="moduleName"></param>
  328. /// <param name="scaling"></param>
  329. /// <param name="dataType"></param>
  330. /// <param name="datas"></param>
  331. /// <param name="index"></param>
  332. private void UpdateWagoAnalogData(string moduleName,string scaling,string dataType, byte[] datas,int index)
  333. {
  334. object value = null;
  335. if (dataType == "short")
  336. {
  337. value = BitConverter.ToInt16(datas, index * 2);
  338. }
  339. else
  340. {
  341. value = BitConverter.ToUInt16(datas, index * 2);
  342. }
  343. if (!string.IsNullOrEmpty(scaling))
  344. {
  345. value = ScalingManager.Instance.CalculateValueByTwincatVariable(moduleName, (double)value);
  346. }
  347. if (!_moduleNameValueDictionary.ContainsKey(moduleName))
  348. {
  349. _moduleNameValueDictionary[moduleName] = value;
  350. IOModuleManager.Instance.UpdateIoValue(moduleName, value);
  351. }
  352. else
  353. {
  354. if (value.ToString() != _moduleNameValueDictionary[moduleName].ToString())
  355. {
  356. _moduleNameValueDictionary[moduleName] = value;
  357. IOModuleManager.Instance.UpdateIoValue(moduleName, value);
  358. }
  359. }
  360. }
  361. /// <summary>
  362. /// 写入IO数值
  363. /// </summary>
  364. /// <param name="ioName"></param>
  365. /// <param name="value"></param>
  366. /// <returns></returns>
  367. public bool WriteIOValue(string ioName,object value)
  368. {
  369. if (_moduleNameDODictionary.ContainsKey(ioName))
  370. {
  371. return SetDoValue(ioName, (bool)value);
  372. }
  373. else if (_moduleNameAODictionary.ContainsKey(ioName))
  374. {
  375. return SetAoValue(ioName, (double)value);
  376. }
  377. else
  378. {
  379. LOG.WriteLog(eEvent.ERR_WAGO, "Wago", $"{ioName} object is null");
  380. return false;
  381. }
  382. }
  383. /// <summary>
  384. /// 设置DO数值
  385. /// </summary>
  386. /// <param name="doName"></param>
  387. /// <param name="value"></param>
  388. /// <returns></returns>
  389. private bool SetDoValue(string doName, bool value)
  390. {
  391. if (!_moduleNameDODictionary.ContainsKey(doName))
  392. {
  393. LOG.WriteLog(eEvent.ERR_WAGO, "Wago", $"{doName} object is null");
  394. return false;
  395. }
  396. WagoDO wagoDO = _moduleNameDODictionary[doName];
  397. if (!_ioModbusDictionary.ContainsKey(doName))
  398. {
  399. LOG.WriteLog(eEvent.ERR_WAGO, "Wago", $"{doName} wago device is null");
  400. return false;
  401. }
  402. if (!_doNameIndexDictionary.ContainsKey(doName))
  403. {
  404. LOG.WriteLog(eEvent.ERR_WAGO, "Wago", $"{doName} does not have index dictionary");
  405. return false;
  406. }
  407. var var=_doNameIndexDictionary[doName];
  408. WagoModbusDevice device = _ioModbusDictionary[doName];
  409. if (!_doModuleDictionary.ContainsKey(doName))
  410. {
  411. LOG.WriteLog(eEvent.ERR_WAGO, "Wago", $"{doName} does not have module dictionary");
  412. return false;
  413. }
  414. string module = _doModuleDictionary[doName];
  415. if (!_moduleDatasDictionary.ContainsKey(module))
  416. {
  417. LOG.WriteLog(eEvent.ERR_WAGO, "Wago", $"{module} does not have datas dictionary");
  418. return false;
  419. }
  420. byte[] datas = _moduleDatasDictionary[module];
  421. if (var.Item1 >= datas.Length)
  422. {
  423. LOG.WriteLog(eEvent.ERR_WAGO, "Wago", $"{doName} {var.Item1} is over datas length");
  424. return false;
  425. }
  426. try
  427. {
  428. bool lastValue = wagoDO.Invert ? !value : value;
  429. byte festoValue = GenerateWagoData(wagoDO, datas[var.Item1], lastValue);
  430. bool result = device.WriteDOValue((ushort)wagoDO.Address, festoValue);
  431. if (result)
  432. {
  433. LOG.WriteLog(eEvent.INFO_WAGO, "Wago", $"{doName} write {value} success");
  434. }
  435. else
  436. {
  437. LOG.WriteLog(eEvent.ERR_WAGO, "Wago", $"{doName} write {value} failed");
  438. }
  439. return result;
  440. }
  441. catch (Exception ex)
  442. {
  443. LOG.WriteLog(eEvent.ERR_WAGO, "Wago", $"write {doName} value {value} {ex.Message}");
  444. return false;
  445. }
  446. }
  447. /// <summary>
  448. /// 构建Festo字节数值
  449. /// </summary>
  450. /// <param name="wagoDO"></param>
  451. /// <param name="source"></param>
  452. /// <param name="value"></param>
  453. /// <returns></returns>
  454. private byte GenerateWagoData(WagoDO wagoDO, byte source, bool value)
  455. {
  456. int bit = wagoDO.Bit;
  457. bool sourceBit = (source >> bit & 0x01) == 1;
  458. if (sourceBit == value)
  459. {
  460. return source;
  461. }
  462. else
  463. {
  464. if (value)
  465. {
  466. return (byte)(source + Math.Pow(2, bit));
  467. }
  468. else
  469. {
  470. return (byte)(source - Math.Pow(2, bit));
  471. }
  472. }
  473. }
  474. /// <summary>
  475. /// 设置DO数值
  476. /// </summary>
  477. /// <param name="aoName"></param>
  478. /// <param name="value"></param>
  479. /// <returns></returns>
  480. public bool SetAoValue(string aoName, double value)
  481. {
  482. if (!_moduleNameAODictionary.ContainsKey(aoName))
  483. {
  484. LOG.WriteLog(eEvent.ERR_WAGO, "Wago", $"{aoName} object is null");
  485. return false;
  486. }
  487. WagoAO wagoAO = _moduleNameAODictionary[aoName];
  488. if (!_ioModbusDictionary.ContainsKey(aoName))
  489. {
  490. LOG.WriteLog(eEvent.ERR_WAGO, "Wago", $"{aoName} wago device is null");
  491. return false;
  492. }
  493. WagoModbusDevice device = _ioModbusDictionary[aoName];
  494. try
  495. {
  496. byte[] writeByts = null;
  497. if (!string.IsNullOrEmpty(wagoAO.Scaling))
  498. {
  499. var var=ScalingManager.Instance.CalculateTwincatValueByInput(aoName, value);
  500. if (var.Item1)
  501. {
  502. if (wagoAO.DataType == "short")
  503. {
  504. writeByts = BitConverter.GetBytes((short)var.Item2);
  505. }
  506. else
  507. {
  508. writeByts = BitConverter.GetBytes((ushort)var.Item2);
  509. }
  510. }
  511. }
  512. bool result = device.WriteAOValue((ushort)wagoAO.Address, writeByts);
  513. if (result)
  514. {
  515. LOG.WriteLog(eEvent.INFO_WAGO, "Wago", $"{aoName} write {value} success");
  516. }
  517. else
  518. {
  519. LOG.WriteLog(eEvent.ERR_WAGO, "Wago", $"{aoName} write {value} failed");
  520. }
  521. return result;
  522. }
  523. catch (Exception ex)
  524. {
  525. LOG.WriteLog(eEvent.ERR_WAGO, "Wago", $"write {aoName} value {value} {ex.Message}");
  526. return false;
  527. }
  528. }
  529. }
  530. }