123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204 |
- using NX_CommonClassLibrary;
- using NX_LogClassLibrary;
- using NX_ModelClassLibrary.CustomEnum;
- using NX_ModelClassLibrary.CustomEvent;
- using NX_ModelClassLibrary.OpcModel;
- using OPCAutomation;
- using System;
- using System.Collections.Generic;
- using System.Data;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace NX_OpcClassLibrary
- {
- /// <summary>
- /// 先导智能OPC初始化和OPCItem变量初始化
- /// Copyright20181102 (C) sunyalong
- /// 允许修改、添加满足自己项目的需要。
- /// 添加、修改后请详细注释。违者会强制删除不予采用。
- /// </summary>
- public class LeadOpcOperate
- {
- #region 单例模式
- /// <summary>
- /// 单例模式对象
- /// </summary>
- private static LeadOpcOperate _instance = null;
- private static readonly object lockObj = new object();
- /// <summary>
- /// 单例模式方法
- /// </summary>
- public static LeadOpcOperate Instance
- {
- get
- {
- if (_instance == null)
- {
- lock (lockObj)
- {
- if (_instance == null)
- {
- _instance = new LeadOpcOperate();
- }
- }
- }
- return _instance;
- }
- }
- #endregion
- #region OPCItem变量申明区(所有的OPCItem变量均放于此区域)
- /// <summary>
- /// OPC操作类对象
- /// </summary>
- private readonly LeadOPCServer OPC = new LeadOPCServer();
- /// <summary>
- /// OPC实体类对象集合
- /// </summary>
- public List<WcsOpcItemExtMd> listOpcItem = new List<WcsOpcItemExtMd>();
- /// <summary>
- /// 日志显示帮助类对象
- /// </summary>
- public ShowLogToFrmHelper showLogToFrm = new ShowLogToFrmHelper();
- /// <summary>
- /// 日志头部
- /// </summary>
- private readonly string LogHeadText = "OPC业务操作类 ==>> ";
- #endregion
- #region OPC初始化
- /// <summary>
- /// 初始化OPC服务器和OPCItem变量
- /// </summary>
- /// <param name="dt">OPC数据库配置表</param>
- /// <returns></returns>
- public bool OpcInit(List<WcsOpcItemMd> wcsOpcItemMdLst)
- {
- try
- {
- //枚举本地OPC服务器,
- //如果是西门子的OPC服务器一般用OPC.SimaticNET.1;
- //如果是KEPServerEx的服务器一般用KEPware.KEPServerEx.V4;
- List<string> Opclist = OPC.GetLocalServer();
- string OPCServerName = "KEPware.KEPServerEx.V4";
- foreach (string tempOpc in Opclist)
- {
- if (tempOpc == OPCServerName)
- {
- if (OPC.ConnectRemoteServer("", tempOpc))
- {
- if (!OPC.CreateGroup())
- {
- return false;
- }
- else
- {
- //从数据库配置表查询OPC名称、地址等信息并进行配置。
- int a = 1;
- foreach (WcsOpcItemMd md in wcsOpcItemMdLst)
- {
- WcsOpcItemExtMd opcItem = SetChildPropertiesToParent(md);
- OPCItem opcitem = OPC.OPCItems.AddItem(md.OpcItemPos, a);
- opcItem.opc_item = opcitem;
- listOpcItem.Add(opcItem);
- a++;
- }
- }
- }
- }
- }
- ShowLogToForm($"初始化OPC变量操作类成功!", true, LogTypeEnum.Run);
- return true;
- }
- catch (Exception ex)
- {
- ShowLogToForm($"初始化OPC变量操作类发生异常:【{ex.Message}】", true, LogTypeEnum.Err);
- return false;
- }
- }
- #endregion
- #region 父类属性值赋值到子类属性
- private WcsOpcItemExtMd SetChildPropertiesToParent(WcsOpcItemMd wcsOpcItemMd)
- {
- WcsOpcItemExtMd opc = new WcsOpcItemExtMd();
- var ParentType = typeof(WcsOpcItemMd);
- var Properties = ParentType.GetProperties();
- foreach (var Propertie in Properties)
- {
- if (Propertie.CanRead && Propertie.CanWrite)
- {
- Propertie.SetValue(opc, Propertie.GetValue(wcsOpcItemMd, null), null);
- }
- }
- return opc;
- }
- #endregion
- #region OPC 读、写
- /// <summary>
- /// 读取OPC变量值
- /// </summary>
- /// <param name="wcsOpcItemMd">opc变量对象</param>
- /// <returns></returns>
- public object ReadOpcItemValue(WcsOpcItemMd wcsOpcItemMd)
- {
- WcsOpcItemExtMd opc = listOpcItem.FirstOrDefault(x => x.OpcItemCode == wcsOpcItemMd.OpcItemCode && x.OpcGroupCode == wcsOpcItemMd.OpcGroupCode && x.PlcCode == wcsOpcItemMd.PlcCode && x.DevCode == wcsOpcItemMd.DevCode);
- if (opc == null)
- {
- return null;
- }
- else
- {
- return opc.opc_item.Value;
- }
- }
- /// <summary>
- /// 写入OPC变量值
- /// </summary>
- /// <param name="wcsOpcItemMd">opc变量对象</param>
- /// <param name="value">写入值</param>
- /// <returns></returns>
- public bool WriteOpcItemValue(WcsOpcItemMd wcsOpcItemMd, object value)
- {
- try
- {
- WcsOpcItemExtMd opc = listOpcItem.FirstOrDefault(x => x.OpcItemCode == wcsOpcItemMd.OpcItemCode && x.OpcGroupCode == wcsOpcItemMd.OpcGroupCode && x.PlcCode == wcsOpcItemMd.PlcCode && x.DevCode == wcsOpcItemMd.DevCode);
- if (opc == null)
- {
- return false;
- }
- else
- {
- opc.opc_item.Write(value);
- return true;
- }
- }
- catch
- {
- return false;
- }
- }
- #endregion
- #region 桌面显示Log、记录log到文本
- /// <summary>
- /// 桌面显示Log、记录log到文本
- /// </summary>
- /// <param name="msg">log内容</param>
- /// <param name="isShowFormFlag">是否输出到桌面日志端。true:输出桌面;false:只记录文本</param>
- /// <param name="logTypeEnum">日志类型枚举</param>
- private void ShowLogToForm(string msg, bool isShowFormFlag, LogTypeEnum logTypeEnum)
- {
- if (isShowFormFlag)
- {
- showLogToFrm.ShowLog(new ShowLogToFrmEventArgs(msg));
- }
- LogHelper.WriteLog(LogHeadText + msg, logTypeEnum);
- }
- #endregion
- }
- }
|