首次提交:添加src文件夹代码
This commit is contained in:
830
Cowain.Bake.Communication/PLC/PLCBase.cs
Normal file
830
Cowain.Bake.Communication/PLC/PLCBase.cs
Normal file
@@ -0,0 +1,830 @@
|
||||
using Cowain.Bake.Common.Core;
|
||||
using Cowain.Bake.Communication.Interface;
|
||||
using Cowain.Bake.Model.Entity;
|
||||
using HslCommunication;
|
||||
using HslCommunication.Core;
|
||||
using Newtonsoft.Json;
|
||||
using OpcUaHelper;
|
||||
using Prism.Mvvm;
|
||||
using Prism.Services.Dialogs;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.Diagnostics;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Text;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Unity;
|
||||
using Cowain.Bake.Common.Models;
|
||||
using Cowain.Bake.Model.Models;
|
||||
using Cowain.Bake.Common;
|
||||
using Opc.Ua;
|
||||
using Cowain.Bake.BLL;
|
||||
using Cowain.Bake.Common.Enums;
|
||||
|
||||
namespace Cowain.Bake.Communication.PLC
|
||||
{
|
||||
public abstract class PLCBase : BindableBase, IPLCDevice
|
||||
{
|
||||
protected IUnityContainer _unityContainer;
|
||||
protected IDialogService _dialogService;
|
||||
public virtual IReadWriteNet PLC { get; set; }
|
||||
public OpcUaClient OPC { get; set; }
|
||||
public IByteTransform ByteTransform { get; set; }
|
||||
public virtual string IpAddress { get; set; } = "127.0.0.1";
|
||||
public virtual int Port { get; set; }
|
||||
public virtual byte Slot { get; set; }
|
||||
public virtual string ConnectString { get; set; }
|
||||
CancellationTokenSource cts = new CancellationTokenSource();
|
||||
protected PLCBase(IUnityContainer unityContainer, IDialogService dialogService)
|
||||
{
|
||||
_unityContainer = unityContainer;
|
||||
_dialogService = dialogService;
|
||||
}
|
||||
private int readUseTime;
|
||||
public int ReadUseTime
|
||||
{
|
||||
get { return readUseTime; }
|
||||
set { SetProperty(ref readUseTime, value); }
|
||||
}
|
||||
private bool isConnect;
|
||||
public bool IsConnect
|
||||
{
|
||||
get { return isConnect; }
|
||||
set { SetProperty(ref isConnect, value); }
|
||||
}
|
||||
public int Id { get; set; }
|
||||
public string Name { get; set; }
|
||||
public string DeviceName { get; set; }
|
||||
public Dictionary<int, Variable> VariableDic { get; set; } = new Dictionary<int, Variable>();
|
||||
public ObservableCollection<Variable> VariableList { get; set; } = new ObservableCollection<Variable>(); //数组遍历器
|
||||
public List<TagEntity> TagList { get; set; } = new List<TagEntity>();
|
||||
public List<StorageArea> Storages { get; set; } = new List<StorageArea>(); //1个PLC一个,首地址及所对应的标签信息
|
||||
|
||||
public abstract void Close();
|
||||
public abstract void Connect();
|
||||
public abstract void GetJsonParam(string param);
|
||||
|
||||
bool? lastConnect = null;
|
||||
public virtual void StartRead()
|
||||
{
|
||||
Task.Run(async () =>
|
||||
{
|
||||
await Task.Delay(2000); //等待OPC初始化完成
|
||||
Stopwatch sw = new Stopwatch();
|
||||
while (true)
|
||||
{
|
||||
if (Global.AppExit)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
if (!IsConnect)
|
||||
{
|
||||
if (VariableList != null && VariableList.Count > 0)
|
||||
{
|
||||
foreach (var tag in VariableList)
|
||||
{
|
||||
tag.Quality = false;
|
||||
}
|
||||
}
|
||||
Connect();
|
||||
}
|
||||
else
|
||||
{
|
||||
if (IsConnect)
|
||||
{
|
||||
sw.Restart();
|
||||
ReadStorageArea();
|
||||
sw.Stop();
|
||||
ReadUseTime = (int)sw.ElapsedMilliseconds;
|
||||
sw.Reset();
|
||||
if (ReadUseTime > 3000)
|
||||
{
|
||||
LogHelper.Instance.Warn($"{ConnectString}:刷新所有数据时间:" + ReadUseTime + "ms");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
catch(Exception ex)
|
||||
{
|
||||
LogHelper.Instance.Error($"全局读取数据失败:{ex.Message}");
|
||||
}
|
||||
|
||||
if (lastConnect != IsConnect)
|
||||
{
|
||||
lastConnect = IsConnect;
|
||||
//_unityContainer.Resolve<BasicInfoViewModel>().PLCStatus = IsConnect;
|
||||
//_unityContainer.Resolve<DeviceConfigService>().UpdateStatus(this.Name, IsConnect);
|
||||
_unityContainer.Resolve<DeviceConfigService>().UpdateStatus(this.Id, IsConnect);
|
||||
}
|
||||
|
||||
// 触发 GC
|
||||
GC.Collect();
|
||||
GC.WaitForPendingFinalizers();
|
||||
await Task.Delay(1000);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public virtual void Heartbeat()
|
||||
{
|
||||
Task.Run(async () =>
|
||||
{
|
||||
while (!cts.Token.IsCancellationRequested)
|
||||
{
|
||||
if (Global.AppExit)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
await Task.Delay(Global.HEARTBEAT_INTERVAL_TIME);
|
||||
|
||||
if (null == PLC || !IsConnect)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
var node = (from secondaryList in Storages
|
||||
from item in secondaryList.VariableList
|
||||
where item.VarDesc == "心跳"
|
||||
select item).FirstOrDefault();
|
||||
if (null == node)
|
||||
{
|
||||
LogHelper.Instance.Warn("查询心跳节点失败");
|
||||
continue;
|
||||
}
|
||||
|
||||
OperateResult result = Write<Int16>(node.Address + node.VarName, 1); //只写一个心跳就行了
|
||||
if (!result.IsSuccess)
|
||||
{
|
||||
//LogHelper.Instance.Warn($"DeviceId:{Storages[0].DeviceId}:写心跳节点失败");
|
||||
}
|
||||
}
|
||||
}, cts.Token);
|
||||
}
|
||||
public virtual int GetReadMaxLength(Variable variable)
|
||||
{
|
||||
return 101;
|
||||
}
|
||||
public virtual void AddressSplit(TagEntity tag)
|
||||
{
|
||||
//按modbus tcp解析 格式:x=3;0
|
||||
string[] ss = tag.Address.Split(';');
|
||||
if (ss.Length == 2)
|
||||
{
|
||||
VariableDic[(int)tag.Id].AddressType = ss[0];
|
||||
VariableDic[(int)tag.Id].Address = ss[1];
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 赋值类型长度
|
||||
/// </summary>
|
||||
/// <param name="tag"></param>
|
||||
public virtual void SetReadLength(TagEntity tag)
|
||||
{
|
||||
string varType = tag.VarType;
|
||||
Match m = Regex.Match(tag.VarType, @"(?<=\[)([0-9]+)(?=\])", RegexOptions.Singleline);
|
||||
if (m.Success)
|
||||
{
|
||||
//说明是数组[]
|
||||
//VariableDic[tag.VarName].ReadLength = Convert.ToInt32(m.Value); 这是个BUG
|
||||
VariableDic[(int)tag.Id].ArrayLength = Convert.ToInt32(m.Value);
|
||||
Match mType = Regex.Match(tag.VarType, @"(.*)(?=\[)", RegexOptions.Singleline);
|
||||
varType = mType.Value;
|
||||
}
|
||||
|
||||
switch (varType)
|
||||
{
|
||||
case "BOOL":
|
||||
case "BYTE":
|
||||
case "INT16":
|
||||
case "UINT16":
|
||||
VariableDic[(int)tag.Id].ReadLength = 1;
|
||||
break;
|
||||
case "INT32":
|
||||
case "UINT32":
|
||||
case "FLOAT":
|
||||
VariableDic[(int)tag.Id].ReadLength = 2;
|
||||
break;
|
||||
default:
|
||||
LogHelper.Instance.Error("不支持的数据类型:" + tag.VarType);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
public virtual void AnalysisAddress()
|
||||
{
|
||||
foreach (var tag in TagList)
|
||||
{
|
||||
AddressSplit(tag);
|
||||
SetReadLength(tag);
|
||||
}
|
||||
}
|
||||
|
||||
public virtual void GetStorageArea()
|
||||
{
|
||||
Storages.Clear();
|
||||
var query = (from v in VariableList //query = List<IGrouping<string, Variable>> //按地址类型分组:如x=3一组
|
||||
orderby Convert.ToInt16(v.Address), v.ReadLength
|
||||
group v by v.AddressType into a
|
||||
where a.Count() > 0
|
||||
orderby a.Key
|
||||
select a).ToList(); //modbus地址类型分组
|
||||
/* modbus地址分类
|
||||
0x01 读取线圈的操作,
|
||||
0x02 读取离散的操作,
|
||||
0x03 读取寄存器的值,
|
||||
0x04 读取输入寄存器的值
|
||||
0x05 写一个线圈操作,
|
||||
0x06 写一个寄存器值,
|
||||
0x0F 批量写线圈操作,
|
||||
0x10 批量写寄存器值,
|
||||
0x16 掩码写入的寄存器(大部分的modbus设备不支持)
|
||||
*/
|
||||
int k = query.Count;
|
||||
foreach (var g in query)
|
||||
{
|
||||
int k1 = g.Count();
|
||||
var first = g.First();//first:Variable,g:Dictionary<string,List<Variable>>
|
||||
var totalLengthQuery1 = (from a in g //分块读数据
|
||||
group a by (Convert.ToInt16(a.Address) - Convert.ToInt16(first.Address) + a.ReadLength) / GetReadMaxLength(a)).ToList();
|
||||
|
||||
var totalLengthQuery = (from a in g //分块读数据,100个数据分一组,在地址类型上,再按100为一组,分第二次组
|
||||
group a by (Convert.ToInt16(a.Address) - Convert.ToInt16(first.Address) + a.ReadLength) / GetReadMaxLength(a) into b
|
||||
select new
|
||||
{
|
||||
VarList = b, //Regex.Replace(a.VarType, @"\[.*\]", string.Empty)
|
||||
Id = b.Key, //(Convert.ToInt16(a.Address) - Convert.ToInt16(first.Address) + a.ReadLength) / GetReadMaxLength(a)
|
||||
AddressType = b.First().AddressType, //AddressType类型是Variable
|
||||
StartAddress = b.First().Address,
|
||||
GroupReadLength = (Convert.ToInt16(b.Last().Address) - Convert.ToInt16(b.First().Address) + b.Last().ReadLength)
|
||||
}).ToList();
|
||||
|
||||
totalLengthQuery.ForEach(x =>
|
||||
{
|
||||
List<Variable> variables = x.VarList.ToList();
|
||||
foreach (var variable in variables)
|
||||
{
|
||||
//计算offset,字节偏移
|
||||
variable.Offset = (Convert.ToInt16(variable.Address) - Convert.ToInt16(x.StartAddress)) * 2;
|
||||
}
|
||||
Storages.Add(new StorageArea()
|
||||
{
|
||||
//Id = x.Id,
|
||||
AddressType = x.AddressType,
|
||||
StartAddress = x.StartAddress,
|
||||
Len = x.GroupReadLength,
|
||||
VariableList = variables
|
||||
});
|
||||
});
|
||||
}
|
||||
GetReadAddress();
|
||||
}
|
||||
|
||||
public virtual void GetReadAddress()
|
||||
{
|
||||
foreach (var storage in Storages)
|
||||
{
|
||||
storage.ReadAddress = storage.AddressType + storage.StartAddress;
|
||||
}
|
||||
}
|
||||
|
||||
public virtual OperateResult<T> Read<T>(string tag)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
public virtual OperateResult<T> GetValue<T>(string paramName)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
public virtual OperateResult<T> GetValue<T>(string paramName, int machineId, int number = 0)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
public virtual Variable GetVariable(string paramName, int machineId, int number = 0)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public virtual OperateResult Write<T>(string paramName, int machineId, int number, T data)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
public virtual void ReadStorageArea()
|
||||
{
|
||||
try
|
||||
{
|
||||
foreach (var item in Storages)
|
||||
{
|
||||
bool readError = false;
|
||||
for (int i = 0; i < Global.MAX_READS; i++)
|
||||
{
|
||||
var read = PLC.Read(item.ReadAddress, (ushort)item.Len);
|
||||
if (read.IsSuccess)
|
||||
{
|
||||
IsConnect = true;
|
||||
readError = false;
|
||||
//读成功
|
||||
if (item.VariableList != null)
|
||||
{
|
||||
foreach (var variable in item.VariableList)
|
||||
{
|
||||
AnalyseData(read.Content, variable);
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
else
|
||||
{
|
||||
foreach (var val in item.VariableList)
|
||||
{
|
||||
val.Quality = false;
|
||||
}
|
||||
LogHelper.Instance.Error($"读错误,,ErrorCode:{read.ErrorCode},失败:" + read.Message);
|
||||
}
|
||||
readError = true;
|
||||
Thread.Sleep(30);
|
||||
}
|
||||
Thread.Sleep(30);
|
||||
if (readError)
|
||||
{
|
||||
IsConnect = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
catch(Exception ex)
|
||||
{
|
||||
IsConnect = false;
|
||||
LogHelper.Instance.Error(ex.Message);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public virtual void AnalyseData(byte[] data, Variable variable)
|
||||
{
|
||||
if (data != null && data.Length > 0)
|
||||
{
|
||||
if (variable.ArrayLength > 1)
|
||||
{
|
||||
//说明是数组[]
|
||||
//variable.ArrayLength = Convert.ToInt32(m.Value);
|
||||
|
||||
switch (variable.VarType)
|
||||
{
|
||||
case "BOOL":
|
||||
variable.CurValue = ByteTransform.TransBool(data, variable.Offset);
|
||||
variable.Quality = true;
|
||||
break;
|
||||
case "BYTE":
|
||||
variable.CurValue = ByteTransform.TransByte(data, variable.Offset, variable.ArrayLength);
|
||||
variable.Quality = true;
|
||||
break;
|
||||
case "INT16":
|
||||
variable.CurValue = Array.ConvertAll(ByteTransform.TransInt16(data, variable.Offset, variable.ArrayLength), ele => ele.ToString());
|
||||
variable.Quality = true;
|
||||
break;
|
||||
case "UINT16":
|
||||
variable.CurValue = Array.ConvertAll(ByteTransform.TransUInt16(data, variable.Offset, variable.ArrayLength), ele => ele.ToString());
|
||||
variable.Quality = true;
|
||||
break;
|
||||
case "INT32":
|
||||
variable.CurValue = Array.ConvertAll(ByteTransform.TransInt32(data, variable.Offset, variable.ArrayLength), ele => ele.ToString());
|
||||
variable.Quality = true;
|
||||
break;
|
||||
case "UINT32":
|
||||
variable.Value = Array.ConvertAll(ByteTransform.TransUInt32(data, variable.Offset, variable.ArrayLength), ele => ele.ToString());
|
||||
variable.Quality = true;
|
||||
break;
|
||||
case "FLOAT":
|
||||
variable.Value = Array.ConvertAll(ByteTransform.TransSingle(data, variable.Offset, variable.ArrayLength), ele => ele.ToString());
|
||||
variable.Quality = true;
|
||||
break;
|
||||
//case "STR":
|
||||
// variable.CurValue = ByteTransform.TransString(data, variable.Offset, variable.ArrayLength, Encoding.Default);
|
||||
// variable.Quality = true;
|
||||
// break;
|
||||
default:
|
||||
LogHelper.Instance.Error("不支持的数据类型:" + variable.VarType);
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
//值类型tostring不会装箱
|
||||
switch (variable.VarType)
|
||||
{
|
||||
case "BOOL":
|
||||
variable.CurValue = ByteTransform.TransBool(data, 1).ToString();
|
||||
variable.Quality = true;
|
||||
break;
|
||||
case "BYTE":
|
||||
variable.CurValue = ByteTransform.TransByte(data, variable.Offset).ToString();
|
||||
variable.Quality = true;
|
||||
break;
|
||||
case "INT16":
|
||||
variable.CurValue = ByteTransform.TransInt16(data, variable.Offset).ToString();
|
||||
variable.Quality = true;
|
||||
break;
|
||||
case "UINT16":
|
||||
data[0] = 33;
|
||||
data[1] = 251;
|
||||
variable.CurValue = ByteTransform.TransUInt16(data, variable.Offset).ToString();
|
||||
variable.Quality = true;
|
||||
break;
|
||||
case "INT32":
|
||||
variable.CurValue = ByteTransform.TransInt32(data, variable.Offset).ToString();
|
||||
variable.Quality = true;
|
||||
break;
|
||||
case "UINT32":
|
||||
variable.CurValue = ByteTransform.TransUInt32(data, variable.Offset).ToString();
|
||||
variable.Quality = true;
|
||||
break;
|
||||
case "FLOAT":
|
||||
float f = ByteTransform.TransSingle(data, variable.Offset);
|
||||
variable.CurValue = ByteTransform.TransSingle(data, variable.Offset).ToString();
|
||||
variable.Quality = true;
|
||||
break;
|
||||
default:
|
||||
LogHelper.Instance.Error("不支持的数据类型:" + variable.VarType);
|
||||
break;
|
||||
}
|
||||
//variable.Value = new string[] { variable.CurValue };
|
||||
}
|
||||
|
||||
|
||||
if (variable.TrigEnable)
|
||||
{
|
||||
dynamic d = JsonConvert.DeserializeObject<dynamic>(variable.TrigJson);
|
||||
string fName = d.Func;
|
||||
string vals = d.Value;
|
||||
string key = d.Key;
|
||||
string desc = d.Desc;
|
||||
string serviceName = d.Service;
|
||||
if (string.IsNullOrEmpty(serviceName))
|
||||
{
|
||||
serviceName = "TrigerService";
|
||||
}
|
||||
if (!string.IsNullOrEmpty(variable.OldValue.ToString()))
|
||||
{
|
||||
|
||||
if (string.Equals(variable.OldValue, variable.CurValue))
|
||||
{
|
||||
|
||||
if (vals == variable.CurValue.ToString())
|
||||
{
|
||||
//异步执行
|
||||
//this.TrigServiceAsync(serviceName, fName, VariableDic[tag.VarName].FuncJson);
|
||||
//int scriptId = variable.ScriptId;
|
||||
//if (scriptId > 0)
|
||||
//{
|
||||
// var sm = _unityContainer.Resolve<IScriptManage>();
|
||||
// var script = sm.ScriptList.Where(x => x.Id == scriptId).FirstOrDefault();
|
||||
// if (script != null)
|
||||
// {
|
||||
// string funcJson = variable.Json;
|
||||
// if (!string.IsNullOrEmpty(funcJson))
|
||||
// {
|
||||
// TrigScriptAsync(script, variable.Json);
|
||||
// }
|
||||
// }
|
||||
// else
|
||||
// {
|
||||
// LogHelper.Instance.Error($"脚本执行异常:没找到脚本-{scriptId}");
|
||||
// }
|
||||
//}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
variable.OldValue = variable.CurValue;
|
||||
}
|
||||
}
|
||||
|
||||
//private void CopyBytes(byte[] data, Variable variable, int len)
|
||||
//{
|
||||
// variable.CurValue = new byte[len];
|
||||
// Buffer.BlockCopy(data, variable.Offset, variable.CurValue, 0, len);
|
||||
//}
|
||||
public virtual OperateResult Writes(string[] tags, object[] values, int maxCount = Global.MAX_READS)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
public virtual List<DataValue> Reads(NodeId[] nodeIds)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
public abstract OperateResult Write<T>(string address, T data, int maxCount = 5);
|
||||
public virtual OperateResult Write<T>(dynamic plc, string address, T data, int maxCount = 5)
|
||||
{
|
||||
var res = Write<T>(plc, address, data);
|
||||
for (int i = 0; i < maxCount; i++)
|
||||
{
|
||||
if (res.IsSuccess)
|
||||
{
|
||||
break;
|
||||
}
|
||||
Thread.Sleep(15);
|
||||
res = Write<T>(plc, address, data);
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
public virtual OperateResult Write<T>(dynamic plc, string address, T data)
|
||||
{
|
||||
if (!IsConnect)
|
||||
{
|
||||
return new OperateResult("未连接PLC");
|
||||
}
|
||||
|
||||
Type type = typeof(T);
|
||||
OperateResult resultdata = null;
|
||||
try
|
||||
{
|
||||
if (type == typeof(short))
|
||||
{
|
||||
if (data is short one)
|
||||
{
|
||||
resultdata = plc.Write(address, one);
|
||||
}
|
||||
}
|
||||
else if (type == typeof(ushort))
|
||||
{
|
||||
if (data is ushort one)
|
||||
{
|
||||
resultdata = plc.Write(address, one);
|
||||
}
|
||||
}
|
||||
else if (type == typeof(int))
|
||||
{
|
||||
if (data is int one)
|
||||
{
|
||||
resultdata = plc.Write(address, one);
|
||||
}
|
||||
}
|
||||
else if (type == typeof(uint))
|
||||
{
|
||||
if (data is uint one)
|
||||
{
|
||||
resultdata = plc.Write(address, one);
|
||||
}
|
||||
}
|
||||
else if (type == typeof(long))
|
||||
{
|
||||
if (data is long one)
|
||||
{
|
||||
resultdata = plc.Write(address, one);
|
||||
}
|
||||
}
|
||||
else if (type == typeof(ulong))
|
||||
{
|
||||
if (data is ulong one)
|
||||
{
|
||||
resultdata = plc.Write(address, one);
|
||||
}
|
||||
}
|
||||
else if (type == typeof(float))
|
||||
{
|
||||
if (data is float one)
|
||||
{
|
||||
resultdata = plc.Write(address, one);
|
||||
}
|
||||
}
|
||||
else if (type == typeof(double))
|
||||
{
|
||||
if (data is double one)
|
||||
{
|
||||
resultdata = plc.Write(address, one);
|
||||
}
|
||||
}
|
||||
else if (type == typeof(bool))
|
||||
{
|
||||
if (data is bool state)
|
||||
{
|
||||
resultdata = plc.Write(address, state);
|
||||
}
|
||||
}
|
||||
else if (type == typeof(short[]) || type == typeof(List<short>))
|
||||
{
|
||||
if (data is short[] arr)
|
||||
{
|
||||
resultdata = plc.Write(address, arr);
|
||||
}
|
||||
else if (data is List<short> list)
|
||||
{
|
||||
//list
|
||||
resultdata = plc.Write(address, list.ToArray());
|
||||
}
|
||||
}
|
||||
else if (type == typeof(ushort[]) || type == typeof(List<ushort>))
|
||||
{
|
||||
if (data is ushort[] arr)
|
||||
{
|
||||
resultdata = plc.Write(address, arr);
|
||||
}
|
||||
else if (data is List<ushort> list)
|
||||
{
|
||||
//list
|
||||
resultdata = plc.Write(address, list.ToArray());
|
||||
}
|
||||
}
|
||||
else if (type == typeof(int[]) || type == typeof(List<int>))
|
||||
{
|
||||
if (data is int[] arr)
|
||||
{
|
||||
resultdata = plc.Write(address, arr);
|
||||
}
|
||||
else if (data is List<int> list)
|
||||
{
|
||||
//list
|
||||
resultdata = plc.Write(address, list.ToArray());
|
||||
}
|
||||
}
|
||||
else if (type == typeof(uint[]) || type == typeof(List<uint>))
|
||||
{
|
||||
if (data is uint[] arr)
|
||||
{
|
||||
resultdata = plc.Write(address, arr);
|
||||
}
|
||||
else if (data is List<uint> list)
|
||||
{
|
||||
//list
|
||||
resultdata = plc.Write(address, list.ToArray());
|
||||
}
|
||||
}
|
||||
else if (type == typeof(long[]) || type == typeof(List<long>))
|
||||
{
|
||||
if (data is long[] arr)
|
||||
{
|
||||
resultdata = plc.Write(address, arr);
|
||||
}
|
||||
else if (data is List<long> list)
|
||||
{
|
||||
//list
|
||||
resultdata = plc.Write(address, list.ToArray());
|
||||
}
|
||||
}
|
||||
else if (type == typeof(ulong[]) || type == typeof(List<ulong>))
|
||||
{
|
||||
if (data is ulong[] arr)
|
||||
{
|
||||
resultdata = plc.Write(address, arr);
|
||||
}
|
||||
else if (data is List<ulong> list)
|
||||
{
|
||||
//list
|
||||
resultdata = plc.Write(address, list.ToArray());
|
||||
}
|
||||
}
|
||||
else if (type == typeof(float[]) || type == typeof(List<float>))
|
||||
{
|
||||
if (data is float[] arr)
|
||||
{
|
||||
resultdata = plc.Write(address, arr);
|
||||
}
|
||||
else if (data is List<float> list)
|
||||
{
|
||||
//list
|
||||
resultdata = plc.Write(address, list.ToArray());
|
||||
}
|
||||
}
|
||||
else if (type == typeof(double[]) || type == typeof(List<double>))
|
||||
{
|
||||
if (data is double[] arr)
|
||||
{
|
||||
resultdata = plc.Write(address, arr);
|
||||
}
|
||||
else if (data is List<double> list)
|
||||
{
|
||||
//list
|
||||
resultdata = plc.Write(address, list.ToArray());
|
||||
}
|
||||
}
|
||||
else if (type == typeof(bool[]) || type == typeof(List<bool>))
|
||||
{
|
||||
if (data is bool[] arrb)
|
||||
{
|
||||
resultdata = plc.Write(address, arrb);
|
||||
}
|
||||
else if (data is List<bool> listb)
|
||||
{
|
||||
resultdata = plc.Write(address, listb.ToArray());
|
||||
}
|
||||
}
|
||||
else if (type == typeof(byte[]) || type == typeof(List<byte>))
|
||||
{
|
||||
if (data is byte[] arrby)
|
||||
{
|
||||
resultdata = plc.Write(address, arrby);
|
||||
}
|
||||
else if (data is List<byte> listb)
|
||||
{
|
||||
resultdata = plc.Write(address, listb.ToArray());
|
||||
}
|
||||
}
|
||||
else if (type == typeof(string))
|
||||
{
|
||||
if (data is string str)
|
||||
{
|
||||
resultdata = plc.Write(address, str, Encoding.ASCII);
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
IsConnect = false;
|
||||
LogHelper.Instance.Error($"写PLC数据失败:{address},{ex}");
|
||||
}
|
||||
if (!resultdata.IsSuccess)
|
||||
{
|
||||
LogHelper.Instance.Error("写PLC数据失败:" + address);
|
||||
}
|
||||
return resultdata;
|
||||
}
|
||||
|
||||
|
||||
public bool CheckBytesEquals(byte[] a, byte[] b)
|
||||
{
|
||||
if (a == null || b == null) return false;
|
||||
if (a.Length != b.Length) return false;
|
||||
for (int i = 0; i < a.Length; i++)
|
||||
{
|
||||
if (a[i] != b[i])
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
//public Task TrigScriptAsync(ScriptEntity script, string funcJson)
|
||||
//{
|
||||
// //return Task.Run(() =>
|
||||
// //{
|
||||
// // return RunSrcipt(script, funcJson);
|
||||
// //});
|
||||
//}
|
||||
|
||||
//private OperateResult RunSrcipt(ScriptEntity script, string funcJson)
|
||||
//{
|
||||
// Stopwatch watch = new Stopwatch();
|
||||
// watch.Start();
|
||||
// try
|
||||
// {
|
||||
// //OperateResult result = ScriptEngine.RunScript(_unityContainer, funcJson, script);
|
||||
// //LogHelper.Instance.Info($"脚本【{script.Name}】执行用时:" + watch.ElapsedMilliseconds + "ms");
|
||||
// return null;
|
||||
// }
|
||||
// catch (Exception ex)
|
||||
// {
|
||||
// LogHelper.Instance.Error("脚本执行异常", ex);
|
||||
// return null;
|
||||
// }
|
||||
//}
|
||||
|
||||
//public Task TrigServiceAsync(string serviceName, string fName, string funcJson)
|
||||
//{
|
||||
// return Task.Run(() =>
|
||||
// {
|
||||
// TrigService(serviceName, fName, funcJson);
|
||||
// });
|
||||
//}
|
||||
|
||||
public void TrigService(string serviceName, string fName, string funcJson)
|
||||
{
|
||||
Assembly asm = Assembly.GetExecutingAssembly();
|
||||
Type type = asm.GetType($"Cowain.Bake.Communication.Service.{serviceName}");
|
||||
var ts = _unityContainer.Resolve(type);
|
||||
object[] parameters = new object[1];
|
||||
parameters[0] = funcJson;
|
||||
try
|
||||
{
|
||||
MethodInfo mt = ts.GetType().GetMethod(fName);
|
||||
if (mt != null)
|
||||
{
|
||||
mt.Invoke(ts, parameters);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
LogHelper.Instance.Error($"TrigService:{ex}");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
33
Cowain.Bake.Communication/PLC/PLCBlockingCollection.cs
Normal file
33
Cowain.Bake.Communication/PLC/PLCBlockingCollection.cs
Normal file
@@ -0,0 +1,33 @@
|
||||
using Cowain.Bake.Model.Models;
|
||||
using Opc.Ua;
|
||||
using System;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Unity;
|
||||
|
||||
namespace Cowain.Bake.Communication.PLC
|
||||
{
|
||||
public struct BlockData
|
||||
{
|
||||
public DataValue Data;
|
||||
public Variable Node;
|
||||
public BlockData(DataValue data, Variable node)
|
||||
{
|
||||
Data = data;
|
||||
Node = node;
|
||||
}
|
||||
}
|
||||
|
||||
public class PLCBlockingCollection
|
||||
{
|
||||
IUnityContainer _unityContainer;
|
||||
public BlockingCollection<BlockData> MsgBlock = new BlockingCollection<BlockData>();
|
||||
public PLCBlockingCollection(IUnityContainer unityContainer)
|
||||
{
|
||||
_unityContainer = unityContainer;
|
||||
}
|
||||
}
|
||||
}
|
||||
96
Cowain.Bake.Communication/PLC/PLCManage.cs
Normal file
96
Cowain.Bake.Communication/PLC/PLCManage.cs
Normal file
@@ -0,0 +1,96 @@
|
||||
|
||||
using Cowain.Bake.BLL;
|
||||
using Cowain.Bake.Common;
|
||||
using Cowain.Bake.Common.Core;
|
||||
using Cowain.Bake.Common.Enums;
|
||||
using Cowain.Bake.Common.Models;
|
||||
using Cowain.Bake.Communication.Interface;
|
||||
using Cowain.Bake.Model;
|
||||
using Cowain.Bake.Model.Models;
|
||||
using Prism.Ioc;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Reflection;
|
||||
using System.Threading.Tasks;
|
||||
using Unity;
|
||||
|
||||
namespace Cowain.Bake.Communication.PLC
|
||||
{
|
||||
|
||||
public class PLCManage
|
||||
{
|
||||
IUnityContainer _unityContainer;
|
||||
private List<TDeviceConfig> plcList;
|
||||
public PLCManage(IUnityContainer unityContainer)
|
||||
{
|
||||
_unityContainer = unityContainer;
|
||||
plcList = _unityContainer.Resolve<DeviceConfigService>().GetConfig(EDeviceType.PLC);
|
||||
foreach (var item in plcList)
|
||||
{
|
||||
Assembly asm = Assembly.GetExecutingAssembly(); //获得当前程序的绝对路径
|
||||
Type type = asm.GetType(MyPath.PLC + item.DriverName);
|
||||
unityContainer.RegisterSingleton(typeof(IPLCDevice), type, item.Name);
|
||||
var plc = _unityContainer.Resolve<IPLCDevice>(item.Name); //几个PLC,就实例化几个
|
||||
plc.Id = item.Id;
|
||||
plc.Name = item.Name;
|
||||
plc.DeviceName = item.DriverName;
|
||||
plc.GetJsonParam(item.Json);
|
||||
//2023.3.1修改,不需要读取StorageList,驱动自己分批读取
|
||||
var plcTagList = _unityContainer.Resolve<TagListService>().GetTagList(item.Id); //获取PLC的标签和节点
|
||||
if (plcTagList != null && plcTagList.Count > 0)
|
||||
{
|
||||
int q = 0;
|
||||
plcTagList.ForEach((x) =>
|
||||
{
|
||||
plc.TagList.Add(x);
|
||||
plc.VariableList.Add(new Variable()
|
||||
{
|
||||
Id = (int)x.Id,
|
||||
Quality = false,
|
||||
VarType = x.VarType,
|
||||
ArrayLength = x.ArrayLen,
|
||||
StationId = x.StationId,
|
||||
ParamName = x.ParamName,
|
||||
Number = x.Number,
|
||||
Index = q++,
|
||||
TrigEnable = x.TirgEnable,
|
||||
TrigJson = x.TrigJson,
|
||||
OperType = x.OperType.Value,
|
||||
Json = x.Json,
|
||||
VarDesc = x.VarDesc,
|
||||
VarName = x.VarName,
|
||||
Address = x.Address,
|
||||
TagType = x.TagType
|
||||
});
|
||||
});
|
||||
|
||||
foreach (var v in plc.VariableList)
|
||||
{
|
||||
if (!plc.VariableDic.ContainsKey(v.Id))
|
||||
{
|
||||
plc.VariableDic.Add(v.Id, v);
|
||||
}
|
||||
else
|
||||
{
|
||||
LogHelper.Instance.Error($"PLC有相同的节点,Node=[{v.VarName}]");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (item.Enable)
|
||||
{
|
||||
plc.AnalysisAddress(); //把地址拆开,并计算长度
|
||||
plc.GetStorageArea(); //获取存储区域
|
||||
//plc.Connect();
|
||||
//plc.StartRead();
|
||||
Task t = new Task(() => //var t = Task.Run(async delegate
|
||||
{
|
||||
plc.Connect();
|
||||
});
|
||||
plc.StartRead();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
109
Cowain.Bake.Communication/PLC/PLC_ModbusTcp.cs
Normal file
109
Cowain.Bake.Communication/PLC/PLC_ModbusTcp.cs
Normal file
@@ -0,0 +1,109 @@
|
||||
using Cowain.Bake.BLL;
|
||||
using Cowain.Bake.Common;
|
||||
using Cowain.Bake.Common.Core;
|
||||
using HslCommunication;
|
||||
using HslCommunication.Core;
|
||||
using HslCommunication.ModBus;
|
||||
using Newtonsoft.Json;
|
||||
using Prism.Services.Dialogs;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.Diagnostics;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Reflection;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Unity;
|
||||
using Hsl = HslCommunication.Profinet.Omron;
|
||||
|
||||
|
||||
namespace Cowain.Bake.Communication.PLC
|
||||
{
|
||||
public class PLC_ModbusTcp : PLCBase
|
||||
{
|
||||
public override int Port { get; set; } = 502;
|
||||
private ModbusTcpNet plc;
|
||||
|
||||
public PLC_ModbusTcp(IUnityContainer unityContainer, IDialogService dialogService) : base(unityContainer, dialogService)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public override void Close()
|
||||
{
|
||||
IsConnect = false;
|
||||
try
|
||||
{
|
||||
plc?.ConnectClose();
|
||||
plc?.Dispose();
|
||||
plc = null;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
LogHelper.Instance.Error($"关闭PLC连接失败:{ex}");
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
public override void Connect()
|
||||
{
|
||||
Close();
|
||||
if (plc == null)
|
||||
{
|
||||
plc = new ModbusTcpNet();
|
||||
this.PLC = plc;
|
||||
this.ByteTransform = plc.ByteTransform;
|
||||
plc.ConnectTimeOut = 3000;
|
||||
}
|
||||
try
|
||||
{
|
||||
plc.IpAddress = this.IpAddress;
|
||||
plc.Port = this.Port;
|
||||
plc.ByteTransform.DataFormat = HslCommunication.Core.DataFormat.CDAB;
|
||||
var result = plc.ConnectServer();
|
||||
if (result.IsSuccess)
|
||||
{
|
||||
IsConnect = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
IsConnect = false;
|
||||
LogHelper.Instance.Error("连接PLC失败,请检查网络和参数");
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
IsConnect = false;
|
||||
LogHelper.Instance.Error($"连接PLC失败{ex}");
|
||||
}
|
||||
}
|
||||
|
||||
public override void GetJsonParam(string param)
|
||||
{
|
||||
dynamic d = JsonConvert.DeserializeObject<dynamic>(param);
|
||||
this.IpAddress = d.Ip;
|
||||
this.Port = d.Port;
|
||||
}
|
||||
public override void GetReadAddress()
|
||||
{
|
||||
foreach (var storage in Storages)
|
||||
{
|
||||
storage.ReadAddress = storage.AddressType + ";" + storage.StartAddress;
|
||||
}
|
||||
}
|
||||
|
||||
public override OperateResult Write<T>(string address, T data, int maxCount = 5)
|
||||
{
|
||||
return Write<T>(plc, address, data, maxCount);
|
||||
}
|
||||
|
||||
public override OperateResult Writes(string[] tags, object[] values, int maxCount = Global.MAX_READS)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
126
Cowain.Bake.Communication/PLC/PLC_OmronFins.cs
Normal file
126
Cowain.Bake.Communication/PLC/PLC_OmronFins.cs
Normal file
@@ -0,0 +1,126 @@
|
||||
using Cowain.Bake.BLL;
|
||||
using Cowain.Bake.Communication.Interface;
|
||||
using Cowain.Bake.Common.Models;
|
||||
using HslCommunication;
|
||||
using Newtonsoft.Json;
|
||||
using Prism.Services.Dialogs;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.Diagnostics;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Reflection;
|
||||
using System.Text;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Unity;
|
||||
using Hsl = HslCommunication.Profinet.Omron;
|
||||
using Cowain.Bake.Model.Entity;
|
||||
using Cowain.Bake.Common.Core;
|
||||
using Cowain.Bake.Common;
|
||||
|
||||
namespace Cowain.Bake.Communication.PLC
|
||||
{
|
||||
public class PLC_OmronFins : PLCBase
|
||||
{
|
||||
public override int Port { get; set; } = 9600;
|
||||
private Hsl.OmronFinsNet plc;
|
||||
|
||||
public PLC_OmronFins(IUnityContainer unityContainer, IDialogService dialogService) : base(unityContainer, dialogService)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public override void Close()
|
||||
{
|
||||
IsConnect = false;
|
||||
try
|
||||
{
|
||||
plc?.ConnectClose();
|
||||
plc?.Dispose();
|
||||
plc = null;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
LogHelper.Instance.Error($"关闭PLC连接失败{ex}");
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
public override void Connect()
|
||||
{
|
||||
Close();
|
||||
if (plc == null)
|
||||
{
|
||||
//HslCommunication.Profinet.OpenProtocol aa = new HslCommunication.Profinet.OpenProtocol();
|
||||
plc = new Hsl.OmronFinsNet();
|
||||
this.PLC = plc;
|
||||
this.ByteTransform = plc.ByteTransform;
|
||||
plc.ConnectTimeOut = 3000;
|
||||
//plc.SetPersistentConnection();
|
||||
}
|
||||
try
|
||||
{
|
||||
plc.IpAddress = this.IpAddress;
|
||||
plc.Port = this.Port;
|
||||
plc.SA1 = 0;
|
||||
plc.DA2 = this.Slot;
|
||||
plc.ByteTransform.DataFormat = HslCommunication.Core.DataFormat.CDAB;
|
||||
var result = plc.ConnectServer();
|
||||
if (result.IsSuccess)
|
||||
{
|
||||
IsConnect = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
IsConnect = false;
|
||||
LogHelper.Instance.Error("连接PLC失败,请检查网络和参数");
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
IsConnect = false;
|
||||
LogHelper.Instance.Error($"连接PLC失败{ex}");
|
||||
}
|
||||
}
|
||||
|
||||
public override void GetJsonParam(string param)
|
||||
{
|
||||
dynamic d = JsonConvert.DeserializeObject<dynamic>(param);
|
||||
this.IpAddress = d.Ip;
|
||||
this.Port = d.Port;
|
||||
this.Slot = d.Slot;
|
||||
}
|
||||
public override void AddressSplit(TagEntity tag)
|
||||
{
|
||||
//格式:D100,C100,W100,H100
|
||||
Match m = Regex.Match(tag.Address, @"[a-zA-Z]{1}[0-9]+", RegexOptions.Singleline);
|
||||
if (m.Success)
|
||||
{
|
||||
VariableDic[(int)tag.Id].AddressType = m.Value.Substring(0, 1);
|
||||
VariableDic[(int)tag.Id].Address = m.Value.Substring(1);
|
||||
}
|
||||
}
|
||||
|
||||
public override void GetReadAddress()
|
||||
{
|
||||
foreach (var storage in Storages)
|
||||
{
|
||||
storage.ReadAddress = storage.AddressType + storage.StartAddress;
|
||||
}
|
||||
}
|
||||
|
||||
public override OperateResult Write<T>(string address, T data, int maxCount = Global.MAX_READS)
|
||||
{
|
||||
return Write<T>(plc, address, data, maxCount);
|
||||
}
|
||||
|
||||
public override OperateResult Writes(string[] tags, object[] values, int maxCount = Global.MAX_READS)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
682
Cowain.Bake.Communication/PLC/PLC_OpcUaClient.cs
Normal file
682
Cowain.Bake.Communication/PLC/PLC_OpcUaClient.cs
Normal file
@@ -0,0 +1,682 @@
|
||||
using Cowain.Bake.BLL;
|
||||
using Cowain.Bake.Common;
|
||||
using Cowain.Bake.Common.Core;
|
||||
using Cowain.Bake.Common.Enums;
|
||||
using Cowain.Bake.Common.Interface;
|
||||
using Cowain.Bake.Model.Entity;
|
||||
using Cowain.Bake.Model.Models;
|
||||
using HslCommunication;
|
||||
using Newtonsoft.Json;
|
||||
using Opc.Ua;
|
||||
using Opc.Ua.Client;
|
||||
using OpcUaHelper;
|
||||
using Prism.Services.Dialogs;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Unity;
|
||||
|
||||
|
||||
namespace Cowain.Bake.Communication.PLC
|
||||
{
|
||||
public class PLC_OpcUaClient : PLCBase
|
||||
{
|
||||
readonly static object _objLock = new object();
|
||||
readonly CancellationTokenSource cts = new CancellationTokenSource();
|
||||
bool isSubscribeNodes = false;
|
||||
public OpcUaClient opcUaClient { get; set; }
|
||||
ITrigService _trigService { get; set; }
|
||||
public PLC_OpcUaClient(IUnityContainer unityContainer, IDialogService dialogService) : base(unityContainer, dialogService)
|
||||
{
|
||||
opcUaClient = new OpcUaClient();
|
||||
OPC = opcUaClient;
|
||||
|
||||
opcUaClient.ConnectComplete += OpcUaClient_ConnectComplete;
|
||||
opcUaClient.KeepAliveComplete += OpcUaClient_KeepAliveComplete;
|
||||
opcUaClient.ReconnectStarting += OpcUaClient_ReconnectStarting;
|
||||
opcUaClient.ReconnectComplete += OpcUaClient_ReconnectComplete;
|
||||
opcUaClient.OpcStatusChange += OpcUaClient_OpcStatusChange;
|
||||
_trigService = _unityContainer.Resolve<ITrigService>(); //MachinePLCService
|
||||
//Start();
|
||||
}
|
||||
|
||||
~PLC_OpcUaClient()
|
||||
{
|
||||
Close();
|
||||
Stop();
|
||||
}
|
||||
public void Start()
|
||||
{
|
||||
Task.Run(async () =>
|
||||
{
|
||||
while (!cts.Token.IsCancellationRequested)
|
||||
{
|
||||
if (Global.AppExit)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
await Task.Delay(Global.HEARTBEAT_INTERVAL_TIME);
|
||||
|
||||
if (null == opcUaClient || !opcUaClient.Connected || !IsConnect)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
var node = (from secondaryList in Storages
|
||||
from item in secondaryList.VariableList
|
||||
where item.ParamName == EStoveSignal.Heartbeat.ToString()
|
||||
orderby item.Number
|
||||
select item).FirstOrDefault();
|
||||
if (null == node)
|
||||
{
|
||||
LogHelper.Instance.Warn("查询心跳节点失败");
|
||||
continue;
|
||||
}
|
||||
|
||||
OperateResult result = Write<UInt16>(node.Address + node.VarName, 1);
|
||||
if (!result.IsSuccess)
|
||||
{
|
||||
LogHelper.Instance.Warn($"MachineId:{Storages[0].StationId}:写心跳节点失败");
|
||||
}
|
||||
}
|
||||
}, cts.Token);
|
||||
}
|
||||
|
||||
public void Stop()
|
||||
{
|
||||
cts.Cancel();
|
||||
}
|
||||
/// <summary>
|
||||
/// 连接服务器结束后马上浏览根节点
|
||||
/// </summary>
|
||||
/// <param name="sender"></param>
|
||||
/// <param name="e"></param>
|
||||
private void OpcUaClient_ConnectComplete(object sender, EventArgs e)
|
||||
{
|
||||
try
|
||||
{
|
||||
opcUaClient = (OpcUaClient)sender; //2.重连不成功
|
||||
OPC = opcUaClient;
|
||||
IsConnect = opcUaClient.Connected;
|
||||
|
||||
if (!isSubscribeNodes
|
||||
&& IsConnect
|
||||
&& !Global.AppExit)
|
||||
{
|
||||
opcUaClient.RemoveAllSubscription();
|
||||
isSubscribeNodes = true;
|
||||
//SubscribeNodes(); //订阅节点
|
||||
SubscriptionNodes();
|
||||
CheckAddress(); //add by lsm 20260203
|
||||
LogHelper.Instance.Fatal($"{ConnectString},订阅节点");
|
||||
}
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
IsConnect = false;
|
||||
LogHelper.Instance.Error($"订阅失败!,{ConnectString},{exception}");
|
||||
}
|
||||
}
|
||||
|
||||
void CheckAddress()
|
||||
{
|
||||
foreach (var storage in Storages)
|
||||
{
|
||||
foreach (var item in storage.OPCNodes)
|
||||
{
|
||||
DataValue values1 = opcUaClient.ReadNode(item);
|
||||
if (values1 == null || null == values1.Value
|
||||
|| null == values1.WrappedValue.Value
|
||||
|| !StatusCode.IsGood(values1.StatusCode))
|
||||
{
|
||||
LogHelper.Instance.Debug($"{ConnectString},读取失败:{item}");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void OpcUaClient_OpcStatusChange(object sender, OpcUaStatusEventArgs e)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (e.Error) //1.重连不成功
|
||||
{
|
||||
IsConnect = false;
|
||||
LogHelper.Instance.Warn($"OPC UA客户端重连不成功,{e.Text}");
|
||||
}
|
||||
|
||||
if (!Regex.IsMatch(e.Text, "Connected"))
|
||||
{
|
||||
LogHelper.Instance.Info($"本OPC UA客户端的终极事件,{IpAddress},{e.Text}");
|
||||
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
LogHelper.Instance.Error($"OpcUaClient_OpcStatusChange:" + ex.Message);
|
||||
//throw;
|
||||
}
|
||||
|
||||
//if (Regex.IsMatch(e.Text, "Reconnecting")) //验证无效
|
||||
//{
|
||||
// opcUaClient.Session.Reconnect();
|
||||
//}
|
||||
//LogHelper.Instance.Info("本OPC UA客户端的终极事件,当客户端的状态变更都会触发,包括了连接,重连,断开,状态激活:" + e.ToString());
|
||||
}
|
||||
|
||||
private void OpcUaClient_ReconnectComplete(object sender, EventArgs e)
|
||||
{
|
||||
LogHelper.Instance.Info("重新连接到服务器的时候触发");
|
||||
}
|
||||
|
||||
private void OpcUaClient_ReconnectStarting(object sender, EventArgs e)
|
||||
{
|
||||
LogHelper.Instance.Info($"开始重新连接到服务器的时候触发,{IpAddress}");
|
||||
}
|
||||
|
||||
private void OpcUaClient_KeepAliveComplete(object sender, EventArgs e)
|
||||
{
|
||||
//LogHelper.Instance.Debug("ua客户端每隔5秒会与服务器进行通讯验证,每次验证都会触发该方法");
|
||||
}
|
||||
|
||||
public async override void Connect()
|
||||
{
|
||||
try
|
||||
{
|
||||
if (null != opcUaClient.Session)
|
||||
{
|
||||
//如果session没问题,能关掉,为什么要重连呢,230606
|
||||
Close();
|
||||
}
|
||||
//var timeouttask = Task.Delay(3000);
|
||||
//var completedTask = await Task.WhenAny(opcUaClient.ConnectServer(ConnectString), timeouttask);
|
||||
opcUaClient.UserIdentity = new UserIdentity(new AnonymousIdentityToken());
|
||||
await opcUaClient.ConnectServer(ConnectString);
|
||||
opcUaClient.ReconnectPeriod = 20000; //OPC UA 客户端在与服务器断开连接后尝试重新连接的时间间隔,是一个表示时间间隔的数值,以毫秒为单位
|
||||
if (null != opcUaClient.Session)
|
||||
{
|
||||
opcUaClient.Session.OperationTimeout = 5000; // 执行操作时的超时时间。它指定了客户端等待服务器响应的最长时间,以毫秒为单位
|
||||
}
|
||||
LogHelper.Instance.Fatal($"连接OPCUA服务器,{ConnectString}");
|
||||
//if (completedTask == timeouttask)
|
||||
//{
|
||||
// LogHelper.Instance.Error("连接OPCUA服务器超时!");
|
||||
//}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
LogHelper.Instance.Error($"连接OPCUA服务器失败!Connect:{ConnectString},{ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
public override void Close()
|
||||
{
|
||||
if (null != opcUaClient.Session)
|
||||
{
|
||||
IsConnect = false;
|
||||
isSubscribeNodes = false;
|
||||
opcUaClient?.RemoveAllSubscription();
|
||||
opcUaClient?.Disconnect();
|
||||
LogHelper.Instance.Fatal($"Close:{ConnectString},订阅删除");
|
||||
}
|
||||
}
|
||||
public override void StartRead()
|
||||
{
|
||||
base.StartRead();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 把地址拆开,x=3;11, OPC是节点,可以不要
|
||||
/// new用于隐藏方法,它调用的方法来自于申明的类
|
||||
/// </summary>
|
||||
public override void AnalysisAddress()
|
||||
{
|
||||
}
|
||||
|
||||
public override void GetStorageArea()
|
||||
{
|
||||
Storages.Clear();
|
||||
var query = (from v in VariableList //Regex.Replace(v.VarType, @"\[.*\]", string.Empty)
|
||||
orderby v.Id // Convert.ToInt16(v.Address), v.ReadLength
|
||||
group v by new { v.StationId, v.VarType } //new {v.DeviceId, v.VarType} //按读的数据类型分组 Regex.Replace(v.VarType, @"\[.*\]", string.Empty
|
||||
into a
|
||||
where a.Count() > 0
|
||||
orderby a.Key.StationId
|
||||
select a).ToList();
|
||||
foreach (var g in query)
|
||||
{
|
||||
//var first = g.First();
|
||||
var totalLengthQuery = (from a in g //同类型,同大小的分一组,单个和数组各一组,
|
||||
orderby a.ArrayLength
|
||||
group a by a.ArrayLength > 1 into b //a.VarType.Substring(a.VarType.Length - 1)
|
||||
select new
|
||||
{
|
||||
VarList = b,
|
||||
Id = b.First().Id,
|
||||
StationId = b.First().StationId,
|
||||
AddressType = b.First().VarType,
|
||||
//StartAddress = b.First().ReadLength, //这样第一次偏移永远是0
|
||||
GroupReadLength = b.Sum(t => t.ArrayLength)
|
||||
}).ToList();
|
||||
|
||||
totalLengthQuery.ForEach(x =>
|
||||
{
|
||||
List<string> nodes = new List<string>();
|
||||
List<Variable> variables = x.VarList.ToList();
|
||||
var readNode = variables.Where(ns => ns.OperType != (int)EOperTypePLC.Writable).ToList();
|
||||
foreach (var variable in readNode)
|
||||
{
|
||||
nodes.Add($"{variable.Address}{variable.VarName}");
|
||||
}
|
||||
Storages.Add(new StorageArea()
|
||||
{
|
||||
Id = x.Id,
|
||||
StationId = x.StationId,
|
||||
AddressType = x.AddressType,
|
||||
//StartAddress = x.StartAddress.ToString(),
|
||||
OPCNodes = nodes,
|
||||
Len = x.GroupReadLength, //如何判断是不是数组
|
||||
VariableList = variables
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
public override void GetJsonParam(string param)
|
||||
{
|
||||
dynamic d = JsonConvert.DeserializeObject<dynamic>(param);
|
||||
this.ConnectString = d.ConnectString;
|
||||
}
|
||||
|
||||
//public byte[] TToByte<T>(T[] value)
|
||||
//{
|
||||
// string typeName = value.GetType().FullName.Replace("[]", string.Empty);
|
||||
// Type type = Type.GetType(typeName); //System.UInt16
|
||||
// int size = Marshal.SizeOf(type);
|
||||
// byte[] result = new byte[value.Length * size];
|
||||
|
||||
// Buffer.BlockCopy(value, 0, result, 0, result.Length);
|
||||
// return result;
|
||||
//}
|
||||
|
||||
public override OperateResult<T> Read<T>(string tag)
|
||||
{
|
||||
OperateResult<T> operateResult = new OperateResult<T>()
|
||||
{
|
||||
IsSuccess = false,
|
||||
};
|
||||
if (!opcUaClient.Connected)
|
||||
{
|
||||
return operateResult;
|
||||
}
|
||||
operateResult.Content = opcUaClient.ReadNode<T>(tag);
|
||||
|
||||
if (null != operateResult.Content)
|
||||
{
|
||||
operateResult.IsSuccess = true;
|
||||
}
|
||||
|
||||
return operateResult;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 读多个节点,返回的数据类型相同 item.OPCNodes, item.AddressType, (item.VariableList[0].ArrayLength > 1) ? true : false
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="nodes"></param>
|
||||
/// <param name="len"></param>
|
||||
/// <returns></returns>
|
||||
public OperateResult<bool> ReadNode(StorageArea storage)
|
||||
{
|
||||
//opcUaClient.ReadNode 读单个未知数据类型
|
||||
//opcUaClient.ReadNode<> 读单个已知数据类型; 也可以是个数组地址,返回一个数组(同类型)
|
||||
//opcUaClient.ReadNodes 读多个节点数据,可以是多个不同类型的数据;一个节点,一个Object,返回object列表,1对1的关系
|
||||
//opcUaClient.ReadNodes<> 读多个节点数据,同一类型,返回一个数组
|
||||
//MethodInfo mi = null;
|
||||
bool result;
|
||||
OperateResult<bool> operateResult = new OperateResult<bool>()
|
||||
{
|
||||
IsSuccess = false,
|
||||
Content = false,
|
||||
};
|
||||
|
||||
result = Read(storage);
|
||||
|
||||
operateResult.IsSuccess = result;
|
||||
operateResult.Content = result;
|
||||
return operateResult;
|
||||
}
|
||||
|
||||
public override List<DataValue> Reads(NodeId[] nodeIds)
|
||||
{
|
||||
return opcUaClient.ReadNodes(nodeIds);
|
||||
}
|
||||
|
||||
public bool Read(StorageArea storage)
|
||||
{
|
||||
int index = 0;
|
||||
List<NodeId> nodeIds = new List<NodeId>();
|
||||
|
||||
foreach (var item in storage.OPCNodes)
|
||||
{
|
||||
nodeIds.Add(new NodeId(item));
|
||||
}
|
||||
if (0 == nodeIds.Count
|
||||
|| Global.AppExit
|
||||
|| !opcUaClient.Connected)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
List<DataValue> values = opcUaClient.ReadNodes(nodeIds.ToArray());
|
||||
var readNode = storage.VariableList.Where(ns => ns.OperType != (int)EOperTypePLC.Writable).ToList();
|
||||
foreach (var node in readNode)
|
||||
{
|
||||
if (values[index] == null || null == values[index].Value
|
||||
|| null == values[index].WrappedValue.Value
|
||||
|| !StatusCode.IsGood(values[index].StatusCode))
|
||||
{
|
||||
node.Quality = false;
|
||||
//LogHelper.Instance.Error($"读取节点数据失败,{node.VarName},{node.VarDesc}");
|
||||
//_unityContainer.Resolve<LogService>().AddLog($"PLC_OpcUaClient:Read:读取节点数据失败,{node.VarName},{node.VarDesc}", E_LogType.Info.ToString());
|
||||
return false;
|
||||
}
|
||||
else
|
||||
{
|
||||
node.CurValue = values[index].WrappedValue.Value;
|
||||
node.Value = ShowValue(values[index]);
|
||||
node.Quality = true;
|
||||
}
|
||||
index++;
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
string json = JsonConvert.SerializeObject(storage.OPCNodes.ToArray());
|
||||
LogHelper.Instance.Fatal($"读取节点,DeviceId:【{json}】报错," + ex.Message);
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public object ShowValue(DataValue value)
|
||||
{
|
||||
if (value.WrappedValue.TypeInfo.ValueRank == -1)
|
||||
{
|
||||
return value.WrappedValue.Value;
|
||||
}
|
||||
|
||||
if (value.WrappedValue.TypeInfo.BuiltInType == Opc.Ua.BuiltInType.Int32)
|
||||
{
|
||||
return string.Join("|", (int[])value.WrappedValue.Value); // 最终值
|
||||
}
|
||||
else if (value.WrappedValue.TypeInfo.BuiltInType == Opc.Ua.BuiltInType.UInt32)
|
||||
{
|
||||
return string.Join("|", (uint[])value.WrappedValue.Value); // 数组的情况参照上面的例子
|
||||
}
|
||||
else if (value.WrappedValue.TypeInfo.BuiltInType == Opc.Ua.BuiltInType.Float)
|
||||
{
|
||||
return string.Join("|", (float[])value.WrappedValue.Value); // 数组的情况参照上面的例子
|
||||
}
|
||||
else if (value.WrappedValue.TypeInfo.BuiltInType == Opc.Ua.BuiltInType.Boolean)
|
||||
{
|
||||
return string.Join("|", (bool[])value.WrappedValue.Value); // 数组的情况参照上面的例子
|
||||
}
|
||||
else if (value.WrappedValue.TypeInfo.BuiltInType == Opc.Ua.BuiltInType.UInt16)
|
||||
{
|
||||
return string.Join("|", (UInt16[])value.WrappedValue.Value); // 数组的情况参照上面的例子
|
||||
}
|
||||
else if (value.WrappedValue.TypeInfo.BuiltInType == Opc.Ua.BuiltInType.Int16)
|
||||
{
|
||||
return string.Join("|", (Int16[])value.WrappedValue.Value); // 数组的情况参照上面的例子
|
||||
}
|
||||
else
|
||||
{
|
||||
return "None";
|
||||
}
|
||||
}
|
||||
|
||||
public override void ReadStorageArea()
|
||||
{
|
||||
bool readError = false;
|
||||
OperateResult<bool> read;
|
||||
foreach (var item in Storages)
|
||||
{
|
||||
for (int i = 0; i < Global.MAX_READS; i++)
|
||||
{
|
||||
if (Global.AppExit)
|
||||
{
|
||||
return;
|
||||
}
|
||||
read = ReadNode(item);
|
||||
if (read.IsSuccess)
|
||||
{
|
||||
IsConnect = true;
|
||||
readError = false;
|
||||
break;
|
||||
}
|
||||
else
|
||||
{
|
||||
foreach (var val in item.VariableList)
|
||||
{
|
||||
val.Quality = false;
|
||||
}
|
||||
//LogHelper.Instance.Error($"{ConnectString},ErrorCode:{read.ErrorCode},读错误:" + read.Message);
|
||||
}
|
||||
readError = true;
|
||||
//由于此处是不停的根据表中的配置刷新数据的,OPC UA建立security channel的成本很高,所以必须限制此处的时间
|
||||
//给其他线程的读写留出空余时间
|
||||
Thread.Sleep(199);
|
||||
}
|
||||
Thread.Sleep(199);
|
||||
if (readError)
|
||||
{
|
||||
//230605
|
||||
//IsConnect = false;
|
||||
//break;
|
||||
}
|
||||
}
|
||||
// 触发 GC
|
||||
GC.Collect();
|
||||
GC.WaitForPendingFinalizers();
|
||||
}
|
||||
|
||||
//上下料
|
||||
public override OperateResult<T> GetValue<T>(string paramName)
|
||||
{
|
||||
string msg = "";
|
||||
OperateResult<T> result = new OperateResult<T>()
|
||||
{
|
||||
IsSuccess = false,
|
||||
};
|
||||
|
||||
Variable node = (from storage in Storages
|
||||
from item in storage.VariableList
|
||||
where item.ParamName == paramName
|
||||
select item).FirstOrDefault();
|
||||
|
||||
if (null == node || null == node.CurValue)
|
||||
{
|
||||
msg = $"获取内存PLC数据失败,paramName:{paramName}";
|
||||
result.Message = msg;
|
||||
return result;
|
||||
}
|
||||
else
|
||||
{
|
||||
result.Content = (T)Convert.ChangeType(node.CurValue, typeof(T));
|
||||
result.IsSuccess = true;
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
public override Variable GetVariable(string paramName, int machineId, int number = 0)
|
||||
{
|
||||
Variable node = (from storage in Storages
|
||||
from item in storage.VariableList
|
||||
where storage.StationId == machineId && item.ParamName == paramName && item.Number == number
|
||||
select item).FirstOrDefault();
|
||||
|
||||
if (null == node)
|
||||
{
|
||||
LogHelper.Instance.GetCurrentClassError("没有找到这个节点信息");
|
||||
}
|
||||
|
||||
return node;
|
||||
}
|
||||
|
||||
public override OperateResult<T> GetValue<T>(string paramName, int stationId, int number = 1)
|
||||
{
|
||||
string msg = "";
|
||||
OperateResult<T> result = new OperateResult<T>()
|
||||
{
|
||||
IsSuccess = false,
|
||||
};
|
||||
|
||||
Variable node = (from storage in Storages
|
||||
from item in storage.VariableList
|
||||
where storage.StationId == stationId && item.ParamName == paramName && item.Number == number
|
||||
select item).FirstOrDefault();
|
||||
|
||||
if (null == node || null == node.CurValue)
|
||||
{
|
||||
LogHelper.Instance.Error($"没有节点信息:{paramName},工站:{stationId},number:{number}");
|
||||
msg = $"获取内存PLC数据失败,addr:{node.Address}{node.VarName},number:{number},{stationId}";
|
||||
result.Message = msg;
|
||||
return result;
|
||||
}
|
||||
else
|
||||
{
|
||||
result.Content = (T)Convert.ChangeType(node.CurValue, typeof(T));
|
||||
result.IsSuccess = true;
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
//可以发送多个地址
|
||||
public override OperateResult Writes(string[] tags, object[] values, int maxCount = Global.MAX_READS)
|
||||
{
|
||||
OperateResult result = new OperateResult()
|
||||
{
|
||||
IsSuccess = false
|
||||
};
|
||||
try
|
||||
{
|
||||
for (int i = 0; i < maxCount; i++)
|
||||
{
|
||||
result.IsSuccess = opcUaClient.WriteNodes(tags, values); //现在不能写,因为
|
||||
if (result.IsSuccess)
|
||||
{
|
||||
break;
|
||||
}
|
||||
Thread.Sleep(100);
|
||||
}
|
||||
//写不成功就断开链接暂时屏蔽,针对OPC UA,因为重建session需要成本太大
|
||||
//230605
|
||||
//IsConnect = false;
|
||||
//出异常再捕获,写不成功暂时不捕获
|
||||
//LogHelper.Instance.Info($"写数据失败,节点 = {tags.ToString()},值={values.ToString()}");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
LogHelper.Instance.Error($"写数据失败,节点 = {tags},值={values},失败原因:{ex}");
|
||||
_unityContainer.Resolve<LogService>().AddLog($"PLC_OpcUaClient:写数据失败,节点 = {tags},值={values}", E_LogType.Error.ToString());
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
//T:可以为数组
|
||||
public override OperateResult Write<T>(string address, T data, int maxCount = Global.MAX_READS)
|
||||
{
|
||||
OperateResult result = new OperateResult()
|
||||
{
|
||||
IsSuccess = false
|
||||
};
|
||||
|
||||
try
|
||||
{
|
||||
for (int i = 0; i < maxCount; i++)
|
||||
{
|
||||
if (!opcUaClient.Connected)
|
||||
{
|
||||
return result;
|
||||
}
|
||||
result.IsSuccess = opcUaClient.WriteNode<T>(address, data); //现在不能写,因为
|
||||
if (result.IsSuccess)
|
||||
{
|
||||
break;
|
||||
//return result;
|
||||
}
|
||||
Thread.Sleep(199);
|
||||
}
|
||||
//230605
|
||||
//IsConnect = false;
|
||||
//LogHelper.Instance.Info($"写数据失败,节点 = {address},值={data.ToString()}");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
LogHelper.Instance.Error($"写数据失败,节点 = {address},值={data},失败原因:{ex}");
|
||||
_unityContainer.Resolve<LogService>().AddLog($"PLC_OpcUaClient:Write:写数据失败,节点 = {address},值={data},失败原因:" + ex.Message, E_LogType.Error.ToString());
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public void SubscriptionNodes()
|
||||
{
|
||||
List<Variable> SubInfoNodes = (from v in VariableList
|
||||
orderby v.Index
|
||||
where v.TrigEnable == true //按读的数据类型分组 炉子有42个信号
|
||||
select v).ToList();
|
||||
foreach (var item in SubInfoNodes)
|
||||
{
|
||||
string vkey = $"{item.Id}";
|
||||
string nodeId = $"{item.Address}{item.VarName}";
|
||||
SingleNodeIdDatasSubscription(vkey, nodeId, (key, monitoredItem, args) =>
|
||||
{
|
||||
if (vkey == key)
|
||||
{
|
||||
MonitoredItemNotification notification = args.NotificationValue as MonitoredItemNotification;
|
||||
if (notification != null)
|
||||
{
|
||||
item.Quality = StatusCode.IsGood(notification.Value.StatusCode);
|
||||
if (item.Quality)
|
||||
{
|
||||
_unityContainer.Resolve<PLCBlockingCollection>().MsgBlock.Add(new BlockData(notification.Value, item));
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 单节点数据订阅
|
||||
/// </summary>
|
||||
/// <param name="key">订阅的关键字(必须唯一)</param>
|
||||
/// <param name="nodeId">节点:"ns=3;s=\"test\".\"Static_1\""</param>
|
||||
/// <param name="callback">数据订阅的回调方法</param>
|
||||
public void SingleNodeIdDatasSubscription(string key, string nodeId, Action<string, Opc.Ua.Client.MonitoredItem, MonitoredItemNotificationEventArgs> callback)
|
||||
{
|
||||
if (opcUaClient == null) return;
|
||||
try
|
||||
{
|
||||
opcUaClient.AddSubscription(key, nodeId, callback);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
LogHelper.Instance.Error($"订阅节点异常:{nodeId},Msg={ex.Message}");
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
715
Cowain.Bake.Communication/PLC/PLC_OpcUaClient1.cs
Normal file
715
Cowain.Bake.Communication/PLC/PLC_OpcUaClient1.cs
Normal file
@@ -0,0 +1,715 @@
|
||||
using Cowain.Bake.BLL;
|
||||
using Cowain.Bake.Common;
|
||||
using Cowain.Bake.Common.Core;
|
||||
using Cowain.Bake.Common.Enums;
|
||||
using Cowain.Bake.Common.Interface;
|
||||
using Cowain.Bake.Model.Entity;
|
||||
using Cowain.Bake.Model.Models;
|
||||
using HslCommunication;
|
||||
using Newtonsoft.Json;
|
||||
using Opc.Ua;
|
||||
using Opc.Ua.Client;
|
||||
using OpcUaHelper;
|
||||
using Prism.Services.Dialogs;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Unity;
|
||||
|
||||
|
||||
namespace Cowain.Bake.Communication.PLC
|
||||
{
|
||||
public class PLC_OpcUaClient : PLCBase
|
||||
{
|
||||
readonly static object _objLock = new object();
|
||||
readonly CancellationTokenSource cts = new CancellationTokenSource();
|
||||
bool isSubscribeNodes = false;
|
||||
public OpcUaClient opcUaClient { get; set; }
|
||||
ITrigService _trigService { get; set; }
|
||||
public PLC_OpcUaClient(IUnityContainer unityContainer, IDialogService dialogService) : base(unityContainer, dialogService)
|
||||
{
|
||||
opcUaClient = new OpcUaClient();
|
||||
OPC = opcUaClient;
|
||||
|
||||
opcUaClient.ConnectComplete += OpcUaClient_ConnectComplete;
|
||||
opcUaClient.KeepAliveComplete += OpcUaClient_KeepAliveComplete;
|
||||
opcUaClient.ReconnectStarting += OpcUaClient_ReconnectStarting;
|
||||
opcUaClient.ReconnectComplete += OpcUaClient_ReconnectComplete;
|
||||
opcUaClient.OpcStatusChange += OpcUaClient_OpcStatusChange;
|
||||
_trigService = _unityContainer.Resolve<ITrigService>(); //MachinePLCService
|
||||
//Start();
|
||||
}
|
||||
|
||||
~PLC_OpcUaClient()
|
||||
{
|
||||
Close();
|
||||
Stop();
|
||||
}
|
||||
public void Start()
|
||||
{
|
||||
Task.Run(async () =>
|
||||
{
|
||||
while (!cts.Token.IsCancellationRequested)
|
||||
{
|
||||
if (Global.AppExit)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
await Task.Delay(Global.HEARTBEAT_INTERVAL_TIME);
|
||||
|
||||
if (null == opcUaClient || !opcUaClient.Connected || !IsConnect)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
var node = (from secondaryList in Storages
|
||||
from item in secondaryList.VariableList
|
||||
where item.ParamName == EStoveSignal.Heartbeat.ToString()
|
||||
orderby item.Number
|
||||
select item).FirstOrDefault();
|
||||
if (null == node)
|
||||
{
|
||||
LogHelper.Instance.Warn("查询心跳节点失败");
|
||||
continue;
|
||||
}
|
||||
|
||||
OperateResult result = Write<UInt16>(node.Address + node.VarName, 1);
|
||||
if (!result.IsSuccess)
|
||||
{
|
||||
LogHelper.Instance.Warn($"MachineId:{Storages[0].StationId}:写心跳节点失败");
|
||||
}
|
||||
}
|
||||
}, cts.Token);
|
||||
}
|
||||
|
||||
public void Stop()
|
||||
{
|
||||
cts.Cancel();
|
||||
}
|
||||
/// <summary>
|
||||
/// 连接服务器结束后马上浏览根节点
|
||||
/// </summary>
|
||||
/// <param name="sender"></param>
|
||||
/// <param name="e"></param>
|
||||
private void OpcUaClient_ConnectComplete(object sender, EventArgs e)
|
||||
{
|
||||
try
|
||||
{
|
||||
opcUaClient = (OpcUaClient)sender; //2.重连不成功
|
||||
OPC = opcUaClient;
|
||||
IsConnect = opcUaClient.Connected;
|
||||
|
||||
if (!isSubscribeNodes
|
||||
&& IsConnect
|
||||
&& !Global.AppExit)
|
||||
{
|
||||
opcUaClient.RemoveAllSubscription();
|
||||
isSubscribeNodes = true;
|
||||
SubscribeNodes(); //订阅节点
|
||||
LogHelper.Instance.Fatal($"{ConnectString},订阅节点");
|
||||
}
|
||||
}
|
||||
catch (Exception exception)
|
||||
{
|
||||
IsConnect = false;
|
||||
LogHelper.Instance.Error($"订阅失败!,{ConnectString},{exception}");
|
||||
}
|
||||
}
|
||||
|
||||
private void OpcUaClient_OpcStatusChange(object sender, OpcUaStatusEventArgs e)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (e.Error) //1.重连不成功
|
||||
{
|
||||
IsConnect = false;
|
||||
LogHelper.Instance.Warn($"OPC UA客户端重连不成功,{e.Text}");
|
||||
}
|
||||
|
||||
if (!Regex.IsMatch(e.Text, "Connected"))
|
||||
{
|
||||
LogHelper.Instance.Info($"本OPC UA客户端的终极事件,{IpAddress},{e.Text}");
|
||||
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
LogHelper.Instance.Error($"OpcUaClient_OpcStatusChange:" + ex.Message);
|
||||
//throw;
|
||||
}
|
||||
|
||||
//if (Regex.IsMatch(e.Text, "Reconnecting")) //验证无效
|
||||
//{
|
||||
// opcUaClient.Session.Reconnect();
|
||||
//}
|
||||
//LogHelper.Instance.Info("本OPC UA客户端的终极事件,当客户端的状态变更都会触发,包括了连接,重连,断开,状态激活:" + e.ToString());
|
||||
}
|
||||
|
||||
private void OpcUaClient_ReconnectComplete(object sender, EventArgs e)
|
||||
{
|
||||
LogHelper.Instance.Info("重新连接到服务器的时候触发");
|
||||
}
|
||||
|
||||
private void OpcUaClient_ReconnectStarting(object sender, EventArgs e)
|
||||
{
|
||||
LogHelper.Instance.Info($"开始重新连接到服务器的时候触发,{IpAddress}");
|
||||
}
|
||||
|
||||
private void OpcUaClient_KeepAliveComplete(object sender, EventArgs e)
|
||||
{
|
||||
//LogHelper.Instance.Debug("ua客户端每隔5秒会与服务器进行通讯验证,每次验证都会触发该方法");
|
||||
}
|
||||
|
||||
public async override void Connect()
|
||||
{
|
||||
try
|
||||
{
|
||||
if (null != opcUaClient.Session)
|
||||
{
|
||||
//如果session没问题,能关掉,为什么要重连呢,230606
|
||||
Close();
|
||||
}
|
||||
|
||||
//var timeouttask = Task.Delay(3000);
|
||||
//var completedTask = await Task.WhenAny(opcUaClient.ConnectServer(ConnectString), timeouttask);
|
||||
await opcUaClient.ConnectServer(ConnectString);
|
||||
opcUaClient.ReconnectPeriod = 20000; //OPC UA 客户端在与服务器断开连接后尝试重新连接的时间间隔,是一个表示时间间隔的数值,以毫秒为单位
|
||||
if (null != opcUaClient.Session)
|
||||
{
|
||||
opcUaClient.Session.OperationTimeout = 5000; // 执行操作时的超时时间。它指定了客户端等待服务器响应的最长时间,以毫秒为单位
|
||||
}
|
||||
LogHelper.Instance.Fatal($"连接OPCUA服务器,{ConnectString}");
|
||||
//if (completedTask == timeouttask)
|
||||
//{
|
||||
// LogHelper.Instance.Error("连接OPCUA服务器超时!");
|
||||
//}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
LogHelper.Instance.Error($"连接OPCUA服务器失败!Connect:{ConnectString},{ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
public override void Close()
|
||||
{
|
||||
if (null != opcUaClient.Session)
|
||||
{
|
||||
IsConnect = false;
|
||||
isSubscribeNodes = false;
|
||||
opcUaClient.RemoveAllSubscription(); //
|
||||
opcUaClient.Disconnect();
|
||||
LogHelper.Instance.Fatal($"Close:{ConnectString},订阅删除");
|
||||
}
|
||||
}
|
||||
public override void StartRead()
|
||||
{
|
||||
base.StartRead();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 把地址拆开,x=3;11, OPC是节点,可以不要
|
||||
/// new用于隐藏方法,它调用的方法来自于申明的类
|
||||
/// </summary>
|
||||
public override void AnalysisAddress()
|
||||
{
|
||||
}
|
||||
|
||||
public override void GetStorageArea()
|
||||
{
|
||||
Storages.Clear();
|
||||
var query = (from v in VariableList //Regex.Replace(v.VarType, @"\[.*\]", string.Empty)
|
||||
orderby v.Id // Convert.ToInt16(v.Address), v.ReadLength
|
||||
group v by new { v.StationId, v.VarType } //new {v.DeviceId, v.VarType} //按读的数据类型分组 Regex.Replace(v.VarType, @"\[.*\]", string.Empty
|
||||
into a
|
||||
where a.Count() > 0
|
||||
orderby a.Key.StationId
|
||||
select a).ToList();
|
||||
foreach (var g in query)
|
||||
{
|
||||
//var first = g.First();
|
||||
var totalLengthQuery = (from a in g //同类型,同大小的分一组,单个和数组各一组,
|
||||
orderby a.ArrayLength
|
||||
group a by a.ArrayLength > 1 into b //a.VarType.Substring(a.VarType.Length - 1)
|
||||
select new
|
||||
{
|
||||
VarList = b,
|
||||
Id = b.First().Id,
|
||||
StationId = b.First().StationId,
|
||||
AddressType = b.First().VarType,
|
||||
//StartAddress = b.First().ReadLength, //这样第一次偏移永远是0
|
||||
GroupReadLength = b.Sum(t => t.ArrayLength)
|
||||
}).ToList();
|
||||
|
||||
totalLengthQuery.ForEach(x =>
|
||||
{
|
||||
List<string> nodes = new List<string>();
|
||||
List<Variable> variables = x.VarList.ToList();
|
||||
var readNode = variables.Where(ns => ns.OperType != (int)EOperTypePLC.Writable).ToList();
|
||||
foreach (var variable in readNode)
|
||||
{
|
||||
nodes.Add($"{variable.Address}{variable.VarName}");
|
||||
}
|
||||
Storages.Add(new StorageArea()
|
||||
{
|
||||
Id = x.Id,
|
||||
StationId = x.StationId,
|
||||
AddressType = x.AddressType,
|
||||
//StartAddress = x.StartAddress.ToString(),
|
||||
OPCNodes = nodes,
|
||||
Len = x.GroupReadLength, //如何判断是不是数组
|
||||
VariableList = variables
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
public override void GetJsonParam(string param)
|
||||
{
|
||||
dynamic d = JsonConvert.DeserializeObject<dynamic>(param);
|
||||
this.ConnectString = d.ConnectString;
|
||||
}
|
||||
|
||||
//public byte[] TToByte<T>(T[] value)
|
||||
//{
|
||||
// string typeName = value.GetType().FullName.Replace("[]", string.Empty);
|
||||
// Type type = Type.GetType(typeName); //System.UInt16
|
||||
// int size = Marshal.SizeOf(type);
|
||||
// byte[] result = new byte[value.Length * size];
|
||||
|
||||
// Buffer.BlockCopy(value, 0, result, 0, result.Length);
|
||||
// return result;
|
||||
//}
|
||||
|
||||
public override OperateResult<T> Read<T>(string tag)
|
||||
{
|
||||
OperateResult<T> operateResult = new OperateResult<T>()
|
||||
{
|
||||
IsSuccess = false,
|
||||
};
|
||||
if (!opcUaClient.Connected)
|
||||
{
|
||||
return operateResult;
|
||||
}
|
||||
operateResult.Content = opcUaClient.ReadNode<T>(tag);
|
||||
|
||||
if (null != operateResult.Content)
|
||||
{
|
||||
operateResult.IsSuccess = true;
|
||||
}
|
||||
|
||||
return operateResult;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 读多个节点,返回的数据类型相同 item.OPCNodes, item.AddressType, (item.VariableList[0].ArrayLength > 1) ? true : false
|
||||
/// </summary>
|
||||
/// <typeparam name="T"></typeparam>
|
||||
/// <param name="nodes"></param>
|
||||
/// <param name="len"></param>
|
||||
/// <returns></returns>
|
||||
public OperateResult<bool> ReadNode(StorageArea storage)
|
||||
{
|
||||
//opcUaClient.ReadNode 读单个未知数据类型
|
||||
//opcUaClient.ReadNode<> 读单个已知数据类型; 也可以是个数组地址,返回一个数组(同类型)
|
||||
//opcUaClient.ReadNodes 读多个节点数据,可以是多个不同类型的数据;一个节点,一个Object,返回object列表,1对1的关系
|
||||
//opcUaClient.ReadNodes<> 读多个节点数据,同一类型,返回一个数组
|
||||
//MethodInfo mi = null;
|
||||
bool result;
|
||||
OperateResult<bool> operateResult = new OperateResult<bool>()
|
||||
{
|
||||
IsSuccess = false,
|
||||
Content = false,
|
||||
};
|
||||
|
||||
result = Read(storage);
|
||||
|
||||
operateResult.IsSuccess = result;
|
||||
operateResult.Content = result;
|
||||
return operateResult;
|
||||
}
|
||||
|
||||
public override List<DataValue> Reads(NodeId[] nodeIds)
|
||||
{
|
||||
return opcUaClient.ReadNodes(nodeIds);
|
||||
}
|
||||
|
||||
public bool Read(StorageArea storage)
|
||||
{
|
||||
int index = 0;
|
||||
List<NodeId> nodeIds = new List<NodeId>();
|
||||
|
||||
foreach (var item in storage.OPCNodes)
|
||||
{
|
||||
nodeIds.Add(new NodeId(item));
|
||||
}
|
||||
|
||||
//------------------------------------------- add by lsm 20250925
|
||||
//foreach (var item in storage.OPCNodes)
|
||||
//{
|
||||
// DataValue values1 = opcUaClient.ReadNode(item);
|
||||
|
||||
// if (values1 == null || null == values1.Value
|
||||
// || null == values1.WrappedValue.Value
|
||||
// || !StatusCode.IsGood(values1.StatusCode))
|
||||
// {
|
||||
// LogHelper.Instance.Error($"读取失败:{item}");
|
||||
// //_unityContainer.Resolve<LogService>().AddLog($"PLC_OpcUaClient:Read:读取节点数据失败,{node.VarName},{node.VarDesc}", E_LogType.Info.ToString());
|
||||
// }
|
||||
// else
|
||||
// {
|
||||
// int kkk = 0;
|
||||
// }
|
||||
//}
|
||||
//LogHelper.Instance.Debug($"-----end----------");
|
||||
//-------------------------------------------
|
||||
|
||||
if (0 == nodeIds.Count
|
||||
|| Global.AppExit
|
||||
|| !opcUaClient.Connected)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
List<DataValue> values = opcUaClient.ReadNodes(nodeIds.ToArray());
|
||||
var readNode = storage.VariableList.Where(ns => ns.OperType != (int)EOperTypePLC.Writable).ToList();
|
||||
foreach (var node in readNode)
|
||||
{
|
||||
if (values[index] == null || null == values[index].Value
|
||||
|| null == values[index].WrappedValue.Value
|
||||
|| !StatusCode.IsGood(values[index].StatusCode))
|
||||
{
|
||||
node.Quality = false;
|
||||
//LogHelper.Instance.Error($"读取节点数据失败,{node.VarName},{node.VarDesc}");
|
||||
//_unityContainer.Resolve<LogService>().AddLog($"PLC_OpcUaClient:Read:读取节点数据失败,{node.VarName},{node.VarDesc}", E_LogType.Info.ToString());
|
||||
return false;
|
||||
}
|
||||
else
|
||||
{
|
||||
node.CurValue = values[index].WrappedValue.Value;
|
||||
node.Value = ShowValue(values[index]);
|
||||
node.Quality = true;
|
||||
}
|
||||
index++;
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
string json = JsonConvert.SerializeObject(storage.OPCNodes.ToArray());
|
||||
LogHelper.Instance.Fatal($"读取节点,DeviceId:【{json}】报错," + ex.Message);
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public object ShowValue(DataValue value)
|
||||
{
|
||||
if (value.WrappedValue.TypeInfo.ValueRank == -1)
|
||||
{
|
||||
return value.WrappedValue.Value;
|
||||
}
|
||||
|
||||
if (value.WrappedValue.TypeInfo.BuiltInType == Opc.Ua.BuiltInType.Int32)
|
||||
{
|
||||
return string.Join("|", (int[])value.WrappedValue.Value); // 最终值
|
||||
}
|
||||
else if (value.WrappedValue.TypeInfo.BuiltInType == Opc.Ua.BuiltInType.UInt32)
|
||||
{
|
||||
return string.Join("|", (uint[])value.WrappedValue.Value); // 数组的情况参照上面的例子
|
||||
}
|
||||
else if (value.WrappedValue.TypeInfo.BuiltInType == Opc.Ua.BuiltInType.Float)
|
||||
{
|
||||
return string.Join("|", (float[])value.WrappedValue.Value); // 数组的情况参照上面的例子
|
||||
}
|
||||
else if (value.WrappedValue.TypeInfo.BuiltInType == Opc.Ua.BuiltInType.Boolean)
|
||||
{
|
||||
return string.Join("|", (bool[])value.WrappedValue.Value); // 数组的情况参照上面的例子
|
||||
}
|
||||
else if (value.WrappedValue.TypeInfo.BuiltInType == Opc.Ua.BuiltInType.UInt16)
|
||||
{
|
||||
return string.Join("|", (UInt16[])value.WrappedValue.Value); // 数组的情况参照上面的例子
|
||||
}
|
||||
else if (value.WrappedValue.TypeInfo.BuiltInType == Opc.Ua.BuiltInType.Int16)
|
||||
{
|
||||
return string.Join("|", (Int16[])value.WrappedValue.Value); // 数组的情况参照上面的例子
|
||||
}
|
||||
else
|
||||
{
|
||||
return "None";
|
||||
}
|
||||
}
|
||||
|
||||
public override void ReadStorageArea()
|
||||
{
|
||||
bool readError = false;
|
||||
OperateResult<bool> read ;
|
||||
foreach (var item in Storages)
|
||||
{
|
||||
for (int i = 0; i < Global.MAX_READS; i++)
|
||||
{
|
||||
if (Global.AppExit)
|
||||
{
|
||||
return;
|
||||
}
|
||||
read = ReadNode(item);
|
||||
if (read.IsSuccess)
|
||||
{
|
||||
IsConnect = true;
|
||||
readError = false;
|
||||
break;
|
||||
}
|
||||
else
|
||||
{
|
||||
foreach (var val in item.VariableList)
|
||||
{
|
||||
val.Quality = false;
|
||||
}
|
||||
//LogHelper.Instance.Error($"{ConnectString},ErrorCode:{read.ErrorCode},读错误:" + read.Message);
|
||||
}
|
||||
readError = true;
|
||||
//由于此处是不停的根据表中的配置刷新数据的,OPC UA建立security channel的成本很高,所以必须限制此处的时间
|
||||
//给其他线程的读写留出空余时间
|
||||
Thread.Sleep(199);
|
||||
}
|
||||
Thread.Sleep(199);
|
||||
if (readError)
|
||||
{
|
||||
//230605
|
||||
//IsConnect = false;
|
||||
//break;
|
||||
}
|
||||
}
|
||||
// 触发 GC
|
||||
GC.Collect();
|
||||
GC.WaitForPendingFinalizers();
|
||||
}
|
||||
|
||||
//上下料
|
||||
public override OperateResult<T> GetValue<T>(string paramName)
|
||||
{
|
||||
string msg = "";
|
||||
OperateResult<T> result = new OperateResult<T>()
|
||||
{
|
||||
IsSuccess = false,
|
||||
};
|
||||
|
||||
Variable node = (from storage in Storages
|
||||
from item in storage.VariableList
|
||||
where item.ParamName == paramName
|
||||
select item).FirstOrDefault();
|
||||
|
||||
if (null == node || null == node.CurValue)
|
||||
{
|
||||
msg = $"获取内存PLC数据失败,paramName:{paramName}";
|
||||
result.Message = msg;
|
||||
return result;
|
||||
}
|
||||
else
|
||||
{
|
||||
result.Content = (T)Convert.ChangeType(node.CurValue, typeof(T));
|
||||
result.IsSuccess = true;
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
public override Variable GetVariable(string paramName, int machineId, int number = 0)
|
||||
{
|
||||
Variable node = (from storage in Storages
|
||||
from item in storage.VariableList
|
||||
where storage.StationId == machineId && item.ParamName == paramName && item.Number == number
|
||||
select item).FirstOrDefault();
|
||||
|
||||
if (null == node)
|
||||
{
|
||||
LogHelper.Instance.GetCurrentClassError("没有找到这个节点信息");
|
||||
}
|
||||
|
||||
return node;
|
||||
}
|
||||
|
||||
public override OperateResult<T> GetValue<T>(string paramName, int stationId, int number = 1)
|
||||
{
|
||||
string msg = "";
|
||||
OperateResult<T> result = new OperateResult<T>()
|
||||
{
|
||||
IsSuccess = false,
|
||||
};
|
||||
|
||||
Variable node = (from storage in Storages
|
||||
from item in storage.VariableList
|
||||
where storage.StationId == stationId && item.ParamName == paramName && item.Number == number
|
||||
select item).FirstOrDefault();
|
||||
|
||||
if (null == node || null == node.CurValue)
|
||||
{
|
||||
LogHelper.Instance.Error($"没有节点信息:{paramName},工站:{stationId},number:{number}");
|
||||
msg = $"获取内存PLC数据失败,addr:{node.Address}{node.VarName},number:{number},{stationId}";
|
||||
result.Message = msg;
|
||||
return result;
|
||||
}
|
||||
else
|
||||
{
|
||||
result.Content = (T)Convert.ChangeType(node.CurValue, typeof(T));
|
||||
result.IsSuccess = true;
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
//可以发送多个地址
|
||||
public override OperateResult Writes(string[] tags, object[] values, int maxCount = Global.MAX_READS)
|
||||
{
|
||||
OperateResult result = new OperateResult()
|
||||
{
|
||||
IsSuccess = false
|
||||
};
|
||||
try
|
||||
{
|
||||
for (int i = 0; i < maxCount; i++)
|
||||
{
|
||||
result.IsSuccess = opcUaClient.WriteNodes(tags, values); //现在不能写,因为
|
||||
if (result.IsSuccess)
|
||||
{
|
||||
break;
|
||||
}
|
||||
Thread.Sleep(100);
|
||||
}
|
||||
//写不成功就断开链接暂时屏蔽,针对OPC UA,因为重建session需要成本太大
|
||||
//230605
|
||||
//IsConnect = false;
|
||||
//出异常再捕获,写不成功暂时不捕获
|
||||
//LogHelper.Instance.Info($"写数据失败,节点 = {tags.ToString()},值={values.ToString()}");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
LogHelper.Instance.Error($"写数据失败,节点 = {tags},值={values},失败原因:{ex}");
|
||||
_unityContainer.Resolve<LogService>().AddLog($"PLC_OpcUaClient:写数据失败,节点 = {tags},值={values}", E_LogType.Error.ToString());
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
//T:可以为数组
|
||||
public override OperateResult Write<T>(string address, T data, int maxCount = Global.MAX_READS)
|
||||
{
|
||||
OperateResult result = new OperateResult()
|
||||
{
|
||||
IsSuccess = false
|
||||
};
|
||||
|
||||
try
|
||||
{
|
||||
for (int i = 0; i < maxCount; i++)
|
||||
{
|
||||
if (!opcUaClient.Connected)
|
||||
{
|
||||
return result;
|
||||
}
|
||||
result.IsSuccess = opcUaClient.WriteNode<T>(address, data); //现在不能写,因为
|
||||
if (result.IsSuccess)
|
||||
{
|
||||
break;
|
||||
//return result;
|
||||
}
|
||||
Thread.Sleep(199);
|
||||
}
|
||||
//230605
|
||||
//IsConnect = false;
|
||||
//LogHelper.Instance.Info($"写数据失败,节点 = {address},值={data.ToString()}");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
LogHelper.Instance.Error($"写数据失败,节点 = {address},值={data},失败原因:{ex}");
|
||||
_unityContainer.Resolve<LogService>().AddLog($"PLC_OpcUaClient:Write:写数据失败,节点 = {address},值={data},失败原因:" + ex.Message, E_LogType.Error.ToString());
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 订阅节点//AddSubscription:单线程回调模型。因此只要你的回调方法没有主动开线程,就是串行执行。
|
||||
/// </summary>
|
||||
private void SubscribeNodes()
|
||||
{
|
||||
string nodeAddr = "";
|
||||
List<Variable> SubInfoNodes = (from v in VariableList
|
||||
orderby v.Index
|
||||
where v.TrigEnable == true //按读的数据类型分组 炉子有42个信号
|
||||
select v).ToList();
|
||||
|
||||
try
|
||||
{
|
||||
foreach (var node in SubInfoNodes)
|
||||
{
|
||||
nodeAddr = $"{node.Address}{node.VarName}";
|
||||
opcUaClient.RemoveSubscription(node.Id.ToString());
|
||||
opcUaClient.AddSubscription(node.Id.ToString(), nodeAddr, SubCallback); //NotificationReceived 事件处理器,当数据变化时,这个处理器会被异步调用
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
LogHelper.Instance.Fatal($"订阅时报错,{nodeAddr},{ConnectString},{ex.Message}.");
|
||||
_unityContainer.Resolve<LogService>().AddLog($"PLC_OpcUaClient:订阅时报错,{nodeAddr},{ConnectString},{ex.Message}.", E_LogType.Debug.ToString());
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 执行订阅的事件(方法)
|
||||
/// </summary>
|
||||
/// <param name="key">node.TrigJson</param>
|
||||
/// <param name="monitoredItem">节点的值</param>
|
||||
/// <param name="args"></param>
|
||||
private void SubCallback(string key, MonitoredItem monitoredItem, MonitoredItemNotificationEventArgs args)
|
||||
{
|
||||
int Id = 0;
|
||||
try
|
||||
{
|
||||
lock (_objLock)
|
||||
{
|
||||
Id = int.Parse(key);
|
||||
if (0 == Id) //OPC服务退出,会触发这里
|
||||
{
|
||||
LogHelper.Instance.Fatal("OPC触发异常!");
|
||||
return;
|
||||
}
|
||||
// 如果有多个的订阅值都关联了当前的方法,可以通过key和monitoredItem来区分
|
||||
MonitoredItemNotification notification = args.NotificationValue as MonitoredItemNotification;
|
||||
Variable node = VariableDic[Id]; //索引,之前是遍历
|
||||
|
||||
if (null == notification || null == notification.Value
|
||||
|| null == notification.Value.WrappedValue.Value) //key
|
||||
{
|
||||
LogHelper.Instance.Fatal($"触发节点编号;{node.Id},{node.VarDesc},读取数据失败!");
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
if (null == node)
|
||||
{
|
||||
LogHelper.Instance.GetCurrentClassWarn("必须对应唯一的一个Write节点");
|
||||
return;
|
||||
}
|
||||
//foreach (var value in monitoredItem.DequeueValues())
|
||||
//{
|
||||
|
||||
//}
|
||||
//LogHelper.Instance.Info($"1-----SubCallback---回调:{Id}-----{monitoredItem.StartNodeId.ToString()}------信号,线程ID:{System.Threading.Thread.CurrentThread.ManagedThreadId}"); //此为多线程
|
||||
|
||||
_unityContainer.Resolve<PLCBlockingCollection>().MsgBlock.Add(new BlockData(notification.Value, node));
|
||||
//_trigService.RecvTrigInfo(notification.Value, node);
|
||||
}
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
LogHelper.Instance.Info($"OPC 触发解析出错,{key},{e.Message}");
|
||||
_unityContainer.Resolve<LogService>().AddLog($"PLC_OpcUaClient:OPC 触发解析出错,{key},{e.Message}", E_LogType.Debug.ToString());
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user