123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287 |
- using System;
- using System.Collections.Generic;
- using System.Reflection;
- using ProjectBase.Data.Logs;
- using ProjectBase.Data.Redis;
- using SIMDP.Model;
- using Opc.Ua;
- using OpcUaHelper;
- using Opc.Ua.Client.Controls;
- using Newtonsoft.Json.Linq;
- namespace SIMDP.Device
- {
- public class PlcOpcUA : AbstractBaseDevice
- {
- #region 数据定义
- protected OpcUaClient m_OpcUaClient;
- protected string Address = "";
- protected Dictionary<string, OpcDataPointInfo> allPoints = new Dictionary<string, OpcDataPointInfo>();
- protected List<OpcDataPointInfo> monitorPoints = new List<OpcDataPointInfo>();
- #endregion
- #region TYPE定义
- public enum VarType
- {
- Bit = 1,
- Byte = 2,
- Int = 3,
- Word = 4,
- DInt = 5,
- DWord = 6,
- Real = 7,
- LReal = 8,
- String = 9
- }
- public class OpcUaConfig
- {
- public OpcUaConfig(string json)
- {//"opc.tcp://DESKTOP-PA2AC31:53530/OPCUA/SimulationServer"
- JObject jObject = JObject.Parse(json);
- JToken token = jObject["Address"];
- if (token != null)
- Address = (string)token;
- token = jObject["Timer"];
- if (token != null)
- TIMER = Convert.ToDouble(token.ToString());
- }
- public string Address { get; set; }
- public double TIMER { get; set; } = 1000;
- }
- public class OpcDataPointInfo
- {
- // 节点配置信息
- public MoDataPoint mo;
- public string Tag { get; set; }//标签 例子:"ns=3;i=1008"
- public VarType TYPE { get; set; }
- public object value = null;
- public MethodInfo proc = null;
- public OpcDataPointInfo(MoDataPoint _mo)
- {
- mo = _mo;
- Tag = mo.DataPointSource;
- TYPE = getVarTypeByDataPointType(mo.DataPointType);
- }
- VarType getVarTypeByDataPointType(long type)
- {//常用的1、5、9 1bool 5double 9string
- if (type == 1)
- return VarType.Bit;
- else if (type == 2)
- return VarType.Byte;
- else if (type == 3)
- return VarType.Int;
- else if (type == 4)
- return VarType.Word;
- else if (type == 5)
- return VarType.DInt;
- else if (type == 6)
- return VarType.DWord;
- else if (type == 7)
- return VarType.Real;
- else if (type == 8)
- return VarType.LReal;
- else if (type == 9)
- return VarType.String;
- else
- return VarType.Bit;
- }
- }
- #endregion
- #region 接口实现
- public override void Start(MoPlcInfo _plcInfo, List<MoDataPoint> _dataPoints)
- {
- plcInfo = _plcInfo;
- OpcUaConfig plcConfig = new OpcUaConfig(plcInfo.LinkConfig);
- m_OpcUaClient = new OpcUaClient();
- m_OpcUaClient.UserIdentity = new UserIdentity(new AnonymousIdentityToken());
- Address = plcConfig.Address;
- timer.Interval = plcConfig.TIMER;
- timer.Elapsed += timer_Elapsed;
- timer.AutoReset = false;
- // PLC未连接状态
- SetRedisPlcDisConnect();
- // 创建PLC数据点表
- CreateDataPointList(_dataPoints);
- // 初始化自定义函数
- actionStart();
- // 启动时尝试连接一次设备
- Connect();
- // 启动定时器
- timerStopped = false;
- timer.Start();
- }
- public override void Stop()
- {
- actionStop();
- timerStopped = true;
- }
- public override bool isConnected()
- {
- return m_OpcUaClient.Connected;
- }
- protected override bool Connect()
- {
- try
- {
- m_OpcUaClient.ConnectServer(Address);
- }
- catch (Exception ex)
- {
- ClientUtils.HandleException("Connected Failed", ex);
- }
- if (m_OpcUaClient.Connected)
- {
- LogHelper.log.Info("已连接到OPC:" + m_OpcUaClient.OpcUaName);
- SetRedisPlcConnected();
- actionConnected();
- return true;
- }
- else
- {
- LogHelper.log.Warn("未能连接到OPC,状态:" + m_OpcUaClient.Connected.ToString());
- SetRedisPlcDisConnect();
- return false;
- }
- }
- protected override void Disconnect()
- {
- if (m_OpcUaClient != null)
- m_OpcUaClient.Disconnect();
- SetRedisPlcDisConnect();
- }
- void CreateDataPointList(List<MoDataPoint> dataPoints)
- {
- if (redis == null)
- redis = new RedisHelper();
- Type t = this.GetType();
- foreach (MoDataPoint dataPoint in dataPoints)
- {
- OpcDataPointInfo dataPointInfo = new OpcDataPointInfo(dataPoint);
- allPoints.Add(dataPoint.DataPointName, dataPointInfo);
- redis.SetString(dataPoint.DataPointId.ToString() + dataPoint.DataPointName, "");
- // 不再按照信号组类型区分是否为监控节点,只按照是否有执行逻辑
- if (dataPoint.DataProc.Trim() != "")
- {
- dataPointInfo.proc = t.GetMethod(dataPoint.DataProc);
- monitorPoints.Add(dataPointInfo);
- }
- }
- }
- #endregion
- #region 读写操作
- public bool Read(string dataPointName, out object value)
- {
- value = null;
- if (!allPoints.ContainsKey(dataPointName))
- {
- return false;
- }
- OpcDataPointInfo p = allPoints[dataPointName];
- return Read(p, out value);
- }
- public bool Read(OpcDataPointInfo p, out object value)
- {
- switch (p.TYPE)
- {
- case VarType.Bit:
- value = Convert.ToBoolean(m_OpcUaClient.ReadNode<bool>(p.Tag));
- break;
- case VarType.Byte:
- value = Convert.ToByte(m_OpcUaClient.ReadNode<byte>(p.Tag));
- break;
- case VarType.Int:
- value = Convert.ToInt16(m_OpcUaClient.ReadNode<short>(p.Tag));
- break;
- case VarType.Word:
- value = Convert.ToUInt16(m_OpcUaClient.ReadNode<ushort>(p.Tag));
- break;
- case VarType.DInt:
- value = Convert.ToInt32(m_OpcUaClient.ReadNode<int>(p.Tag));
- break;
- case VarType.DWord:
- value = Convert.ToUInt32(m_OpcUaClient.ReadNode<uint>(p.Tag));
- break;
- case VarType.Real:
- value = Convert.ToSingle(m_OpcUaClient.ReadNode<float>(p.Tag));
- break;
- case VarType.LReal:
- value = Convert.ToDouble(m_OpcUaClient.ReadNode<double>(p.Tag));
- break;
- case VarType.String:
- value = m_OpcUaClient.ReadNode<string>(p.Tag).ToString();
- break;
- default:
- value = null;
- return false;
- }
- UpdataRedisDataPoint(p.mo, value.ToString());
- return true;
- }
- public bool Write(string dataPointName, object value)
- {
- if (!allPoints.ContainsKey(dataPointName))
- {
- return false;
- }
- OpcDataPointInfo p = allPoints[dataPointName];
- switch (p.TYPE)
- {
- case VarType.Bit:
- m_OpcUaClient.WriteNode(p.Tag, Convert.ToBoolean(value));
- break;
- case VarType.Byte:
- m_OpcUaClient.WriteNode(p.Tag, Convert.ToByte(value));
- break;
- case VarType.Int:
- m_OpcUaClient.WriteNode(p.Tag, Convert.ToInt16(value));
- break;
- case VarType.Word:
- m_OpcUaClient.WriteNode(p.Tag, Convert.ToUInt16(value));
- break;
- case VarType.DInt:
- m_OpcUaClient.WriteNode(p.Tag, Convert.ToInt32(value));
- break;
- case VarType.DWord:
- m_OpcUaClient.WriteNode(p.Tag, Convert.ToUInt32(value));
- break;
- case VarType.Real:
- m_OpcUaClient.WriteNode(p.Tag, Convert.ToSingle(value));
- break;
- case VarType.LReal:
- m_OpcUaClient.WriteNode(p.Tag, Convert.ToDouble(value));
- break;
- case VarType.String:
- m_OpcUaClient.WriteNode(p.Tag, value.ToString());
- break;
- default:
- return false;
- }
- //LogHelper.log.Debug($"PLC信号({dataPointName})写入值:{value.ToString()}");
- UpdataRedisDataPoint(p.mo, value.ToString());
- return true;
- }
- #endregion
- }
- }
|