HJ212 and Documents
This commit is contained in:
@@ -1,42 +1,83 @@
|
||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
using Modbus.Net.Modbus;
|
||||
using System.Reflection;
|
||||
using AddressUnit = Modbus.Net.AddressUnit<int, int, int>;
|
||||
|
||||
namespace Modbus.Net.Tests
|
||||
{
|
||||
/// <summary>
|
||||
/// 机器方法测试类
|
||||
/// 测试 Machine 层的反射调用和 Utility 层的直接调用
|
||||
/// </summary>
|
||||
[TestClass]
|
||||
public class MachineMethodTest
|
||||
{
|
||||
// 测试机器 IP 地址
|
||||
private string _machineIp = "10.10.18.251";
|
||||
|
||||
/// <summary>
|
||||
/// 测试获取 Utility 方法
|
||||
/// 验证可以通过反射获取 Utility 的读写方法
|
||||
/// </summary>
|
||||
[TestMethod]
|
||||
public void GetUtility()
|
||||
{
|
||||
// 创建 Modbus TCP 机器实例
|
||||
BaseMachine<int, int> baseMachine = new ModbusMachine<int, int>(1, "", ModbusType.Tcp, _machineIp, null, true, 2, 0, Endian.BigEndianLsb);
|
||||
|
||||
// 获取 Utility 方法接口
|
||||
var utility = baseMachine.GetUtilityMethods<IUtilityMethodDatas>();
|
||||
|
||||
// 通过反射获取方法列表
|
||||
var methods = utility.GetType().GetRuntimeMethods();
|
||||
|
||||
// 验证 GetDatasAsync 方法存在
|
||||
Assert.AreEqual(methods.FirstOrDefault(method => method.Name == "GetDatasAsync") != null, true);
|
||||
|
||||
// 验证 SetDatasAsync 方法存在
|
||||
Assert.AreEqual(methods.FirstOrDefault(method => method.Name == "SetDatasAsync") != null, true);
|
||||
|
||||
// 断开连接
|
||||
baseMachine.Disconnect();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 测试调用 Utility 方法
|
||||
/// 验证可以直接调用 Utility 的底层读写方法
|
||||
/// </summary>
|
||||
[TestMethod]
|
||||
public async Task InvokeUtility()
|
||||
{
|
||||
// 创建 Modbus TCP 机器实例
|
||||
BaseMachine<int, int> baseMachine = new ModbusMachine<int, int>(1, "", ModbusType.Tcp, _machineIp, null, true, 2, 0, Endian.BigEndianLsb);
|
||||
|
||||
// 连接 Utility
|
||||
await baseMachine.BaseUtility.ConnectAsync();
|
||||
|
||||
// 使用 Utility 方法写入数据(字节级 API)
|
||||
// 地址格式:"4X 1" = 保持寄存器区地址 1
|
||||
// 写入值:byte 类型的 11
|
||||
var success = await baseMachine.BaseUtility.GetUtilityMethods<IUtilityMethodDatas>().SetDatasAsync("4X 1", new object[] { (byte)11 }, 1);
|
||||
Assert.AreEqual(success.IsSuccess, true);
|
||||
|
||||
// 使用 Utility 方法读取数据
|
||||
// 读取 1 个字节
|
||||
var datas = await baseMachine.BaseUtility.GetUtilityMethods<IUtilityMethodDatas>().GetDatasAsync("4X 1", 1, 1);
|
||||
Assert.AreEqual(datas.Datas[0], 11);
|
||||
|
||||
// 断开连接
|
||||
baseMachine.Disconnect();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 测试调用 Machine 方法
|
||||
/// 验证可以调用 Machine 层的高级读写方法
|
||||
/// </summary>
|
||||
[TestMethod]
|
||||
public async Task InvokeMachine()
|
||||
{
|
||||
BaseMachine<int, int> baseMachine = new ModbusMachine<int, int>(1, "",ModbusType.Tcp, _machineIp, new List<AddressUnit>
|
||||
// 创建 Modbus TCP 机器实例,带地址配置
|
||||
BaseMachine<int, int> baseMachine = new ModbusMachine<int, int>(1, "", ModbusType.Tcp, _machineIp, new List<AddressUnit>
|
||||
{
|
||||
new AddressUnit
|
||||
{
|
||||
@@ -48,26 +89,35 @@ namespace Modbus.Net.Tests
|
||||
DataType = typeof(bool)
|
||||
}
|
||||
}, true, 2, 0, Endian.BigEndianLsb);
|
||||
|
||||
// 使用 Machine 方法写入数据(高级 API)
|
||||
// 按地址写入线圈状态
|
||||
var success = await baseMachine.GetMachineMethods<IMachineMethodDatas>().SetDatasAsync(
|
||||
MachineDataType.Address,
|
||||
new Dictionary<string, double>
|
||||
{
|
||||
{
|
||||
"0X 1.0", 1
|
||||
"0X 1.0", 1 // 线圈 0X 1.0 设置为 ON
|
||||
}
|
||||
});
|
||||
Assert.AreEqual(success.IsSuccess, true);
|
||||
|
||||
// 使用 Machine 方法读取数据
|
||||
var datas = await baseMachine.GetMachineMethods<IMachineMethodDatas>().GetDatasAsync(MachineDataType.Address);
|
||||
Assert.AreEqual(datas.Datas["0X 1.0"].DeviceValue, 1);
|
||||
|
||||
// 写入 OFF
|
||||
success = await baseMachine.GetMachineMethods<IMachineMethodDatas>().SetDatasAsync(
|
||||
MachineDataType.Address,
|
||||
new Dictionary<string, double>
|
||||
{
|
||||
{
|
||||
"0X 1.0", 0
|
||||
"0X 1.0", 0 // 线圈 0X 1.0 设置为 OFF
|
||||
}
|
||||
});
|
||||
Assert.AreEqual(success.IsSuccess, true);
|
||||
|
||||
// 断开连接
|
||||
baseMachine.Disconnect();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user