WagoControllerCfgManager.cs 23 KB

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