|
|
@@ -1,73 +1,211 @@
|
|
|
|
|
|
|
|
|
-//对象实例
|
|
|
+
|
|
|
using S7.Net;
|
|
|
using System.Collections.Concurrent;
|
|
|
using YSAI.Core.data;
|
|
|
using YSAI.Core.@enum;
|
|
|
+using YSAI.DB;
|
|
|
+using YSAI.Log;
|
|
|
+using YSAI.Mqtt.client;
|
|
|
using YSAI.S7.client;
|
|
|
using YSAI.Unility;
|
|
|
|
|
|
-S7ClientOperate s7ClientOperate = S7ClientOperate.Instance(new S7ClientData.Basics()
|
|
|
+//采集
|
|
|
+using (类 operate = new 类(new 类数据.Basics
|
|
|
{
|
|
|
- Ip = "192.168.2.20",
|
|
|
- Port = 102,
|
|
|
- Rack = 0,
|
|
|
- Slot = 1,
|
|
|
- S7CpuType = CpuType.S71200,
|
|
|
- ChangeOut = false,
|
|
|
- HandleInterval=1,
|
|
|
- SN = Guid.NewGuid().ToString()
|
|
|
-}); ;
|
|
|
|
|
|
-//打开
|
|
|
-OperateResult operateResult = s7ClientOperate.On();
|
|
|
-Console.WriteLine(operateResult.Message);
|
|
|
-
|
|
|
-//点位地址
|
|
|
-Address address = new Address();
|
|
|
-address.SN = Guid.NewGuid().ToString();
|
|
|
-address.CreationTime = DateTime.Now.ToLocalTime();
|
|
|
-address.AddressArray = new List<AddressDetails>();
|
|
|
-address.AddressArray.Add(new AddressDetails()
|
|
|
-{
|
|
|
- AddressName = "DB71.DBD4",
|
|
|
- AddressDataType = YSAI.Core.@enum.DataType.String,
|
|
|
- AddressType = AddressType.Reality
|
|
|
-});
|
|
|
-
|
|
|
-//读取
|
|
|
-operateResult = s7ClientOperate.Read(address);
|
|
|
-Console.WriteLine(operateResult.Message);
|
|
|
-if (operateResult.State)
|
|
|
+ //采集对象参数
|
|
|
+
|
|
|
+}))
|
|
|
{
|
|
|
- foreach (var item in operateResult.RData as ConcurrentDictionary<string, AddressValue>)
|
|
|
+ //打开
|
|
|
+ OperateResult result = operate.On();
|
|
|
+ LogHelper.Info(result.ToJson().JsonFormatting());
|
|
|
+
|
|
|
+ //点位地址
|
|
|
+ Address address = new Address();
|
|
|
+ address.SN = Guid.NewGuid().ToString();
|
|
|
+ address.CreationTime = DateTime.Now.ToLocalTime();
|
|
|
+ address.AddressArray = new List<AddressDetails>
|
|
|
{
|
|
|
- Console.WriteLine(item.Key);
|
|
|
- Console.WriteLine(item.Value.Value);
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
-//订阅
|
|
|
-s7ClientOperate.OnEvent += S7ClientOperate_OnEvent;
|
|
|
-s7ClientOperate.Subscribe(address);
|
|
|
+ new AddressDetails()//S7点位地址
|
|
|
+ {
|
|
|
+ SN=$"{Guid.NewGuid().ToNString()} - Factory",
|
|
|
+ AddressName = "DB71.DBD4",
|
|
|
+ AddressDataType = YSAI.Core.@enum.DataType.Float
|
|
|
+ },
|
|
|
+ new AddressDetails()//OpcUa点位地址
|
|
|
+ {
|
|
|
+ SN=$"{Guid.NewGuid().ToNString()} - Factory",
|
|
|
+ AddressName = "ns=2;s=Scalar_Simulation_Float",
|
|
|
+ AddressDataType = YSAI.Core.@enum.DataType.Float
|
|
|
+ },
|
|
|
+ new AddressDetails()//OpcDa点位地址
|
|
|
+ {
|
|
|
+ SN=$"{Guid.NewGuid().ToNString()} - Factory",
|
|
|
+ AddressName = "a.a.a",
|
|
|
+ AddressDataType = YSAI.Core.@enum.DataType.Float
|
|
|
+ },
|
|
|
+ new AddressDetails()//Modbus点位地址
|
|
|
+ {
|
|
|
+ SN=$"{Guid.NewGuid().ToNString()} - Factory",
|
|
|
+ AddressName = "0,2",
|
|
|
+ AddressDataType = YSAI.Core.@enum.DataType.Float
|
|
|
+ },
|
|
|
+ new AddressDetails() //DB点位地址
|
|
|
+ {
|
|
|
+ SN=$"{Guid.NewGuid().ToNString()} - Factory",
|
|
|
+ AddressName = "数据库采集",
|
|
|
+ AddressDataType = YSAI.Core.@enum.DataType.String,
|
|
|
+ AddressExtendParam=new DBData.ExtendParam.Read()
|
|
|
+ {
|
|
|
+ Sql = @"SELECT A,B,C,D FORM TABLE ORDER BY YPBH DESC LIMIT 0,1",
|
|
|
+ ColumnName = new List<string>() { "A", "B", "C", "D"}
|
|
|
+ }
|
|
|
+ },
|
|
|
+ new AddressDetails() //地址详情参数介绍
|
|
|
+ {
|
|
|
+ SN=$"A88", //可以理解成唯一标识符(可以存机台号、组名、车间、厂)
|
|
|
+ AddressAnotherName="A_Name", //地址别名
|
|
|
+ AddressDataType=YSAI.Core.@enum.DataType.Float, //数据类型
|
|
|
+ AddressDescribe="NameDescribe", //地址描述
|
|
|
+ AddressExtendParam=new object(), //扩展数据
|
|
|
+ AddressName="Name", //地址名称
|
|
|
+ //AddressParseParam=new AddressParse //地址解析参数
|
|
|
+ //{
|
|
|
+ // ParseType=ParseType.ScriptAnalysis, //解析类型
|
|
|
+ // Script=new YSAI.Core.script.ScriptData.Basics //脚本解析数据
|
|
|
+ // {
|
|
|
+ // }
|
|
|
+ //},
|
|
|
+ AddressParseParam=new AddressParse //地址解析参数
|
|
|
+ {
|
|
|
+ ParseType=ParseType.MethodAnalysis, //解析类型
|
|
|
+ ReflectionSN="反射SN"
|
|
|
+ },
|
|
|
+ AddressPropertyName="Name1", //地址别名
|
|
|
+ AddressType=AddressType.Reality, //地址类型
|
|
|
+ IsEnable=true //是否启用
|
|
|
+ }
|
|
|
+ };
|
|
|
+
|
|
|
+ //读取
|
|
|
+ result = operate.Read(address);
|
|
|
+ LogHelper.Info(result.ToJson().JsonFormatting());
|
|
|
+
|
|
|
+ //订阅
|
|
|
+ operate.OnEvent += delegate (object? sender, EventResult e)
|
|
|
+ {
|
|
|
+ LogHelper.Info(e.ToJson().JsonFormatting());
|
|
|
+ };
|
|
|
+ result = operate.Subscribe(address);
|
|
|
+ LogHelper.Info(result.ToJson().JsonFormatting());
|
|
|
|
|
|
-while (true)
|
|
|
-{
|
|
|
- string value= Console.ReadLine();
|
|
|
//写入
|
|
|
- ConcurrentDictionary<string, string> keyValuePairs = new ConcurrentDictionary<string, string>();
|
|
|
- keyValuePairs.TryAdd("DB71.DBD4", value);
|
|
|
- operateResult = s7ClientOperate.Write(keyValuePairs);
|
|
|
- Console.WriteLine(operateResult.Message);
|
|
|
+ ConcurrentDictionary<string, string> value = new ConcurrentDictionary<string, string>
|
|
|
+ {
|
|
|
+ ["地址"] = "值"
|
|
|
+ };
|
|
|
+ result = operate.Write(value);
|
|
|
+ LogHelper.Info(result.ToJson().JsonFormatting());
|
|
|
+
|
|
|
+ //关闭
|
|
|
+ result = operate.Off();
|
|
|
+ LogHelper.Info(result.ToJson().JsonFormatting());
|
|
|
}
|
|
|
|
|
|
-void S7ClientOperate_OnEvent(object? sender, EventResult e)
|
|
|
+//转发
|
|
|
+using (类 operate = new 类(new 类数据.Basics
|
|
|
{
|
|
|
- Console.WriteLine(e.ToJson());
|
|
|
+
|
|
|
+ //转发对象参数
|
|
|
+
|
|
|
+}))
|
|
|
+{
|
|
|
+ //打开
|
|
|
+ OperateResult result = operate.On();
|
|
|
+ LogHelper.Info(result.ToJson().JsonFormatting());
|
|
|
+
|
|
|
+ //生产
|
|
|
+ result = operate.Produce("主题", "内容");
|
|
|
+ LogHelper.Info(result.ToJson().JsonFormatting());
|
|
|
+
|
|
|
+ //消费
|
|
|
+ operate.OnEvent += delegate (object? sender, EventResult e)
|
|
|
+ {
|
|
|
+ LogHelper.Info(e.ToJson().JsonFormatting());
|
|
|
+ };
|
|
|
+ result = operate.Subscribe("主题");
|
|
|
+ LogHelper.Info(result.ToJson().JsonFormatting());
|
|
|
+
|
|
|
+ //关闭
|
|
|
+ result = operate.Off();
|
|
|
+ LogHelper.Info(result.ToJson().JsonFormatting());
|
|
|
}
|
|
|
|
|
|
|
|
|
+
|
|
|
+// S7ClientOperate s7ClientOperate = S7ClientOperate.Instance(new S7ClientData.Basics()
|
|
|
+// {
|
|
|
+// Ip = "192.168.2.20",
|
|
|
+// Port = 102,
|
|
|
+// Rack = 0,
|
|
|
+// Slot = 1,
|
|
|
+// S7CpuType = CpuType.S71200,
|
|
|
+// ChangeOut = false,
|
|
|
+// HandleInterval = 1,
|
|
|
+// SN = Guid.NewGuid().ToString()
|
|
|
+// }); ;
|
|
|
+
|
|
|
+////打开
|
|
|
+//OperateResult operateResult = s7ClientOperate.On();
|
|
|
+//Console.WriteLine(operateResult.Message);
|
|
|
+
|
|
|
+////点位地址
|
|
|
+//Address address = new Address();
|
|
|
+//address.SN = Guid.NewGuid().ToString();
|
|
|
+//address.CreationTime = DateTime.Now.ToLocalTime();
|
|
|
+//address.AddressArray = new List<AddressDetails>();
|
|
|
+//address.AddressArray.Add(new AddressDetails()
|
|
|
+//{
|
|
|
+// AddressName = "DB71.DBD4",
|
|
|
+// AddressDataType = YSAI.Core.@enum.DataType.String,
|
|
|
+// AddressType = AddressType.Reality
|
|
|
+//});
|
|
|
+
|
|
|
+////读取
|
|
|
+//operateResult = s7ClientOperate.Read(address);
|
|
|
+//Console.WriteLine(operateResult.Message);
|
|
|
+//if (operateResult.State)
|
|
|
+//{
|
|
|
+// foreach (var item in operateResult.RData as ConcurrentDictionary<string, AddressValue>)
|
|
|
+// {
|
|
|
+// Console.WriteLine(item.Key);
|
|
|
+// Console.WriteLine(item.Value.Value);
|
|
|
+// }
|
|
|
+//}
|
|
|
+
|
|
|
+////订阅
|
|
|
+//s7ClientOperate.OnEvent += S7ClientOperate_OnEvent;
|
|
|
+//s7ClientOperate.Subscribe(address);
|
|
|
+
|
|
|
+//while (true)
|
|
|
+//{
|
|
|
+// string value= Console.ReadLine();
|
|
|
+// //写入
|
|
|
+// ConcurrentDictionary<string, string> keyValuePairs = new ConcurrentDictionary<string, string>();
|
|
|
+// keyValuePairs.TryAdd("DB71.DBD4", value);
|
|
|
+// operateResult = s7ClientOperate.Write(keyValuePairs);
|
|
|
+// Console.WriteLine(operateResult.Message);
|
|
|
+//}
|
|
|
+
|
|
|
+//void S7ClientOperate_OnEvent(object? sender, EventResult e)
|
|
|
+//{
|
|
|
+// Console.WriteLine(e.ToJson());
|
|
|
+//}
|
|
|
+
|
|
|
+
|
|
|
//WsServiceOperate wsServiceOperate = WsServiceOperate.Instance(new WsServiceData.Basics
|
|
|
//{
|
|
|
// Ip = "127.0.0.1",
|