更新示例
This commit is contained in:
@@ -1,42 +0,0 @@
|
||||
//------------------------------------------------------------------------------
|
||||
// 此代码版权声明为全文件覆盖,如有原作者特别声明,会在下方手动补充
|
||||
// 此代码版权(除特别声明外的代码)归作者本人Diego所有
|
||||
// 源代码使用协议遵循本仓库的开源协议及附加协议
|
||||
// Gitee源代码仓库:https://gitee.com/diego2098/ThingsGateway
|
||||
// Github源代码仓库:https://github.com/kimdiego2098/ThingsGateway
|
||||
// 使用文档:https://diego2098.gitee.io/thingsgateway-docs/
|
||||
// QQ群:605534569
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
using ThingsGateway.Foundation.Dlt645;
|
||||
|
||||
using TouchSocket.Core;
|
||||
|
||||
namespace ThingsGateway.Foundation
|
||||
{
|
||||
internal class Dlt645MasterTest
|
||||
{
|
||||
/// <summary>
|
||||
/// 新建链路
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public IChannel GetChannel()
|
||||
{
|
||||
TouchSocketConfig touchSocketConfig = new TouchSocketConfig();
|
||||
return touchSocketConfig.GetSerialPortWithOption(new("COM1")); //直接获取串口对象
|
||||
//return touchSocketConfig.GetChannel(ChannelTypeEnum.SerialPortClient, null, null, new("COM1"));//通过链路枚举获取对象
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 新建协议对象
|
||||
/// </summary>
|
||||
/// <param name="channel"></param>
|
||||
/// <returns></returns>
|
||||
public IProtocol GetProtocol(IChannel channel)
|
||||
{
|
||||
var client = new Dlt645_2007Master(channel);
|
||||
client.Station = "311111111114";//表号
|
||||
return client;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,82 @@
|
||||
//------------------------------------------------------------------------------
|
||||
// 此代码版权声明为全文件覆盖,如有原作者特别声明,会在下方手动补充
|
||||
// 此代码版权(除特别声明外的代码)归作者本人Diego所有
|
||||
// 源代码使用协议遵循本仓库的开源协议及附加协议
|
||||
// Gitee源代码仓库:https://gitee.com/diego2098/ThingsGateway
|
||||
// Github源代码仓库:https://github.com/kimdiego2098/ThingsGateway
|
||||
// 使用文档:https://diego2098.gitee.io/thingsgateway-docs/
|
||||
// QQ群:605534569
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
using ThingsGateway.Foundation.Modbus;
|
||||
|
||||
using TouchSocket.Core;
|
||||
|
||||
namespace ThingsGateway.Foundation
|
||||
{
|
||||
internal class ModbusMasterTest
|
||||
{
|
||||
private static ModbusMaster GetMaster()
|
||||
{
|
||||
var clientConfig = new TouchSocketConfig();
|
||||
clientConfig.ConfigureContainer(a => a.AddConsoleLogger());
|
||||
//创建通道,也可以通过TouchSocketConfig.GetChannel扩展获取
|
||||
var clientChannel = clientConfig.GetTcpClientWithIPHost("tcp://127.0.0.1:502");
|
||||
//var clientChannel = clientConfig.GetSerialPortWithOption("COM1");
|
||||
//clientChannel.Logger.LogLevel = LogLevel.Trace;
|
||||
ModbusMaster modbusMaster = new(clientChannel)
|
||||
{
|
||||
//modbus协议格式
|
||||
ModbusType = Modbus.ModbusTypeEnum.ModbusRtu,
|
||||
//ModbusType = Modbus.ModbusTypeEnum.ModbusTcp,
|
||||
};
|
||||
return modbusMaster;
|
||||
}
|
||||
|
||||
public static void Test()
|
||||
{
|
||||
using ModbusMaster modbusMaster = GetMaster();
|
||||
using ModbusMaster modbusMaster1 = GetMaster();
|
||||
modbusMaster1.Station = 2;
|
||||
//构造实体类对象,传入协议对象与连读打包的最大数量
|
||||
ModbusVariable modbusVariable = new(modbusMaster, 100);
|
||||
ModbusVariable modbusVariable1 = new(modbusMaster1, 100);
|
||||
|
||||
Test(modbusVariable, 10);
|
||||
Test(modbusVariable1, 100);
|
||||
Console.WriteLine(modbusVariable.ToJsonString());
|
||||
Console.WriteLine(modbusVariable1.ToJsonString());
|
||||
Console.ReadLine();
|
||||
|
||||
static void Test(ModbusVariable modbusVariable, ushort value)
|
||||
{
|
||||
modbusVariable.WriteData1(value, default);
|
||||
modbusVariable.WriteData2(value, default);
|
||||
|
||||
//执行连读
|
||||
modbusVariable.MulRead();
|
||||
Console.WriteLine(modbusVariable.ToJsonString());
|
||||
//源生成WriteData1与WriteData2方法(Write{属性名称})
|
||||
var data1 = modbusVariable.WriteData1(value + 10, default);
|
||||
var data2 = modbusVariable.WriteData2(value + 10, default);
|
||||
//执行连读
|
||||
modbusVariable.MulRead();
|
||||
Console.WriteLine(modbusVariable.ToJsonString());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[GeneratorVariable]
|
||||
public partial class ModbusVariable : VariableObject
|
||||
{
|
||||
[VariableRuntime(RegisterAddress = "400001")]
|
||||
public ushort Data1 { get; set; }
|
||||
|
||||
[VariableRuntime(RegisterAddress = "400051")]
|
||||
public ushort Data2 { get; set; }
|
||||
|
||||
public ModbusVariable(IProtocol protocol, int maxPack) : base(protocol, maxPack)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,61 +0,0 @@
|
||||
//------------------------------------------------------------------------------
|
||||
// 此代码版权声明为全文件覆盖,如有原作者特别声明,会在下方手动补充
|
||||
// 此代码版权(除特别声明外的代码)归作者本人Diego所有
|
||||
// 源代码使用协议遵循本仓库的开源协议及附加协议
|
||||
// Gitee源代码仓库:https://gitee.com/diego2098/ThingsGateway
|
||||
// Github源代码仓库:https://github.com/kimdiego2098/ThingsGateway
|
||||
// 使用文档:https://diego2098.gitee.io/thingsgateway-docs/
|
||||
// QQ群:605534569
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
using Newtonsoft.Json.Linq;
|
||||
|
||||
using ThingsGateway.Foundation.Modbus;
|
||||
|
||||
using TouchSocket.Core;
|
||||
|
||||
namespace ThingsGateway.Foundation
|
||||
{
|
||||
internal class ModbusMatserTest
|
||||
{
|
||||
public async Task Test()
|
||||
{
|
||||
var clientConfig = new TouchSocketConfig();
|
||||
//创建通道,也可以通过TouchSocketConfig.GetChannel扩展获取
|
||||
var clientChannel = clientConfig.GetTcpClientWithIPHost("tcp://127.0.0.1:502");
|
||||
|
||||
//创建modbus客户端,传入通道
|
||||
using ModbusMaster modbusMaster = new(clientChannel)
|
||||
{
|
||||
//modbus协议格式
|
||||
//ModbusType = Modbus.ModbusTypeEnum.ModbusRtu,
|
||||
ModbusType = Modbus.ModbusTypeEnum.ModbusTcp,
|
||||
};
|
||||
|
||||
//测试5千次
|
||||
for (int i = 0; i < 5000; i++)
|
||||
{
|
||||
//读写对应数据类型
|
||||
var result = await modbusMaster.ReadInt32Async("40001", 1);
|
||||
if (!result.IsSuccess)
|
||||
{
|
||||
Console.WriteLine(result);
|
||||
}
|
||||
}
|
||||
|
||||
var wResult = await modbusMaster.WriteAsync("40001", 1);
|
||||
|
||||
//动态类型读写
|
||||
var objResult = await modbusMaster.ReadAsync("40001", 1, DataTypeEnum.Int32);
|
||||
var objWResult = await modbusMaster.WriteAsync("40001", JToken.FromObject(1), DataTypeEnum.Int32);
|
||||
|
||||
//地址说明
|
||||
//单独设置解析顺序
|
||||
var objABCDResult = await modbusMaster.ReadAsync("40001;dataformat=badc", 1, DataTypeEnum.Int32);
|
||||
//单独设置站号
|
||||
var objSResult = await modbusMaster.ReadAsync("40001;dataformat=badc;s=2", 1, DataTypeEnum.Int32);
|
||||
//单独设置写入功能码
|
||||
var objFWResult = await modbusMaster.ReadAsync("40001;s=2;w=16", 1, DataTypeEnum.Int16);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -8,75 +8,14 @@
|
||||
// QQ群:605534569
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
using ThingsGateway.Foundation.Modbus;
|
||||
|
||||
using TouchSocket.Core;
|
||||
|
||||
namespace ThingsGateway.Foundation
|
||||
{
|
||||
internal class Program
|
||||
{
|
||||
private static ModbusMaster GetMaster()
|
||||
{
|
||||
var clientConfig = new TouchSocketConfig();
|
||||
clientConfig.ConfigureContainer(a => a.AddConsoleLogger());
|
||||
//创建通道,也可以通过TouchSocketConfig.GetChannel扩展获取
|
||||
var clientChannel = clientConfig.GetTcpClientWithIPHost("tcp://127.0.0.1:502");
|
||||
//var clientChannel = clientConfig.GetSerialPortWithOption("COM1");
|
||||
//clientChannel.Logger.LogLevel = LogLevel.Trace;
|
||||
ModbusMaster modbusMaster = new(clientChannel)
|
||||
{
|
||||
//modbus协议格式
|
||||
ModbusType = Modbus.ModbusTypeEnum.ModbusRtu,
|
||||
//ModbusType = Modbus.ModbusTypeEnum.ModbusTcp,
|
||||
};
|
||||
return modbusMaster;
|
||||
}
|
||||
|
||||
private static void Main(string[] args)
|
||||
{
|
||||
using ModbusMaster modbusMaster = GetMaster();
|
||||
using ModbusMaster modbusMaster1 = GetMaster();
|
||||
modbusMaster1.Station = 2;
|
||||
//构造实体类对象,传入协议对象与连读打包的最大数量
|
||||
ModbusVariable modbusVariable = new(modbusMaster, 100);
|
||||
ModbusVariable modbusVariable1 = new(modbusMaster1, 100);
|
||||
|
||||
Test(modbusVariable, 10);
|
||||
Test(modbusVariable1, 100);
|
||||
Console.WriteLine(modbusVariable.ToJsonString());
|
||||
Console.WriteLine(modbusVariable1.ToJsonString());
|
||||
Console.ReadLine();
|
||||
|
||||
static void Test(ModbusVariable modbusVariable, ushort value)
|
||||
{
|
||||
modbusVariable.WriteData1(value, default);
|
||||
modbusVariable.WriteData2(value, default);
|
||||
|
||||
//执行连读
|
||||
modbusVariable.MulRead();
|
||||
Console.WriteLine(modbusVariable.ToJsonString());
|
||||
//源生成WriteData1与WriteData2方法(Write{属性名称})
|
||||
var data1 = modbusVariable.WriteData1(value + 10, default);
|
||||
var data2 = modbusVariable.WriteData2(value + 10, default);
|
||||
//执行连读
|
||||
modbusVariable.MulRead();
|
||||
Console.WriteLine(modbusVariable.ToJsonString());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[GeneratorVariable]
|
||||
public partial class ModbusVariable : VariableObject
|
||||
{
|
||||
[VariableRuntime(RegisterAddress = "400001")]
|
||||
public ushort Data1 { get; set; }
|
||||
|
||||
[VariableRuntime(RegisterAddress = "400051")]
|
||||
public ushort Data2 { get; set; }
|
||||
|
||||
public ModbusVariable(IProtocol protocol, int maxPack) : base(protocol, maxPack)
|
||||
{
|
||||
//ModbusMasterTest.Test();
|
||||
S7MatserTest.Test();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,89 @@
|
||||
//------------------------------------------------------------------------------
|
||||
// 此代码版权声明为全文件覆盖,如有原作者特别声明,会在下方手动补充
|
||||
// 此代码版权(除特别声明外的代码)归作者本人Diego所有
|
||||
// 源代码使用协议遵循本仓库的开源协议及附加协议
|
||||
// Gitee源代码仓库:https://gitee.com/diego2098/ThingsGateway
|
||||
// Github源代码仓库:https://github.com/kimdiego2098/ThingsGateway
|
||||
// 使用文档:https://diego2098.gitee.io/thingsgateway-docs/
|
||||
// QQ群:605534569
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
using ThingsGateway.Foundation.SiemensS7;
|
||||
|
||||
using TouchSocket.Core;
|
||||
|
||||
namespace ThingsGateway.Foundation
|
||||
{
|
||||
internal class S7MatserTest
|
||||
{
|
||||
private static void Test(S7Variable s7Variable, ushort value)
|
||||
{
|
||||
s7Variable.WriteData1(value, default);
|
||||
s7Variable.WriteData2(value, default);
|
||||
|
||||
//执行连读
|
||||
s7Variable.MulRead();
|
||||
Console.WriteLine(s7Variable.ToJsonString());
|
||||
//源生成WriteData1与WriteData2方法(Write{属性名称})
|
||||
var data1 = s7Variable.WriteData1(value + 10, default);
|
||||
var data2 = s7Variable.WriteData2(value + 10, default);
|
||||
//执行连读
|
||||
s7Variable.MulRead();
|
||||
Console.WriteLine(s7Variable.ToJsonString());
|
||||
}
|
||||
|
||||
private static SiemensS7Master GetMaster()
|
||||
{
|
||||
var clientConfig = new TouchSocketConfig();
|
||||
clientConfig.ConfigureContainer(a => a.AddConsoleLogger());
|
||||
//创建通道,也可以通过TouchSocketConfig.GetChannel扩展获取
|
||||
var clientChannel = clientConfig.GetTcpClientWithIPHost("tcp://127.0.0.1:102");
|
||||
//clientChannel.Logger.LogLevel = LogLevel.Trace;
|
||||
SiemensS7Master siemensS7Master = new(clientChannel)
|
||||
{
|
||||
SiemensType = SiemensTypeEnum.S1500,
|
||||
};
|
||||
return siemensS7Master;
|
||||
}
|
||||
public static void Test()
|
||||
{
|
||||
using SiemensS7Master siemensS7Master = GetMaster();
|
||||
//构造实体类对象,传入协议对象与连读打包的最大数量
|
||||
S7Variable s7Variable = new(siemensS7Master, 100);
|
||||
|
||||
Test(s7Variable, 10);
|
||||
Console.ReadLine();
|
||||
|
||||
static void Test(S7Variable s7Variable, ushort value)
|
||||
{
|
||||
s7Variable.WriteData1(value, default);
|
||||
s7Variable.WriteData2(value, default);
|
||||
|
||||
//执行连读
|
||||
s7Variable.MulRead();
|
||||
Console.WriteLine(s7Variable.ToJsonString());
|
||||
//源生成WriteData1与WriteData2方法(Write{属性名称})
|
||||
var data1 = s7Variable.WriteData1(value + 10, default);
|
||||
var data2 = s7Variable.WriteData2(value + 10, default);
|
||||
//执行连读
|
||||
s7Variable.MulRead();
|
||||
Console.WriteLine(s7Variable.ToJsonString());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
[GeneratorVariable]
|
||||
public partial class S7Variable : VariableObject
|
||||
{
|
||||
[VariableRuntime(RegisterAddress = "M100")]
|
||||
public ushort Data1 { get; set; }
|
||||
|
||||
[VariableRuntime(RegisterAddress = "M200")]
|
||||
public ushort Data2 { get; set; }
|
||||
|
||||
public S7Variable(IProtocol protocol, int maxPack) : base(protocol, maxPack)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -12,6 +12,7 @@
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\ThingsGateway.Foundation.Dlt645\src\ThingsGateway.Foundation.Dlt645.csproj" />
|
||||
<ProjectReference Include="..\..\ThingsGateway.Foundation.Modbus\src\ThingsGateway.Foundation.Modbus.csproj" />
|
||||
<ProjectReference Include="..\..\ThingsGateway.Foundation.SiemensS7\src\ThingsGateway.Foundation.SiemensS7.csproj" />
|
||||
<ProjectReference Include="..\..\ThingsGateway.Foundation.SourceGenerator\src\ThingsGateway.Foundation.SourceGenerator.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
|
||||
</ItemGroup>
|
||||
|
||||
|
||||
Reference in New Issue
Block a user