所有库支持 .net8 与 .net9 ,不管你的项目是 8 或 9 都可以使用
https://www.nuget.org/profiles/Shun
能帮助到各位,是我莫大的荣幸,如果觉得不错可以小小的赞助一下喔
这些都是免费的公共动态库,在 Nuget 上都可以搜索到 [ 搜索格式: Snet.* ]
支持定制开发(带上需求文档)
namespace Snet.Model.@interface
{
/// <summary>
/// 数采接口
/// </summary>
public interface IDaq : IOn, IOff, IRead, IWrite, ISubscribe, IGetStatus, IEvent, IGetParam, ICreateInstance, ILog, IDisposable { }
}
namespace Snet.Model.@interface
{
/// <summary>
/// 转发接口
/// </summary>
public interface IRelay : IOn, IOff, IProducer, IConsumer, IGetStatus, IEvent, IGetParam, ICreateInstance, ILog, IDisposable { }
}
//实例创建的几种方式
//以OPCUA 采集协议为例
using Snet.Model.@interface;
using Snet.Opc.ua.client;
OpcUaClientOperate? operate = null;
IDaq? daq = null;
//无参实例
operate = new OpcUaClientOperate();
//无参实例调函数创建实例,与无参实例配合使用
operate = new OpcUaClientOperate().CreateInstance(new OpcUaClientData.Basics()).GetRData<OpcUaClientOperate>();
//有参实例
operate = new OpcUaClientOperate(new OpcUaClientData.Basics());
//有参单例
operate = OpcUaClientOperate.Instance(new OpcUaClientData.Basics());
//接口 - 无参实例
daq = new OpcUaClientOperate();
//接口 - 无参实例调函数创建实例,与无参实例配合使用
daq = new OpcUaClientOperate().CreateInstance(new OpcUaClientData.Basics()).GetRData<OpcUaClientOperate>();
//接口 - 有参实例
daq = new OpcUaClientOperate(new OpcUaClientData.Basics());
//接口 - 有参单例
daq = OpcUaClientOperate.Instance(new OpcUaClientData.Basics());
using (operate)
{
//使用完直接释放
}
using (daq)
{
//使用完直接释放
}
//采集协议
//以OPCUA 采集协议为例
using System.Collections.Concurrent;
using Snet.Core.script;
using Snet.Log;
using Snet.Model.data;
using Snet.Model.@enum;
using Snet.Opc.ua.client;
using Snet.Unility;
using (OpcUaClientOperate operate = new OpcUaClientOperate(new OpcUaClientData.Basics
{
ServerUrl = "opc.tcp://127.0.0.1:6688",
UserName = "user",
Password = "password",
}))
{
//点位地址
Address address = new Address();
address.SN = Guid.NewGuid().ToString();
address.CreationTime = DateTime.Now.ToLocalTime();
address.AddressArray = new List<AddressDetails>
{
new AddressDetails() //地址详情参数介绍
{
SN=$"", //可以理解成唯一标识符(可以存机台号、组名、车间、厂)
AddressAnotherName="", //地址别名
AddressDataType=DataType.String, //数据类型
AddressDescribe="", //地址描述
AddressExtendParam=new object(), //扩展数据
AddressName="", //实际地址[ 不能为空 ]
AddressPropertyName="", //属性名称
AddressType=AddressType.Reality, //地址类型
IsEnable=true, //是否启用
AddressRelayParam=new AddressRelay //转发参数
{
ISns = new List<string> { "ISN1", "ISN2" }, //实例SN
Topic = $"topic", //主题
ContentFormat="Value:{0}" //内容格式
},
AddressParseParam=new AddressParse //脚本解析实例
{
ScriptParam=new ScriptData.Basics //脚本解析基础数据
{
ScriptCode = @"function Convert(addressname,value) { return '【这是调用脚本解析】传入的地址是:'+ addressname + '----传入的参数是:' + value; }", //脚本代码
ScriptFunction = "Convert", //脚本入口
ScriptType = ScriptData.ScriptType.JavaScript //脚本类型
}
},
//AddressParseParam = new AddressParse //反射解析
//{
// ReflectionParam = new object[] //反射解析的参数
// {
// new ReflectionData.Basics //反射解析基础数据
// {
// //反射解析的基础数据
// },
// "SN" //反射解析的SN
// }
// },
}
};
#region 打开
OperateResult result = operate.On();
LogHelper.Info(result.ToJson().JsonFormatting()); //转成JSON.JSON格式化
#endregion 打开
#region 读取
//读取
result = operate.Read(address);
LogHelper.Info(result.ToJson().JsonFormatting()); //转成JSON.JSON格式化
#endregion 读取
#region 订阅
//事件信息结果
operate.OnInfoEvent += delegate (object? sender, EventInfoResult e)
{
LogHelper.Info(e.ToJson().JsonFormatting()); //转成JSON.JSON格式化
};
//事件数据结果
operate.OnDataEvent += delegate (object? sender, EventDataResult e)
{
LogHelper.Info(e.ToJson().JsonFormatting()); //转成JSON.JSON格式化
//得到精简版数据 速度很快 <=2ms
IEnumerable<AddressValueSimplify>? simplifies = e.GetSource<ConcurrentDictionary<string, AddressValue>>()?.GetSimplifyArray();
};
result = operate.Subscribe(address);
LogHelper.Info(result.ToJson().JsonFormatting()); //转成JSON.JSON格式化
#endregion 订阅
#region 写入
ConcurrentDictionary<string, object> value = new ConcurrentDictionary<string, object>
{
["地址"] = "string 值",
["地址"] = (float)1.1f,
["地址"] = (double)2.2d,
["地址"] = (int)3,
["地址"] = true
};
result = operate.Write(value);
LogHelper.Info(result.ToJson().JsonFormatting()); //转成JSON.JSON格式化
#endregion 写入
#region 关闭
result = operate.Off();
LogHelper.Info(result.ToJson().JsonFormatting()); //转成JSON.JSON格式化
#endregion 关闭
#region 获取状态
result = operate.GetStatus();
LogHelper.Info(result.ToJson().JsonFormatting()); //转成JSON.JSON格式化
#endregion 获取状态
#region 获取参数
result = operate.GetParam();
LogHelper.Info(result.GetRData<ParamStructure>().ToJson().JsonFormatting()); //转成JSON.JSON格式化
#endregion 获取参数
}
//转发协议
//以MQTT为例
using Snet.Log;
using Snet.Model.data;
using Snet.Mqtt.client;
using Snet.Unility;
using (MqttClientOperate operate = new MqttClientOperate(new MqttClientData.Basics
{
Ip = "127.0.0.1",
Port = 11819,
UserName = "user",
Password = "password"
}))
{
#region 打开
OperateResult result = operate.On();
LogHelper.Info(result.ToJson().JsonFormatting()); //转成JSON.JSON格式化
#endregion 打开
#region 生产
result = operate.Produce("主题", "内容");
LogHelper.Info(result.ToJson().JsonFormatting()); //转成JSON.JSON格式化
#endregion 生产
#region 消费
//事件信息结果
operate.OnInfoEvent += delegate (object? sender, EventInfoResult e)
{
LogHelper.Info(e.ToJson().JsonFormatting()); //转成JSON.JSON格式化
};
//事件数据结果
operate.OnDataEvent += delegate (object? sender, EventDataResult e)
{
LogHelper.Info(e.ToJson().JsonFormatting()); //转成JSON.JSON格式化
//根据传进的RRT 转发响应类型来判断
switch (basics.RT)
{
case ResponseType.Bytes: //字节数据
byte[] bytes = e.GetRData<byte[]>();
break;
case ResponseType.Content: //字符串数据
string str = e.GetRData<string>();
break;
case ResponseType.ContentWithTopic: //带主题与内容的包体数据
ResponseModel rm = e.RData.ToJsonEntity<ResponseModel>();
break;
}
};
result = operate.Subscribe("主题");
LogHelper.Info(result.ToJson().JsonFormatting()); //转成JSON.JSON格式化
#endregion 消费
#region 关闭
result = operate.Off();
LogHelper.Info(result.ToJson().JsonFormatting()); //转成JSON.JSON格式化
#endregion 关闭
#region 获取状态
result = operate.GetStatus();
LogHelper.Info(result.ToJson().JsonFormatting()); //转成JSON.JSON格式化
#endregion 获取状态
#region 获取参数
result = operate.GetParam();
LogHelper.Info(result.GetRData<ParamStructure>().ToJson().JsonFormatting()); //转成JSON.JSON格式化
#endregion 获取参数
}
//更新,无休眠时间
while(true)
{
//三大步骤
1.优化
2.更新
3.提交
}
复制