PlcOpcDA.cs 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. using System;
  2. using System.Collections.Generic;
  3. using ProjectBase.Data.Logs;
  4. using SIMDP.Model;
  5. using System.Timers;
  6. using OPCAutomation;
  7. using System.Reflection;
  8. namespace SIMDP.Device
  9. {
  10. public class PlcOpcDA : AbstractBaseDevice
  11. {
  12. #region 数据定义
  13. /// <summary>
  14. /// OPCServer Object
  15. /// </summary>
  16. OPCServer KepServer;
  17. /// <summary>
  18. /// OPCGroup Object
  19. /// </summary>
  20. OPCGroup KepGroupAll;
  21. /// <summary>
  22. /// OPCGroup Object
  23. /// </summary>
  24. OPCGroup KepGroupSubscribe;
  25. /// <summary>
  26. /// OPCItems Object
  27. /// 客户端句柄
  28. /// </summary>
  29. int itmHandleClient = 1234;
  30. /// <summary>
  31. /// OPC点表
  32. /// </summary>
  33. List<MoDataPoint> opcPointList;
  34. /// <summary>
  35. /// 数据节点字典 key-变量名称, value-节点信息
  36. /// </summary>
  37. protected Dictionary<string, OpcDataPointInfo> allPoints = new Dictionary<string, OpcDataPointInfo>();
  38. /// <summary>
  39. /// 监控节点列表
  40. /// </summary>
  41. protected Dictionary<int, OpcDataPointInfo> monitorPoints = new Dictionary<int, OpcDataPointInfo>();
  42. #endregion
  43. #region TYPE定义
  44. public class OpcDataPointInfo
  45. {
  46. // 节点配置信息
  47. public MoDataPoint mo;
  48. public OPCItem item;
  49. public object value = null;
  50. public MethodInfo proc = null;
  51. public OpcDataPointInfo(MoDataPoint _mo, OPCItem _item)
  52. {
  53. mo = _mo; item = _item;
  54. }
  55. }
  56. #endregion
  57. #region 接口实现
  58. public override void Start(MoPlcInfo _plcInfo, List<MoDataPoint> _dataPoints)
  59. {
  60. KepServer = new OPCServer();
  61. plcInfo = _plcInfo;
  62. opcPointList = _dataPoints;
  63. // PLC未连接状态
  64. SetRedisPlcDisConnect();
  65. // 初始化自定义函数
  66. actionStart();
  67. // 启动时尝试连接一次设备
  68. Connect();
  69. // 启动定时器
  70. timer.Interval = 1000;
  71. timerStopped = false;
  72. timer.Start();
  73. }
  74. public override bool isConnected()
  75. {
  76. return (KepServer.ServerState == (int)OPCServerState.OPCRunning);
  77. }
  78. protected override bool Connect()
  79. {
  80. KepServer.Connect(plcInfo.LinkConfig);
  81. if (KepServer.ServerState == (int)OPCServerState.OPCRunning)
  82. {
  83. LogHelper.log.Info("已连接到OPC:" + KepServer.ServerName);
  84. if (CreateGroup())
  85. SetRedisPlcConnected();
  86. else
  87. KepServer.Disconnect();
  88. }
  89. else //这里你可以根据返回的状态来自定义显示信息,请查看自动化接口API文档
  90. {
  91. LogHelper.log.Warn("未能连接到OPC,状态:" + KepServer.ServerState.ToString());
  92. }
  93. return isConnected();
  94. }
  95. protected override void Disconnect()
  96. {
  97. if (KepServer != null)
  98. KepServer.Disconnect();
  99. SetRedisPlcDisConnect();
  100. }
  101. /// <summary>
  102. /// 创建组
  103. /// </summary>
  104. public bool CreateGroup()
  105. {
  106. try
  107. {
  108. OPCGroups KepGroups = ((IOPCAutoServer)KepServer).OPCGroups;
  109. KepGroups.DefaultGroupDeadband = 0;
  110. KepGroups.DefaultGroupIsActive = true;
  111. KepGroups.DefaultGroupUpdateRate = 500;
  112. KepGroupAll = KepGroups.Add("SIMDPOPCGROUP"); // 所有节点组(可读写)
  113. KepGroupAll.IsSubscribed = false;
  114. KepGroupSubscribe = KepGroups.Add("SIMDPSUBSCRIBE"); // 监控节点组(变更订阅)
  115. KepGroupSubscribe.IsSubscribed = true;
  116. // 添加节点到OPCGROUP
  117. allPoints.Clear();
  118. monitorPoints.Clear();
  119. Type t = this.GetType();
  120. OPCItems KepItems = KepGroupAll.OPCItems;
  121. OPCItems KepItemsSubscribe = KepGroupSubscribe.OPCItems;
  122. foreach (var mo in opcPointList)
  123. {
  124. OPCItem KepItem = KepItems.AddItem(mo.DataPointSource, itmHandleClient);
  125. OpcDataPointInfo point = new OpcDataPointInfo(mo, KepItem);
  126. allPoints.Add(mo.DataPointName, point);
  127. //if (BLLFactory<BlDataGroup>.Instance.IsSignalGroupById(mo.DataPointGroupId))
  128. // 不再按照信号组类型区分是否为监控节点,只按照是否有执行逻辑
  129. if (mo.DataProc.Trim() != "")
  130. {
  131. OPCItem monitorItem = KepItemsSubscribe.AddItem(mo.DataPointSource, itmHandleClient);
  132. OpcDataPointInfo monitorPoint = new OpcDataPointInfo(mo, monitorItem);
  133. monitorPoint.proc = t.GetMethod(mo.DataProc);
  134. monitorPoints.Add(itmHandleClient, monitorPoint);
  135. }
  136. itmHandleClient++;
  137. }
  138. KepGroupSubscribe.DataChange += new DIOPCGroupEvent_DataChangeEventHandler(KepGroup_DataChange);
  139. }
  140. catch (Exception err)
  141. {
  142. LogHelper.log.Error("创建组出现错误:" + err.Message);
  143. return false;
  144. }
  145. return true;
  146. }
  147. #endregion
  148. #region 读写操作
  149. /// <summary>
  150. /// 每当项数据有变化时执行的事件
  151. /// </summary>
  152. /// <param name="TransactionID">处理ID</param>
  153. /// <param name="NumItems">项个数</param>
  154. /// <param name="ClientHandles">项客户端句柄</param>
  155. /// <param name="ItemValues">TAG值</param>
  156. /// <param name="Qualities">品质</param>
  157. /// <param name="TimeStamps">时间戳</param>
  158. void KepGroup_DataChange(int TransactionID, int NumItems, ref Array ClientHandles, ref Array ItemValues, ref Array Qualities, ref Array TimeStamps)
  159. {
  160. for (int i = 1; i <= NumItems; i++)
  161. {
  162. object value = ItemValues.GetValue(i);
  163. if (value == null)
  164. continue;
  165. int iHandle = (int)ClientHandles.GetValue(i);
  166. OpcDataPointInfo point;
  167. if (!monitorPoints.TryGetValue(iHandle, out point))
  168. continue;
  169. // 处理信号
  170. UpdataRedisDataPoint(point.mo, value.ToString(), true);
  171. if (point.proc != null)
  172. point.proc.Invoke(this, new object[] { value });
  173. }
  174. }
  175. /// <summary>
  176. /// 写入OPC变量
  177. /// </summary>
  178. public bool Write(string dataPointName, object value)
  179. {
  180. OpcDataPointInfo pointInfo;
  181. if (allPoints.TryGetValue(dataPointName, out pointInfo))
  182. {
  183. pointInfo.item.Write(value);
  184. UpdataRedisDataPoint(pointInfo.mo, value.ToString(), true);
  185. return true;
  186. }
  187. else
  188. {
  189. LogHelper.log.Error("写入OPC变量错误,未定义变量名:" + dataPointName);
  190. return false;
  191. }
  192. }
  193. /// <summary>
  194. /// 读取OPC变量
  195. /// </summary>
  196. public bool Read(string dataPointName, out object value)
  197. {
  198. OpcDataPointInfo pointInfo;
  199. if (allPoints.TryGetValue(dataPointName, out pointInfo))
  200. {
  201. object quality;
  202. object timestamp;
  203. pointInfo.item.Read((short)OPCDataSource.OPCDevice, out value, out quality, out timestamp);
  204. UpdataRedisDataPoint(pointInfo.mo, value.ToString(), true);
  205. return true;
  206. }
  207. else
  208. {
  209. LogHelper.log.Error("读取OPC变量错误,未定义变量名:" + dataPointName);
  210. value = null;
  211. return false;
  212. }
  213. }
  214. #endregion
  215. }
  216. }