159 lines
5.9 KiB
C#
159 lines
5.9 KiB
C#
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
|
||
{
|
||
Id = "0",
|
||
Area = "4X",
|
||
Address = 2,
|
||
SubAddress = 0,
|
||
CommunicationTag = "A1",
|
||
DataType = typeof(ushort)
|
||
},
|
||
new AddressUnit
|
||
{
|
||
Id = "1",
|
||
Area = "4X",
|
||
Address = 3,
|
||
SubAddress = 0,
|
||
CommunicationTag = "A2",
|
||
DataType = typeof(ushort)
|
||
},
|
||
new AddressUnit
|
||
{
|
||
Id = "2",
|
||
Area = "4X",
|
||
Address = 4,
|
||
SubAddress = 0,
|
||
CommunicationTag = "A3",
|
||
DataType = typeof(ushort)
|
||
},
|
||
new AddressUnit
|
||
{
|
||
Id = "3",
|
||
Area = "4X",
|
||
Address = 5,
|
||
SubAddress = 0,
|
||
CommunicationTag = "A4",
|
||
DataType = typeof(ushort)
|
||
},
|
||
new AddressUnit
|
||
{
|
||
Id = "4",
|
||
Area = "4X",
|
||
Address = 6,
|
||
SubAddress = 0,
|
||
CommunicationTag = "A5",
|
||
DataType = typeof(uint)
|
||
},
|
||
new AddressUnit
|
||
{
|
||
Id = "5",
|
||
Area = "4X",
|
||
Address = 8,
|
||
SubAddress = 0,
|
||
CommunicationTag = "A6",
|
||
DataType = typeof(uint)
|
||
}
|
||
};
|
||
|
||
// 设置两个从站的地址列表
|
||
_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() }, // 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() }
|
||
};
|
||
|
||
// 分别向两个从站写入不同的数据
|
||
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"]);
|
||
Assert.AreEqual(ans2.Datas["A2"].DeviceValue, dic2["A2"]);
|
||
Assert.AreEqual(ans.Datas["A3"].DeviceValue, dic1["A3"]);
|
||
Assert.AreEqual(ans2.Datas["A3"].DeviceValue, dic2["A3"]);
|
||
Assert.AreEqual(ans.Datas["A4"].DeviceValue, dic1["A4"]);
|
||
Assert.AreEqual(ans2.Datas["A4"].DeviceValue, dic2["A4"]);
|
||
Assert.AreEqual(ans.Datas["A5"].DeviceValue, dic1["A5"]);
|
||
Assert.AreEqual(ans2.Datas["A5"].DeviceValue, dic2["A5"]);
|
||
Assert.AreEqual(ans.Datas["A6"].DeviceValue, dic1["A6"]);
|
||
Assert.AreEqual(ans2.Datas["A6"].DeviceValue, dic2["A6"]);
|
||
}
|
||
}
|
||
}
|