TwincatCoeManager.cs 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. using Aitex.Core.RT.Log;
  2. using Aitex.Core.Util;
  3. using MECF.Framework.Common.Beckhoff.IOAxis;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. namespace MECF.Framework.Common.TwinCat
  10. {
  11. public class TwincatCoeManager : Singleton<TwincatCoeManager>
  12. {
  13. #region 内部变量
  14. /// <summary>
  15. /// 模块COE字典(key-模块名称,key-TwincatCoe接口对象)
  16. /// </summary>
  17. private Dictionary<string, TwincatCoeInterface> _moduleCoeInterfaceDic = new Dictionary<string, TwincatCoeInterface>();
  18. /// <summary>
  19. /// 地址COE字典(key-NetId:Port,key-TwincatCoe接口对象)
  20. /// </summary>
  21. private Dictionary<string, TwincatCoeInterface> _addressInterfaceDic = new Dictionary<string, TwincatCoeInterface>();
  22. #endregion
  23. /// <summary>
  24. /// 初始化
  25. /// </summary>
  26. /// <param name="moduleName"></param>
  27. /// <param name="beckhoffAxis"></param>
  28. public void Initialize(BeckhoffAxis beckhoffAxis)
  29. {
  30. string address = $"{beckhoffAxis.COEAddress}.{beckhoffAxis.COEPort}";
  31. if (!_addressInterfaceDic.ContainsKey(address))
  32. {
  33. TwincatCoeInterface coeInterface = new TwincatCoeInterface(beckhoffAxis);
  34. _moduleCoeInterfaceDic[beckhoffAxis.Name] = coeInterface;
  35. _addressInterfaceDic[address] = coeInterface;
  36. coeInterface.Connect();
  37. }
  38. else
  39. {
  40. _moduleCoeInterfaceDic[beckhoffAxis.Name] = _addressInterfaceDic[address];
  41. }
  42. _addressInterfaceDic[address].InnitialInputAndOutputVariable(beckhoffAxis);
  43. }
  44. /// <summary>
  45. /// 启动读取输入变量
  46. /// </summary>
  47. public void StartReadInput()
  48. {
  49. string[] strAry=_addressInterfaceDic.Keys.ToArray();
  50. foreach (string item in strAry)
  51. {
  52. _addressInterfaceDic[item].StartReadInputDataThread();
  53. }
  54. }
  55. /// <summary>
  56. /// 写变量数值
  57. /// </summary>
  58. /// <param name="variableName"></param>
  59. /// <param name="value"></param>
  60. /// <returns></returns>
  61. public bool WriteVariableValue(string moduleName,string variableName,object value)
  62. {
  63. if(_moduleCoeInterfaceDic.ContainsKey(moduleName))
  64. {
  65. return _moduleCoeInterfaceDic[moduleName].WriteModuleCoeData(moduleName,variableName, value);
  66. }
  67. else
  68. {
  69. LOG.WriteLog(eEvent.ERR_TWINCAT, moduleName, $"Twincat Coe variables doesnot have [{moduleName}] module");
  70. return false;
  71. }
  72. }
  73. }
  74. }