BeckhoffOpcHelper.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. using ProjectBase.Data.Logs;
  2. using ProjectBase.Data.Redis;
  3. using SIASUN.Autopilot.Model;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. namespace SIASUN.Autopilot.Device.BeckHoff
  8. {
  9. internal class BeckhoffOpcHelper : IOpcHelper
  10. {
  11. private Dictionary<string , AbsTwinCatOpc> dicTwincatOpc = null;
  12. private int PLC_COUNT = 0;
  13. private List<MoBeckhoffNode> _moList = new List<MoBeckhoffNode>();
  14. static RedisHelper redis = null;
  15. public void Opc_Init(List<MoBeckhoffNode> mlist)
  16. {
  17. try
  18. {
  19. _moList = mlist;//BLLFactory<BlBeckhoffNode>.Instance.GetAll();
  20. PLC_COUNT = _moList.Count;
  21. if (redis == null)
  22. redis = new RedisHelper();
  23. if (dicTwincatOpc.Count > 0)
  24. dicTwincatOpc.Clear();
  25. var nodelist = from mo in _moList select mo.NodeName;
  26. if (PLC_COUNT <= 0)
  27. return;
  28. else
  29. dicTwincatOpc = new Dictionary<string, AbsTwinCatOpc>(nodelist.Count());
  30. for (int i = 0; i < PLC_COUNT; i++)
  31. {
  32. if (dicTwincatOpc.ContainsKey(_moList[i].NodeName))
  33. {
  34. Console.WriteLine($"id {i}, 重复变量:{_moList[i].NodeName} ;");
  35. continue;
  36. }
  37. switch (_moList[i].DataType)
  38. {
  39. case "double":
  40. dicTwincatOpc.Add(_moList[i].NodeName, new TwinCatAds<Double>(_moList[i]));
  41. break;
  42. case "int":
  43. dicTwincatOpc.Add(_moList[i].NodeName, new TwinCatAds<Int16>(_moList[i]));
  44. break;
  45. case "uint":
  46. dicTwincatOpc.Add(_moList[i].NodeName, new TwinCatAds<UInt16>(_moList[i]));
  47. break;
  48. case "dint":
  49. dicTwincatOpc.Add(_moList[i].NodeName, new TwinCatAds<Int64>(_moList[i]));
  50. break;
  51. case "word":
  52. dicTwincatOpc.Add(_moList[i].NodeName, new TwinCatAds<Int16>(_moList[i]));
  53. break;
  54. case "bool":
  55. dicTwincatOpc.Add(_moList[i].NodeName, new TwinCatAds<Boolean>(_moList[i]));
  56. break;
  57. case "byte":
  58. dicTwincatOpc.Add(_moList[i].NodeName, new TwinCatAds<Byte>(_moList[i]));
  59. break;
  60. case "real":
  61. dicTwincatOpc.Add(_moList[i].NodeName, new TwinCatAds<float>(_moList[i]));
  62. break;
  63. };
  64. }
  65. }
  66. catch(Exception ex)
  67. {
  68. LogHelper.log.Error(ex);
  69. }
  70. }
  71. public void Opc_disConnect()
  72. {
  73. try
  74. {
  75. foreach (var itemOpc in dicTwincatOpc)
  76. {
  77. itemOpc.Value.DisConnectOpc();
  78. }
  79. }
  80. catch (Exception ex)
  81. {
  82. LogHelper.log.Error(ex);
  83. }
  84. finally
  85. {
  86. dicTwincatOpc.Clear();
  87. }
  88. }
  89. public bool Opc_AddNotification(string command)
  90. {
  91. try
  92. {
  93. if (dicTwincatOpc.ContainsKey(command))
  94. {
  95. return dicTwincatOpc[command].CreateNotification();
  96. }
  97. }
  98. catch (Exception ex)
  99. {
  100. Console.WriteLine($"删除变量事件通知:{command} 错误:{ex.Message}");
  101. }
  102. return false;
  103. }
  104. public bool Opc_DelNotification(string command)
  105. {
  106. try
  107. {
  108. if (command.Equals("all"))
  109. {
  110. foreach (var itemOpc in dicTwincatOpc)
  111. itemOpc.Value.DeleteNotification();
  112. return true;
  113. }
  114. else if (dicTwincatOpc.ContainsKey(command))
  115. {
  116. return dicTwincatOpc[command].DeleteNotification();
  117. }
  118. }
  119. catch (Exception ex)
  120. {
  121. Console.WriteLine($"删除变量事件通知:{command} 错误:{ex.Message}");
  122. }
  123. return false;
  124. }
  125. public bool OpcConnectState()
  126. {
  127. try
  128. {
  129. foreach (var itemOpc in dicTwincatOpc)
  130. {
  131. itemOpc.Value.IsConnectOpc();
  132. }
  133. }
  134. catch (Exception ex)
  135. {
  136. LogHelper.log.Error(ex);
  137. }
  138. finally
  139. {
  140. dicTwincatOpc.Clear();
  141. }
  142. return false;
  143. }
  144. public void SetOpcSend(string nodeName, string value)
  145. {
  146. try
  147. {
  148. var DataType = from mo in _moList where mo.NodeName == nodeName select mo.DataType;
  149. if (string.IsNullOrWhiteSpace(DataType.FirstOrDefault()))
  150. throw new Exception($"no have this var :{nodeName}");
  151. dicTwincatOpc[nodeName].Set(value, DataType.FirstOrDefault());
  152. }
  153. catch(Exception ex)
  154. {
  155. LogHelper.log.Error(ex);
  156. }
  157. }
  158. public string GetOpcRecv(string nodeName, out Type _type)
  159. {
  160. try
  161. {
  162. return dicTwincatOpc[nodeName].Get(out _type).ToString();
  163. }
  164. catch(Exception ex)
  165. {
  166. LogHelper.log.Error(ex);
  167. }
  168. _type = null;
  169. return string.Empty;
  170. }
  171. }
  172. }