HJ212 and Documents
This commit is contained in:
@@ -1,28 +1,47 @@
|
||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
using Modbus.Net.Modbus;
|
||||
using AddressUnit = Modbus.Net.AddressUnit<string, int, int>;
|
||||
|
||||
namespace Modbus.Net.Tests
|
||||
{
|
||||
/// <summary>
|
||||
/// Modbus 多从站测试类
|
||||
/// 测试在同一串口上访问多个 Modbus RTU 从站
|
||||
/// </summary>
|
||||
[TestClass]
|
||||
public class ModbusMultiStationTest
|
||||
{
|
||||
// Modbus RTU 机器实例(从站 1)
|
||||
private BaseMachine<string, string>? _modbusRtuMachine1;
|
||||
|
||||
// Modbus RTU 机器实例(从站 2)
|
||||
private BaseMachine<string, string>? _modbusRtuMachine2;
|
||||
|
||||
// 测试串口
|
||||
private string _machineCom = "COM1";
|
||||
|
||||
/// <summary>
|
||||
/// 测试初始化方法
|
||||
/// 创建两个不同从站地址的 Modbus RTU 机器实例
|
||||
/// </summary>
|
||||
[TestInitialize]
|
||||
public void Init()
|
||||
{
|
||||
// 创建从站地址为 1 的 Modbus RTU 机器
|
||||
_modbusRtuMachine1 = new ModbusMachine<string, string>("1", "", ModbusType.Rtu, _machineCom, null, true, 1, 0, Endian.BigEndianLsb);
|
||||
|
||||
// 创建从站地址为 2 的 Modbus RTU 机器
|
||||
_modbusRtuMachine2 = new ModbusMachine<string, string>("2", "", ModbusType.Rtu, _machineCom, null, true, 2, 0, Endian.BigEndianLsb);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 测试多从站读写
|
||||
/// 验证在同一串口上可以分别访问不同从站地址的设备
|
||||
/// </summary>
|
||||
[TestMethod]
|
||||
public async Task MultiStation()
|
||||
{
|
||||
// 定义地址单元列表
|
||||
var addresses = new List<AddressUnit>
|
||||
{
|
||||
new AddressUnit
|
||||
@@ -81,63 +100,47 @@ namespace Modbus.Net.Tests
|
||||
}
|
||||
};
|
||||
|
||||
// 设置两个从站的地址列表
|
||||
_modbusRtuMachine1!.GetAddresses = addresses.ToList();
|
||||
_modbusRtuMachine2!.GetAddresses = addresses.ToList();
|
||||
|
||||
Random r = new Random();
|
||||
|
||||
// 生成从站 1 的随机数据
|
||||
var dic1 = new Dictionary<string, double>()
|
||||
{
|
||||
{
|
||||
"A1", r.Next(0, UInt16.MaxValue)
|
||||
},
|
||||
{
|
||||
"A2", r.Next(0, UInt16.MaxValue)
|
||||
},
|
||||
{
|
||||
"A3", r.Next(0, UInt16.MaxValue)
|
||||
},
|
||||
{
|
||||
"A4", r.Next(0, UInt16.MaxValue)
|
||||
},
|
||||
{
|
||||
"A5", r.Next()
|
||||
},
|
||||
{
|
||||
"A6", r.Next()
|
||||
},
|
||||
{ "A1", r.Next(0, UInt16.MaxValue) },
|
||||
{ "A2", r.Next(0, UInt16.MaxValue) },
|
||||
{ "A3", r.Next(0, UInt16.MaxValue) },
|
||||
{ "A4", r.Next(0, UInt16.MaxValue) },
|
||||
{ "A5", r.Next() }, // uint 类型
|
||||
{ "A6", r.Next() } // uint 类型
|
||||
};
|
||||
|
||||
// 生成从站 2 的随机数据
|
||||
var dic2 = new Dictionary<string, double>()
|
||||
{
|
||||
{
|
||||
"A1", r.Next(0, UInt16.MaxValue)
|
||||
},
|
||||
{
|
||||
"A2", r.Next(0, UInt16.MaxValue)
|
||||
},
|
||||
{
|
||||
"A3", r.Next(0, UInt16.MaxValue)
|
||||
},
|
||||
{
|
||||
"A4", r.Next(0, UInt16.MaxValue)
|
||||
},
|
||||
{
|
||||
"A5", r.Next()
|
||||
},
|
||||
{
|
||||
"A6", r.Next()
|
||||
},
|
||||
{ "A1", r.Next(0, UInt16.MaxValue) },
|
||||
{ "A2", r.Next(0, UInt16.MaxValue) },
|
||||
{ "A3", r.Next(0, UInt16.MaxValue) },
|
||||
{ "A4", r.Next(0, UInt16.MaxValue) },
|
||||
{ "A5", r.Next() },
|
||||
{ "A6", r.Next() }
|
||||
};
|
||||
|
||||
// 分别向两个从站写入不同的数据
|
||||
await _modbusRtuMachine1.SetDatasAsync(MachineDataType.CommunicationTag, dic1);
|
||||
await _modbusRtuMachine2.SetDatasAsync(MachineDataType.CommunicationTag, dic2);
|
||||
|
||||
// 分别从两个从站读取数据
|
||||
var ans = await _modbusRtuMachine1.GetDatasAsync(MachineDataType.CommunicationTag);
|
||||
var ans2 = await _modbusRtuMachine2.GetDatasAsync(MachineDataType.CommunicationTag);
|
||||
|
||||
// 断开连接
|
||||
_modbusRtuMachine1.Disconnect();
|
||||
_modbusRtuMachine2.Disconnect();
|
||||
|
||||
// 验证从站 1 读取的数据与写入的一致
|
||||
Assert.AreEqual(ans.Datas["A1"].DeviceValue, dic1["A1"]);
|
||||
Assert.AreEqual(ans2.Datas["A1"].DeviceValue, dic2["A1"]);
|
||||
Assert.AreEqual(ans.Datas["A2"].DeviceValue, dic1["A2"]);
|
||||
|
||||
Reference in New Issue
Block a user