diff --git a/Modbus.Net/Modbus.Net/Interface/IMachine.cs b/Modbus.Net/Modbus.Net/Interface/IMachine.cs index 9ae0b2b..a68b4dd 100644 --- a/Modbus.Net/Modbus.Net/Interface/IMachine.cs +++ b/Modbus.Net/Modbus.Net/Interface/IMachine.cs @@ -6,7 +6,7 @@ namespace Modbus.Net /// 设备的抽象 /// /// - public interface IMachine : IMachineProperty, IMachineMethodDatas, IMachineReflectionCall where TKey : IEquatable + public interface IMachine : IMachineProperty, IMachineMethodDatas where TKey : IEquatable { } } diff --git a/Modbus.Net/Modbus.Net/Interface/IMachineReflectionCall.cs b/Modbus.Net/Modbus.Net/Interface/IMachineReflectionCall.cs deleted file mode 100644 index bafc560..0000000 --- a/Modbus.Net/Modbus.Net/Interface/IMachineReflectionCall.cs +++ /dev/null @@ -1,29 +0,0 @@ -using System.Threading.Tasks; - -namespace Modbus.Net -{ - /// - /// 设备的反射调用接口 - /// - public interface IMachineReflectionCall - { - /// - /// 反射方式调用获取方法 - /// - /// 要返回的数据类型 - /// 方法名 - /// 参数 - /// 返回的数据 - Task> InvokeGet(string functionName, object[] parameters); - - /// - /// 反射方式调用设置方法 - /// - /// 要设置的数据类型 - /// 方法名 - /// 参数 - /// 要设置的数据 - /// 设置是否成功 - Task> InvokeSet(string functionName, object[] parameters, T datas); - } -} diff --git a/Modbus.Net/Modbus.Net/Interface/IUtility.cs b/Modbus.Net/Modbus.Net/Interface/IUtility.cs index ba39e4c..eeec539 100644 --- a/Modbus.Net/Modbus.Net/Interface/IUtility.cs +++ b/Modbus.Net/Modbus.Net/Interface/IUtility.cs @@ -3,7 +3,7 @@ /// /// Api入口的抽象 /// - public interface IUtility : IUtilityProperty, IUtilityMethodData + public interface IUtility : IUtilityProperty, IUtilityMethodDatas { } } diff --git a/Modbus.Net/Modbus.Net/Interface/IUtilityMethod.cs b/Modbus.Net/Modbus.Net/Interface/IUtilityMethod.cs index 907c4e8..b74e26b 100644 --- a/Modbus.Net/Modbus.Net/Interface/IUtilityMethod.cs +++ b/Modbus.Net/Modbus.Net/Interface/IUtilityMethod.cs @@ -14,7 +14,7 @@ namespace Modbus.Net /// /// Utility的数据读写接口 /// - public interface IUtilityMethodData : IUtilityMethod + public interface IUtilityMethodDatas : IUtilityMethod { /// /// 获取数据 diff --git a/Modbus.Net/Modbus.Net/Job/MachineJobScheduler.cs b/Modbus.Net/Modbus.Net/Job/MachineJobScheduler.cs index 3a5ec4c..ef4032b 100644 --- a/Modbus.Net/Modbus.Net/Job/MachineJobScheduler.cs +++ b/Modbus.Net/Modbus.Net/Job/MachineJobScheduler.cs @@ -140,7 +140,7 @@ namespace Modbus.Net /// 获取数据的方式 /// /// - public async Task> From(string queryId, IMachineReflectionCall machine, MachineDataType machineDataType) + public async Task> From(string queryId, IMachineMethod machine, MachineDataType machineDataType) { JobKey jobKey = JobKey.Create("Modbus.Net.DataQuery.Job." + queryId, "Modbus.Net.DataQuery.Group." + _trigger.Key.Name); @@ -314,7 +314,7 @@ namespace Modbus.Net /// 写入数据的设备实例 /// /// - public async Task> To(string queryId, IMachineReflectionCall machine) + public async Task> To(string queryId, IMachineMethod machine) { JobKey jobKey = JobKey.Create("Modbus.Net.DataQuery.Job." + queryId, "Modbus.Net.DataQuery.Group." + _trigger.Key.Name); @@ -347,7 +347,7 @@ namespace Modbus.Net /// 要获取数据的设备实例 /// 获取数据的方式 /// - public async Task> From(string queryId, IMachineReflectionCall machine, MachineDataType machineDataType) + public async Task> From(string queryId, IMachineMethod machine, MachineDataType machineDataType) { return await new MachineGetJobScheduler(_scheduler, _trigger, _parentJobKey).From(queryId, machine, machineDataType); } @@ -433,7 +433,7 @@ namespace Modbus.Net context.JobDetail.JobDataMap.TryGetValue("Machine", out machine); context.JobDetail.JobDataMap.TryGetValue("DataType", out machineDataType); context.JobDetail.JobDataMap.TryGetValue("Function", out callFunction); - var values = await (machine as IMachineReflectionCall)!.InvokeGet>>((string)callFunction, new object[] { (MachineDataType)machineDataType }); + var values = await (machine as IMachineMethod)!.InvokeGet>>((string)callFunction, new object[] { (MachineDataType)machineDataType }); context.JobDetail.JobDataMap.Put("Value", values); await context.Scheduler.AddJob(context.JobDetail, true, false); @@ -492,7 +492,7 @@ namespace Modbus.Net context.JobDetail.JobDataMap.Put("Success", false); return; } - var success = await (machine as IMachineReflectionCall)!.InvokeSet((string)callFunction, new object[] { (MachineDataType)machineDataType }, (Dictionary)valuesSet); + var success = await (machine as IMachineMethod)!.InvokeSet((string)callFunction, new object[] { (MachineDataType)machineDataType }, (Dictionary)valuesSet); context.JobDetail.JobDataMap.Put("Success", success); } diff --git a/Modbus.Net/Modbus.Net/Machine/BaseMachine.cs b/Modbus.Net/Modbus.Net/Machine/BaseMachine.cs index b4c270f..1cdeb62 100644 --- a/Modbus.Net/Modbus.Net/Machine/BaseMachine.cs +++ b/Modbus.Net/Modbus.Net/Machine/BaseMachine.cs @@ -37,7 +37,7 @@ namespace Modbus.Net /// /// 设备的Id类型 /// 设备中使用的AddressUnit的Id类型 - public abstract class BaseMachine : IMachine, IMachineReflectionCall + public abstract class BaseMachine : IMachine where TKey : IEquatable where TUnitKey : IEquatable { @@ -187,7 +187,7 @@ namespace Modbus.Net //获取数据 var datas = await - BaseUtility.GetUtilityMethods().GetDatasAsync( + BaseUtility.GetUtilityMethods().GetDatasAsync( AddressFormater.FormatAddress(communicateAddress.Area, communicateAddress.Address, communicateAddress.SubAddress), (int) @@ -433,7 +433,7 @@ namespace Modbus.Net communicateAddress.Address); var datasReturn = - await BaseUtility.GetUtilityMethods().GetDatasAsync( + await BaseUtility.GetUtilityMethods().GetDatasAsync( AddressFormater.FormatAddress(communicateAddress.Area, communicateAddress.Address, 0), (int) Math.Ceiling(communicateAddress.GetCount * @@ -545,7 +545,7 @@ namespace Modbus.Net } //写入数据 await - BaseUtility.GetUtilityMethods().SetDatasAsync(addressStart, + BaseUtility.GetUtilityMethods().SetDatasAsync(addressStart, valueHelper.ByteArrayToObjectArray(datas.Datas, new KeyValuePair(communicateAddress.DataType, communicateAddress.GetCount))); } @@ -636,29 +636,6 @@ namespace Modbus.Net return BaseUtility as TUtilityMethod; } - /// - public Task> InvokeGet(string functionName, object[] parameters) - { - var machineMethodType = GetType(); - var machineMethod = this as IMachineMethod; - var machineSetMethod = machineMethodType.GetMethod("Get" + functionName + "Async"); - var ans = machineSetMethod.Invoke(machineMethod, parameters); - return (Task>)ans; - } - - /// - public Task> InvokeSet(string functionName, object[] parameters, T datas) - { - var machineMethodType = GetType(); - var machineMethod = this as IMachineMethod; - var machineSetMethod = machineMethodType.GetMethod("Set" + functionName + "Async"); - object[] allParams = new object[parameters.Length + 1]; - Array.Copy(parameters, allParams, parameters.Length); - allParams[parameters.Length] = datas; - var ans = machineSetMethod.Invoke(machineMethod, allParams); - return (Task>)ans; - } - /// /// 连接设备 /// diff --git a/Modbus.Net/Modbus.Net/Machine/MachineMethodReflectionCall.cs b/Modbus.Net/Modbus.Net/Machine/MachineMethodReflectionCall.cs new file mode 100644 index 0000000..f7a77b3 --- /dev/null +++ b/Modbus.Net/Modbus.Net/Machine/MachineMethodReflectionCall.cs @@ -0,0 +1,47 @@ +using System; +using System.Threading.Tasks; + +namespace Modbus.Net +{ + /// + /// 设备的反射调用接口 + /// + public static class MachineMethodReflectionCall + { + /// + /// 反射方式调用获取方法 + /// + /// 要返回的数据类型 + /// 设备方法组 + /// 方法名 + /// 参数 + /// 返回的数据 + public static Task> InvokeGet(this IMachineMethod machineMethod, string functionName, object[] parameters) + { + var machineMethodType = machineMethod.GetType(); + var machineGetMethod = machineMethodType.GetMethod("Get" + functionName + "Async"); + var ans = machineGetMethod.Invoke(machineMethod, parameters); + return (Task>)ans; + } + + /// + /// 反射方式调用设置方法 + /// + /// 要设置的数据类型 + /// 设备方法组 + /// 方法名 + /// 参数 + /// 要设置的数据 + /// 设置是否成功 + public static Task> InvokeSet(this IMachineMethod machineMethod, string functionName, object[] parameters, T datas) + { + var machineMethodType = machineMethod.GetType(); + var machineSetMethod = machineMethodType.GetMethod("Set" + functionName + "Async"); + object[] allParams = new object[parameters.Length + 1]; + Array.Copy(parameters, allParams, parameters.Length); + allParams[parameters.Length] = datas; + var ans = machineSetMethod.Invoke(machineMethod, allParams); + return (Task>)ans; + } + } +} diff --git a/Modbus.Net/Modbus.Net/Utility/UtilityMethodReflectionCall.cs b/Modbus.Net/Modbus.Net/Utility/UtilityMethodReflectionCall.cs new file mode 100644 index 0000000..088ea58 --- /dev/null +++ b/Modbus.Net/Modbus.Net/Utility/UtilityMethodReflectionCall.cs @@ -0,0 +1,47 @@ +using System; +using System.Threading.Tasks; + +namespace Modbus.Net +{ + /// + /// 设备的反射调用接口 + /// + public static class UtilityMethodReflectionCall + { + /// + /// 反射方式调用获取方法 + /// + /// 要返回的数据类型 + /// 方法组 + /// 方法名 + /// 参数 + /// 返回的数据 + public static Task> InvokeGet(this IUtilityMethod utilityMethod, string functionName, object[] parameters) + { + var utilityMethodType = utilityMethod.GetType(); + var utilityGetMethod = utilityMethodType.GetMethod("Get" + functionName + "Async"); + var ans = utilityGetMethod.Invoke(utilityMethod, parameters); + return (Task>)ans; + } + + /// + /// 反射方式调用设置方法 + /// + /// 要设置的数据类型 + /// 方法组 + /// 方法名 + /// 参数 + /// 要设置的数据 + /// 设置是否成功 + public static Task> InvokeSet(this IUtilityMethod utilityMethod, string functionName, object[] parameters, T datas) + { + var utilityMethodType = utilityMethod.GetType(); + var utilitySetMethod = utilityMethodType.GetMethod("Set" + functionName + "Async"); + object[] allParams = new object[parameters.Length + 1]; + Array.Copy(parameters, allParams, parameters.Length); + allParams[parameters.Length] = datas; + var ans = utilitySetMethod.Invoke(utilityMethod, allParams); + return (Task>)ans; + } + } +} diff --git a/Tests/Modbus.Net.Tests/MachineMethodTest.cs b/Tests/Modbus.Net.Tests/MachineMethodTest.cs index c7f6210..99a4064 100644 --- a/Tests/Modbus.Net.Tests/MachineMethodTest.cs +++ b/Tests/Modbus.Net.Tests/MachineMethodTest.cs @@ -11,7 +11,7 @@ namespace Modbus.Net.Tests public void GetUtility() { BaseMachine baseMachine = new ModbusMachine(1, ModbusType.Tcp, "192.168.3.12", null, true, 2, 0); - var utility = baseMachine.GetUtilityMethods(); + var utility = baseMachine.GetUtilityMethods(); var methods = utility.GetType().GetRuntimeMethods(); Assert.AreEqual(methods.FirstOrDefault(method => method.Name == "GetDataAsync") != null, true); Assert.AreEqual(methods.FirstOrDefault(method => method.Name == "SetDataAsync") != null, true); @@ -22,9 +22,9 @@ namespace Modbus.Net.Tests public async Task InvokeUtility() { BaseMachine baseMachine = new ModbusMachine(1, ModbusType.Tcp, "192.168.3.12", null, true, 2, 0); - var success = await baseMachine.BaseUtility.GetUtilityMethods().SetDatasAsync("4X 1", new object[] { (byte)11 }); + var success = await baseMachine.BaseUtility.GetUtilityMethods().SetDatasAsync("4X 1", new object[] { (byte)11 }); Assert.AreEqual(success, true); - var datas = await baseMachine.BaseUtility.GetUtilityMethods().GetDatasAsync("4X 1", 1); + var datas = await baseMachine.BaseUtility.GetUtilityMethods().GetDatasAsync("4X 1", 1); Assert.AreEqual(datas.Datas[0], 11); baseMachine.Disconnect(); } diff --git a/Tests/Modbus.Net.Tests/ModbusTest.cs b/Tests/Modbus.Net.Tests/ModbusTest.cs index c148ee7..5400323 100644 --- a/Tests/Modbus.Net.Tests/ModbusTest.cs +++ b/Tests/Modbus.Net.Tests/ModbusTest.cs @@ -292,18 +292,18 @@ namespace Modbus.Net.Tests await _modbusTcpMachine!.BaseUtility.GetUtilityMethods().SetSingleCoilAsync("4X 1", dic1["4X 1"]); await _modbusAsciiMachine!.BaseUtility.GetUtilityMethods().SetSingleCoilAsync("4X 1", dic1["4X 1"]); await _modbusRtuMachine!.BaseUtility.GetUtilityMethods().SetSingleCoilAsync("4X 1", dic1["4X 1"]); - var ans = await _modbusTcpMachine.BaseUtility.GetUtilityMethods().GetDatasAsync("4X 1", 1); - var ans2 = await _modbusRtuMachine.BaseUtility.GetUtilityMethods().GetDatasAsync("4X 1", 1); - var ans3 = await _modbusAsciiMachine.BaseUtility.GetUtilityMethods().GetDatasAsync("4X 1", 1); + var ans = await _modbusTcpMachine.BaseUtility.GetUtilityMethods().GetDatasAsync("4X 1", 1); + var ans2 = await _modbusRtuMachine.BaseUtility.GetUtilityMethods().GetDatasAsync("4X 1", 1); + var ans3 = await _modbusAsciiMachine.BaseUtility.GetUtilityMethods().GetDatasAsync("4X 1", 1); Assert.AreEqual(ans.Datas[0], dic1["4X 1"]); Assert.AreEqual(ans2.Datas[0], dic1["4X 1"]); Assert.AreEqual(ans3.Datas[0], dic1["4X 1"]); await _modbusTcpMachine.BaseUtility.GetUtilityMethods().SetSingleCoilAsync("0X 1", dic2["0X 1"] >= 1); await _modbusAsciiMachine.BaseUtility.GetUtilityMethods().SetSingleCoilAsync("0X 1", dic2["0X 1"] >= 1); await _modbusRtuMachine.BaseUtility.GetUtilityMethods().SetSingleCoilAsync("0X 1", dic2["0X 1"] >= 1); - var ans21 = await _modbusTcpMachine.BaseUtility.GetUtilityMethods().GetDatasAsync("0X 1", 1); - var ans22 = await _modbusRtuMachine.BaseUtility.GetUtilityMethods().GetDatasAsync("0X 1", 1); - var ans23 = await _modbusAsciiMachine.BaseUtility.GetUtilityMethods().GetDatasAsync("0X 1", 1); + var ans21 = await _modbusTcpMachine.BaseUtility.GetUtilityMethods().GetDatasAsync("0X 1", 1); + var ans22 = await _modbusRtuMachine.BaseUtility.GetUtilityMethods().GetDatasAsync("0X 1", 1); + var ans23 = await _modbusAsciiMachine.BaseUtility.GetUtilityMethods().GetDatasAsync("0X 1", 1); Assert.AreEqual(ans21.Datas[0] ? 1 : 0, dic2["0X 1"]); Assert.AreEqual(ans22.Datas[0] ? 1 : 0, dic2["0X 1"]); Assert.AreEqual(ans23.Datas[0] ? 1 : 0, dic2["0X 1"]);