LeadOpcOperate.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. using NX_CommonClassLibrary;
  2. using NX_LogClassLibrary;
  3. using NX_ModelClassLibrary.CustomEnum;
  4. using NX_ModelClassLibrary.CustomEvent;
  5. using NX_ModelClassLibrary.OpcModel;
  6. using OPCAutomation;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Data;
  10. using System.Linq;
  11. using System.Text;
  12. using System.Threading.Tasks;
  13. namespace NX_OpcClassLibrary
  14. {
  15. /// <summary>
  16. /// 先导智能OPC初始化和OPCItem变量初始化
  17. /// Copyright20181102 (C) sunyalong
  18. /// 允许修改、添加满足自己项目的需要。
  19. /// 添加、修改后请详细注释。违者会强制删除不予采用。
  20. /// </summary>
  21. public class LeadOpcOperate
  22. {
  23. #region 单例模式
  24. /// <summary>
  25. /// 单例模式对象
  26. /// </summary>
  27. private static LeadOpcOperate _instance = null;
  28. private static readonly object lockObj = new object();
  29. /// <summary>
  30. /// 单例模式方法
  31. /// </summary>
  32. public static LeadOpcOperate Instance
  33. {
  34. get
  35. {
  36. if (_instance == null)
  37. {
  38. lock (lockObj)
  39. {
  40. if (_instance == null)
  41. {
  42. _instance = new LeadOpcOperate();
  43. }
  44. }
  45. }
  46. return _instance;
  47. }
  48. }
  49. #endregion
  50. #region OPCItem变量申明区(所有的OPCItem变量均放于此区域)
  51. /// <summary>
  52. /// OPC操作类对象
  53. /// </summary>
  54. private readonly LeadOPCServer OPC = new LeadOPCServer();
  55. /// <summary>
  56. /// OPC实体类对象集合
  57. /// </summary>
  58. public List<WcsOpcItemExtMd> listOpcItem = new List<WcsOpcItemExtMd>();
  59. /// <summary>
  60. /// 日志显示帮助类对象
  61. /// </summary>
  62. public ShowLogToFrmHelper showLogToFrm = new ShowLogToFrmHelper();
  63. /// <summary>
  64. /// 日志头部
  65. /// </summary>
  66. private readonly string LogHeadText = "OPC业务操作类 ==>> ";
  67. #endregion
  68. #region OPC初始化
  69. /// <summary>
  70. /// 初始化OPC服务器和OPCItem变量
  71. /// </summary>
  72. /// <param name="dt">OPC数据库配置表</param>
  73. /// <returns></returns>
  74. public bool OpcInit(List<WcsOpcItemMd> wcsOpcItemMdLst)
  75. {
  76. try
  77. {
  78. //枚举本地OPC服务器,
  79. //如果是西门子的OPC服务器一般用OPC.SimaticNET.1;
  80. //如果是KEPServerEx的服务器一般用KEPware.KEPServerEx.V4;
  81. List<string> Opclist = OPC.GetLocalServer();
  82. string OPCServerName = "KEPware.KEPServerEx.V4";
  83. foreach (string tempOpc in Opclist)
  84. {
  85. if (tempOpc == OPCServerName)
  86. {
  87. if (OPC.ConnectRemoteServer("", tempOpc))
  88. {
  89. if (!OPC.CreateGroup())
  90. {
  91. return false;
  92. }
  93. else
  94. {
  95. //从数据库配置表查询OPC名称、地址等信息并进行配置。
  96. int a = 1;
  97. foreach (WcsOpcItemMd md in wcsOpcItemMdLst)
  98. {
  99. WcsOpcItemExtMd opcItem = SetChildPropertiesToParent(md);
  100. OPCItem opcitem = OPC.OPCItems.AddItem(md.OpcItemPos, a);
  101. opcItem.opc_item = opcitem;
  102. listOpcItem.Add(opcItem);
  103. a++;
  104. }
  105. }
  106. }
  107. }
  108. }
  109. ShowLogToForm($"初始化OPC变量操作类成功!", true, LogTypeEnum.Run);
  110. return true;
  111. }
  112. catch (Exception ex)
  113. {
  114. ShowLogToForm($"初始化OPC变量操作类发生异常:【{ex.Message}】", true, LogTypeEnum.Err);
  115. return false;
  116. }
  117. }
  118. #endregion
  119. #region 父类属性值赋值到子类属性
  120. private WcsOpcItemExtMd SetChildPropertiesToParent(WcsOpcItemMd wcsOpcItemMd)
  121. {
  122. WcsOpcItemExtMd opc = new WcsOpcItemExtMd();
  123. var ParentType = typeof(WcsOpcItemMd);
  124. var Properties = ParentType.GetProperties();
  125. foreach (var Propertie in Properties)
  126. {
  127. if (Propertie.CanRead && Propertie.CanWrite)
  128. {
  129. Propertie.SetValue(opc, Propertie.GetValue(wcsOpcItemMd, null), null);
  130. }
  131. }
  132. return opc;
  133. }
  134. #endregion
  135. #region OPC 读、写
  136. /// <summary>
  137. /// 读取OPC变量值
  138. /// </summary>
  139. /// <param name="wcsOpcItemMd">opc变量对象</param>
  140. /// <returns></returns>
  141. public object ReadOpcItemValue(WcsOpcItemMd wcsOpcItemMd)
  142. {
  143. WcsOpcItemExtMd opc = listOpcItem.FirstOrDefault(x => x.OpcItemCode == wcsOpcItemMd.OpcItemCode && x.OpcGroupCode == wcsOpcItemMd.OpcGroupCode && x.PlcCode == wcsOpcItemMd.PlcCode && x.DevCode == wcsOpcItemMd.DevCode);
  144. if (opc == null)
  145. {
  146. return null;
  147. }
  148. else
  149. {
  150. return opc.opc_item.Value;
  151. }
  152. }
  153. /// <summary>
  154. /// 写入OPC变量值
  155. /// </summary>
  156. /// <param name="wcsOpcItemMd">opc变量对象</param>
  157. /// <param name="value">写入值</param>
  158. /// <returns></returns>
  159. public bool WriteOpcItemValue(WcsOpcItemMd wcsOpcItemMd, object value)
  160. {
  161. try
  162. {
  163. WcsOpcItemExtMd opc = listOpcItem.FirstOrDefault(x => x.OpcItemCode == wcsOpcItemMd.OpcItemCode && x.OpcGroupCode == wcsOpcItemMd.OpcGroupCode && x.PlcCode == wcsOpcItemMd.PlcCode && x.DevCode == wcsOpcItemMd.DevCode);
  164. if (opc == null)
  165. {
  166. return false;
  167. }
  168. else
  169. {
  170. opc.opc_item.Write(value);
  171. return true;
  172. }
  173. }
  174. catch
  175. {
  176. return false;
  177. }
  178. }
  179. #endregion
  180. #region 桌面显示Log、记录log到文本
  181. /// <summary>
  182. /// 桌面显示Log、记录log到文本
  183. /// </summary>
  184. /// <param name="msg">log内容</param>
  185. /// <param name="isShowFormFlag">是否输出到桌面日志端。true:输出桌面;false:只记录文本</param>
  186. /// <param name="logTypeEnum">日志类型枚举</param>
  187. private void ShowLogToForm(string msg, bool isShowFormFlag, LogTypeEnum logTypeEnum)
  188. {
  189. if (isShowFormFlag)
  190. {
  191. showLogToFrm.ShowLog(new ShowLogToFrmEventArgs(msg));
  192. }
  193. LogHelper.WriteLog(LogHeadText + msg, logTypeEnum);
  194. }
  195. #endregion
  196. }
  197. }