diff --git a/Modbus.Net/Modbus.Net.Modbus.SelfDefinedSample/ModbusProtocolTime.cs b/Modbus.Net/Modbus.Net.Modbus.SelfDefinedSample/ModbusProtocolTime.cs index 1f63549..5d67e60 100644 --- a/Modbus.Net/Modbus.Net.Modbus.SelfDefinedSample/ModbusProtocolTime.cs +++ b/Modbus.Net/Modbus.Net.Modbus.SelfDefinedSample/ModbusProtocolTime.cs @@ -1,4 +1,5 @@ using System; +using ProtocolUnit = Modbus.Net.ProtocolUnit; namespace Modbus.Net.Modbus.SelfDefinedSample { diff --git a/Modbus.Net/Modbus.Net.Modbus/ModbusMachine.cs b/Modbus.Net/Modbus.Net.Modbus/ModbusMachine.cs index c79551b..6d6b737 100644 --- a/Modbus.Net/Modbus.Net.Modbus/ModbusMachine.cs +++ b/Modbus.Net/Modbus.Net.Modbus/ModbusMachine.cs @@ -48,49 +48,5 @@ namespace Modbus.Net.Modbus { } } - - /// - /// Modbus设备 - /// - public class ModbusMachine : BaseMachine - { - /// - /// 构造函数 - /// - /// 设备的ID号 - /// 连接类型 - /// 连接地址 - /// 读写的地址 - /// 是否保持连接 - /// 从站号 - /// 主站号 - /// 端格式 - public ModbusMachine(string id, ModbusType connectionType, string connectionString, - IEnumerable getAddresses, bool keepConnect, byte slaveAddress, byte masterAddress, - Endian endian = Endian.BigEndianLsb) - : base(id, getAddresses, keepConnect, slaveAddress, masterAddress) - { - BaseUtility = new ModbusUtility(connectionType, connectionString, slaveAddress, masterAddress, endian); - AddressFormater = new AddressFormaterModbus(); - AddressCombiner = new AddressCombinerContinus(AddressTranslator, 100); - AddressCombinerSet = new AddressCombinerContinus(AddressTranslator, 100); - } - - /// - /// 构造函数 - /// - /// 设备的ID号 - /// 连接类型 - /// 连接地址 - /// 读写的地址 - /// 从站号 - /// 主站号 - /// 端格式 - public ModbusMachine(string id, ModbusType connectionType, string connectionString, - IEnumerable getAddresses, byte slaveAddress, byte masterAddress, - Endian endian = Endian.BigEndianLsb) - : this(id, connectionType, connectionString, getAddresses, true, slaveAddress, masterAddress, endian) - { - } - } + } \ No newline at end of file diff --git a/Modbus.Net/Modbus.Net.Modbus/ModbusProtocol.cs b/Modbus.Net/Modbus.Net.Modbus/ModbusProtocol.cs index 1adefe1..910af71 100644 --- a/Modbus.Net/Modbus.Net.Modbus/ModbusProtocol.cs +++ b/Modbus.Net/Modbus.Net.Modbus/ModbusProtocol.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Threading.Tasks; +using ProtocolUnit = Modbus.Net.ProtocolUnit; namespace Modbus.Net.Modbus { diff --git a/Modbus.Net/Modbus.Net.Modbus/ModbusProtocolLinkerBytesExtend.cs b/Modbus.Net/Modbus.Net.Modbus/ModbusProtocolLinkerBytesExtend.cs index c7fff30..0680b9f 100644 --- a/Modbus.Net/Modbus.Net.Modbus/ModbusProtocolLinkerBytesExtend.cs +++ b/Modbus.Net/Modbus.Net.Modbus/ModbusProtocolLinkerBytesExtend.cs @@ -48,7 +48,7 @@ namespace Modbus.Net.Modbus /// /// Tcp协议字节伸缩 /// - public class ModbusTcpProtocolLinkerBytesExtend : IProtocolLinkerBytesExtend + public class ModbusTcpProtocolLinkerBytesExtend : IProtocolLinkerBytesExtend { private static ushort _sendCount = 0; private static readonly object _counterLock = new object(); @@ -93,7 +93,7 @@ namespace Modbus.Net.Modbus /// /// Rtu协议字节伸缩 /// - public class ModbusRtuProtocolLinkerBytesExtend : IProtocolLinkerBytesExtend + public class ModbusRtuProtocolLinkerBytesExtend : IProtocolLinkerBytesExtend { /// /// 协议扩展,协议内容发送前调用 @@ -128,7 +128,7 @@ namespace Modbus.Net.Modbus /// /// Ascii协议字节伸缩 /// - public class ModbusAsciiProtocolLinkerBytesExtend : IProtocolLinkerBytesExtend + public class ModbusAsciiProtocolLinkerBytesExtend : IProtocolLinkerBytesExtend { /// /// 协议扩展,协议内容发送前调用 diff --git a/Modbus.Net/Modbus.Net.Modbus/ModbusUtility.cs b/Modbus.Net/Modbus.Net.Modbus/ModbusUtility.cs index cc06be2..041953c 100644 --- a/Modbus.Net/Modbus.Net.Modbus/ModbusUtility.cs +++ b/Modbus.Net/Modbus.Net.Modbus/ModbusUtility.cs @@ -67,7 +67,7 @@ namespace Modbus.Net.Modbus /// /// Modbus基础Api入口 /// - public class ModbusUtility : BaseUtility, IUtilityMethodWriteSingleCoil + public class ModbusUtility : BaseUtility, PipeUnit>, IUtilityMethodWriteSingleCoil { private static readonly ILogger logger = LogProvider.CreateLogger(); diff --git a/Modbus.Net/Modbus.Net.OPC/OpcMachine.cs b/Modbus.Net/Modbus.Net.OPC/OpcMachine.cs index 01aa686..dd0d00d 100644 --- a/Modbus.Net/Modbus.Net.OPC/OpcMachine.cs +++ b/Modbus.Net/Modbus.Net.OPC/OpcMachine.cs @@ -26,7 +26,7 @@ namespace Modbus.Net.OPC /// /// Opc Da设备 /// - public abstract class OpcMachine : BaseMachine + public abstract class OpcMachine : BaseMachine { /// /// 构造函数 @@ -37,8 +37,8 @@ namespace Modbus.Net.OPC protected OpcMachine(string id, IEnumerable getAddresses, bool keepConnect) : base(id, getAddresses, keepConnect) { - AddressCombiner = new AddressCombinerSingle(); - AddressCombinerSet = new AddressCombinerSingle(); + AddressCombiner = new AddressCombinerSingle(); + AddressCombinerSet = new AddressCombinerSingle(); } } } \ No newline at end of file diff --git a/Modbus.Net/Modbus.Net.Siemens/SiemensMachine.cs b/Modbus.Net/Modbus.Net.Siemens/SiemensMachine.cs index d2bed48..49c87fb 100644 --- a/Modbus.Net/Modbus.Net.Siemens/SiemensMachine.cs +++ b/Modbus.Net/Modbus.Net.Siemens/SiemensMachine.cs @@ -50,51 +50,4 @@ namespace Modbus.Net.Siemens { } } - - /// - /// 西门子设备 - /// - public class SiemensMachine : BaseMachine - { - /// - /// 构造函数 - /// - /// 设备id号 - /// 连接类型 - /// 连接地址 - /// 设备类型 - /// 读写的地址 - /// 是否保持连接 - /// 从站号 - /// 主站号 - /// 本机模块位,0到7,仅200使用,其它型号不要填写 - /// PLC模块位,0到7,仅200使用,其它型号不要填写 - public SiemensMachine(string id, SiemensType connectionType, string connectionString, SiemensMachineModel model, - IEnumerable getAddresses, bool keepConnect, byte slaveAddress, byte masterAddress, byte src = 1, byte dst = 0) - : base(id, getAddresses, keepConnect, slaveAddress, masterAddress) - { - BaseUtility = new SiemensUtility(connectionType, connectionString, model, slaveAddress, masterAddress, src, dst); - AddressFormater = new AddressFormaterSiemens(); - AddressCombiner = new AddressCombinerContinus(AddressTranslator, 100); - AddressCombinerSet = new AddressCombinerContinus(AddressTranslator, 100); - } - - /// - /// 构造函数 - /// - /// 设备id号 - /// 连接类型 - /// 连接地址 - /// 设备类型 - /// 读写的地址 - /// 从站号 - /// 主站号 - /// 本机模块位,0到7,仅200使用,其它型号不要填写 - /// PLC模块位,0到7,仅200使用,其它型号不要填写 - public SiemensMachine(string id, SiemensType connectionType, string connectionString, SiemensMachineModel model, - IEnumerable getAddresses, byte slaveAddress, byte masterAddress, byte src = 1, byte dst = 0) - : this(id, connectionType, connectionString, model, getAddresses, true, slaveAddress, masterAddress, src, dst) - { - } - } } \ No newline at end of file diff --git a/Modbus.Net/Modbus.Net.Siemens/SiemensPpiProtocol.cs b/Modbus.Net/Modbus.Net.Siemens/SiemensPpiProtocol.cs index 0e1908a..0c3e62b 100644 --- a/Modbus.Net/Modbus.Net.Siemens/SiemensPpiProtocol.cs +++ b/Modbus.Net/Modbus.Net.Siemens/SiemensPpiProtocol.cs @@ -62,7 +62,7 @@ namespace Modbus.Net.Siemens /// 协议核心 /// 协议的参数 /// 设备返回的信息 - private async Task ForceSendReceiveAsync(ProtocolUnit unit, IInputStruct content) + private async Task ForceSendReceiveAsync(ProtocolUnit unit, IInputStruct content) { return await base.SendReceiveAsync(unit, content); } diff --git a/Modbus.Net/Modbus.Net.Siemens/SiemensProtocol.cs b/Modbus.Net/Modbus.Net.Siemens/SiemensProtocol.cs index ce798e3..9d1ab96 100644 --- a/Modbus.Net/Modbus.Net.Siemens/SiemensProtocol.cs +++ b/Modbus.Net/Modbus.Net.Siemens/SiemensProtocol.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Linq; +using ProtocolUnit = Modbus.Net.ProtocolUnit; namespace Modbus.Net.Siemens { diff --git a/Modbus.Net/Modbus.Net.Siemens/SiemensProtocolLinkerBytesExtend.cs b/Modbus.Net/Modbus.Net.Siemens/SiemensProtocolLinkerBytesExtend.cs index 6adf06b..a2b37f2 100644 --- a/Modbus.Net/Modbus.Net.Siemens/SiemensProtocolLinkerBytesExtend.cs +++ b/Modbus.Net/Modbus.Net.Siemens/SiemensProtocolLinkerBytesExtend.cs @@ -5,7 +5,7 @@ namespace Modbus.Net.Siemens /// /// 西门子Tcp协议扩展 /// - public class SiemensTcpProtocolLinkerBytesExtend : IProtocolLinkerBytesExtend + public class SiemensTcpProtocolLinkerBytesExtend : IProtocolLinkerBytesExtend { /// /// 协议扩展,协议内容发送前调用 @@ -35,7 +35,7 @@ namespace Modbus.Net.Siemens /// /// 西门子Ppi协议扩展 /// - public class SiemensPpiProtocolLinkerBytesExtend : IProtocolLinkerBytesExtend + public class SiemensPpiProtocolLinkerBytesExtend : IProtocolLinkerBytesExtend { /// /// 协议扩展,协议内容发送前调用 diff --git a/Modbus.Net/Modbus.Net.Siemens/SiemensTcpProtocol.cs b/Modbus.Net/Modbus.Net.Siemens/SiemensTcpProtocol.cs index 7aeb932..23a6ad2 100644 --- a/Modbus.Net/Modbus.Net.Siemens/SiemensTcpProtocol.cs +++ b/Modbus.Net/Modbus.Net.Siemens/SiemensTcpProtocol.cs @@ -106,7 +106,7 @@ namespace Modbus.Net.Siemens /// 协议的核心 /// 协议的参数 /// 返回的数据 - public override PipeUnit SendReceive(ProtocolUnit unit, IInputStruct content) + public override PipeUnit SendReceive(ProtocolUnit unit, IInputStruct content) { return AsyncHelper.RunSync(() => SendReceiveAsync(unit, content)); } @@ -117,7 +117,7 @@ namespace Modbus.Net.Siemens /// 发送的数据 /// 协议的参数 /// 返回的数据 - public override async Task SendReceiveAsync(ProtocolUnit unit, IInputStruct content) + public override async Task SendReceiveAsync(ProtocolUnit unit, IInputStruct content) { if (ProtocolLinker != null && ProtocolLinker.IsConnected) return await base.SendReceiveAsync(unit, content); if (_connectTryCount > 10) return null; @@ -133,7 +133,7 @@ namespace Modbus.Net.Siemens /// 发送的数据 /// 协议的参数 /// 返回的数据 - private async Task ForceSendReceiveAsync(ProtocolUnit unit, IInputStruct content) + private async Task ForceSendReceiveAsync(ProtocolUnit unit, IInputStruct content) { return await base.SendReceiveAsync(unit, content); } diff --git a/Modbus.Net/Modbus.Net.Siemens/SiemensUtility.cs b/Modbus.Net/Modbus.Net.Siemens/SiemensUtility.cs index d5e4b90..bb7117f 100644 --- a/Modbus.Net/Modbus.Net.Siemens/SiemensUtility.cs +++ b/Modbus.Net/Modbus.Net.Siemens/SiemensUtility.cs @@ -59,7 +59,7 @@ namespace Modbus.Net.Siemens /// /// 西门子通讯Api入口 /// - public class SiemensUtility : BaseUtility + public class SiemensUtility : BaseUtility, PipeUnit> { private static readonly ILogger logger = LogProvider.CreateLogger(); diff --git a/Modbus.Net/Modbus.Net/Interface/IMachine.cs b/Modbus.Net/Modbus.Net/Interface/IMachine.cs index f4da644..9ae0b2b 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, IMachineMethodData, IMachineReflectionCall where TKey : IEquatable + public interface IMachine : IMachineProperty, IMachineMethodDatas, IMachineReflectionCall where TKey : IEquatable { } } diff --git a/Modbus.Net/Modbus.Net/Interface/IMachineMethod.cs b/Modbus.Net/Modbus.Net/Interface/IMachineMethod.cs index 8ba85a2..676fdcd 100644 --- a/Modbus.Net/Modbus.Net/Interface/IMachineMethod.cs +++ b/Modbus.Net/Modbus.Net/Interface/IMachineMethod.cs @@ -13,19 +13,19 @@ namespace Modbus.Net /// /// Machineݶдӿ /// - public interface IMachineMethodData : IMachineMethod + public interface IMachineMethodDatas : IMachineMethod { /// /// ȡ /// /// 豸ȡ - ReturnStruct> GetDatas(MachineDataType getDataType); + ReturnStruct>> GetDatas(MachineDataType getDataType); /// /// ȡ /// /// 豸ȡ - Task>> GetDatasAsync(MachineDataType getDataType); + Task>>> GetDatasAsync(MachineDataType getDataType); /// /// д diff --git a/Modbus.Net/Modbus.Net/Interface/IMachineProperty.cs b/Modbus.Net/Modbus.Net/Interface/IMachineProperty.cs index 435b452..cb63737 100644 --- a/Modbus.Net/Modbus.Net/Interface/IMachineProperty.cs +++ b/Modbus.Net/Modbus.Net/Interface/IMachineProperty.cs @@ -56,12 +56,6 @@ namespace Modbus.Net /// /// 是否断开成功 bool Disconnect(); - - /// - /// 获取设备的Id的字符串 - /// - /// - string GetMachineIdString(); } /// diff --git a/Modbus.Net/Modbus.Net/Interface/IProtocolLinkerBytesExtend.cs b/Modbus.Net/Modbus.Net/Interface/IProtocolLinkerBytesExtend.cs index ed89694..d80cd9a 100644 --- a/Modbus.Net/Modbus.Net/Interface/IProtocolLinkerBytesExtend.cs +++ b/Modbus.Net/Modbus.Net/Interface/IProtocolLinkerBytesExtend.cs @@ -1,12 +1,5 @@ namespace Modbus.Net { - /// - /// 协议字节伸缩 - /// - public interface IProtocolLinkerBytesExtend : IProtocolLinkerBytesExtend - { - } - /// /// 协议字节伸缩 /// diff --git a/Modbus.Net/Modbus.Net/Job/MachineJobScheduler.cs b/Modbus.Net/Modbus.Net/Job/MachineJobScheduler.cs index 61de44d..28a9c2f 100644 --- a/Modbus.Net/Modbus.Net/Job/MachineJobScheduler.cs +++ b/Modbus.Net/Modbus.Net/Job/MachineJobScheduler.cs @@ -3,6 +3,7 @@ using Quartz.Impl; using Quartz.Impl.Matchers; using System; using System.Collections.Generic; +using System.Reflection; using System.Threading.Tasks; namespace Modbus.Net @@ -10,14 +11,7 @@ namespace Modbus.Net /// /// 返回结果的定义类 /// - public class DataReturnDef : DataReturnDef - { - } - - /// - /// 返回结果的定义类 - /// - public class DataReturnDef where TMachineKey : IEquatable + public class DataReturnDef where TMachineKey : IEquatable where TReturnUnit : struct { /// /// 设备的Id @@ -27,13 +21,13 @@ namespace Modbus.Net /// /// 返回的数据值 /// - public ReturnStruct> ReturnValues { get; set; } + public ReturnStruct>> ReturnValues { get; set; } } /// /// 设备调度器创建类 /// - public sealed class MachineJobSchedulerCreator + public sealed class MachineJobSchedulerCreator where TMachineKey : IEquatable where TReturnUnit : struct where TMachineMethod : IMachineMethod { /// /// 创建设备调度器 @@ -42,7 +36,7 @@ namespace Modbus.Net /// 重复次数,负数为无限循环,0为执行一次 /// 间隔秒数 /// - public static async Task CreateScheduler(string triggerKey, int count = 0, int intervalSecond = 1) + public static async Task> CreateScheduler(string triggerKey, int count = 0, int intervalSecond = 1) { return await CreateScheduler(triggerKey, count, (double)intervalSecond); } @@ -54,12 +48,12 @@ namespace Modbus.Net /// 重复次数,负数为无限循环,0为执行一次 /// 间隔毫秒数 /// - public static async Task CreateSchedulerMillisecond(string triggerKey, int count = 0, int intervalMilliSecond = 1000) + public static async Task> CreateSchedulerMillisecond(string triggerKey, int count = 0, int intervalMilliSecond = 1000) { - return await CreateScheduler(triggerKey, count, (double)intervalMilliSecond / 1000.0); + return await CreateScheduler(triggerKey, count, intervalMilliSecond / 1000.0); } - private static async Task CreateScheduler(string triggerKey, int count = 0, double interval = 1) + private static async Task> CreateScheduler(string triggerKey, int count = 0, double interval = 1) { IScheduler scheduler = await StdSchedulerFactory.GetDefaultScheduler(); @@ -87,7 +81,7 @@ namespace Modbus.Net var jobKeys = await scheduler.GetJobKeys(GroupMatcher.GroupEquals("Modbus.Net.DataQuery.Group." + triggerKey)); await scheduler.DeleteJobs(jobKeys); - return new MachineGetJobScheduler(scheduler, trigger); + return new MachineGetJobScheduler(scheduler, trigger); } /// @@ -107,7 +101,7 @@ namespace Modbus.Net /// /// 获取数据任务 /// - public sealed class MachineGetJobScheduler + public sealed class MachineGetJobScheduler where TMachineKey: IEquatable where TReturnUnit : struct where TMachineMethod : IMachineMethod { private IScheduler _scheduler; @@ -147,18 +141,23 @@ namespace Modbus.Net /// 获取数据的方式 /// /// - public async Task From(string queryId, IMachineReflectionCall machine, MachineDataType machineDataType) + public async Task> From(string queryId, IMachineReflectionCall machine, MachineDataType machineDataType) { JobKey jobKey = JobKey.Create("Modbus.Net.DataQuery.Job." + queryId, "Modbus.Net.DataQuery.Group." + _trigger.Key.Name); - IJobDetail job = JobBuilder.Create() + IJobDetail job = JobBuilder.Create>() .WithIdentity(jobKey) .StoreDurably(true) .Build(); + string methodName = typeof(TMachineMethod).Name; + if (methodName.Substring(0,14) != "IMachineMethod") + { + throw new FormatException("IMachineMethod Name not match format exception"); + } job.JobDataMap.Put("DataType", machineDataType); job.JobDataMap.Put("Machine", machine); - job.JobDataMap.Put("Function", "Datas"); + job.JobDataMap.Put("Function", methodName.Remove(0,14)); if (_parentJobKey != null) { @@ -173,7 +172,7 @@ namespace Modbus.Net await _scheduler.ScheduleJob(job, _trigger); } - return new MachineQueryJobScheduler(_scheduler, _trigger, jobKey); + return new MachineQueryJobScheduler(_scheduler, _trigger, jobKey); } /// @@ -183,25 +182,12 @@ namespace Modbus.Net /// 要写入的数据模板 /// 获取数据的方式 /// - public Task Apply(string queryId, Dictionary values, MachineDataType machineDataType) - { - return Apply(queryId, values, machineDataType); - } - - /// - /// 直接向任务队列中写一个数据模板 - /// - /// 设备的ID类型 - /// 任务ID,每个触发器唯一 - /// 要写入的数据模板 - /// 获取数据的方式 - /// /// - public async Task Apply(string queryId, Dictionary values, MachineDataType machineDataType) where TMachineKey : IEquatable + public async Task> Apply(string queryId, Dictionary values, MachineDataType machineDataType) { JobKey jobKey = JobKey.Create("Modbus.Net.DataQuery.Job." + queryId, "Modbus.Net.DataQuery.Group." + _trigger.Key.Name); - IJobDetail job = JobBuilder.Create>() + IJobDetail job = JobBuilder.Create>() .WithIdentity(jobKey) .StoreDurably(true) .Build(); @@ -223,7 +209,7 @@ namespace Modbus.Net await _scheduler.ScheduleJob(job, _trigger); } - return new MachineQueryJobScheduler(_scheduler, _trigger, jobKey); + return new MachineQueryJobScheduler(_scheduler, _trigger, jobKey); } /// @@ -233,23 +219,10 @@ namespace Modbus.Net /// 要写入的数据模板 /// 获取数据的方式 /// - public Task ApplyTo(string queryId, Dictionary values, MachineDataType machineDataType) - { - return ApplyTo(queryId, values, machineDataType); - } - - /// - /// 直接向任务队列中写一个数据模板,并跳过处理数据流程 - /// - /// 设备的ID类型 - /// 任务ID,每个触发器唯一 - /// 要写入的数据模板 - /// 获取数据的方式 - /// /// - public async Task ApplyTo(string queryId, Dictionary values, MachineDataType machineDataType) where TMachineKey : IEquatable + public async Task> ApplyTo(string queryId, Dictionary values, MachineDataType machineDataType) { - var applyJobScheduler = await Apply(queryId, values, machineDataType); + var applyJobScheduler = await Apply(queryId, values, machineDataType); return await applyJobScheduler.Query(); } } @@ -257,7 +230,7 @@ namespace Modbus.Net /// /// 处理数据任务 /// - public sealed class MachineQueryJobScheduler + public sealed class MachineQueryJobScheduler where TMachineKey: IEquatable where TReturnUnit : struct where TMachineMethod : IMachineMethod { private IScheduler _scheduler; @@ -284,26 +257,14 @@ namespace Modbus.Net /// 任务ID,每个触发器唯一 /// 处理数据的函数,输入返回读数据的定义和值,输出写数据字典 /// - public Task Query(string queryId = null, Func> QueryDataFunc = null) - { - return Query(queryId, QueryDataFunc); - } - - /// - /// 处理数据 - /// - /// 设备的ID类型 - /// 任务ID,每个触发器唯一 - /// 处理数据的函数,输入返回读数据的定义和值,输出写数据字典 - /// /// - public async Task Query(string queryId = null, Func> QueryDataFunc = null) where TMachineKey : IEquatable + public async Task> Query(string queryId = null, Func, Dictionary> QueryDataFunc = null) { - if (queryId == null) return new MachineSetJobScheduler(_scheduler, _trigger, _parentJobKey); + if (queryId == null) return new MachineSetJobScheduler(_scheduler, _trigger, _parentJobKey); JobKey jobKey = JobKey.Create("Modbus.Net.DataQuery.Job." + queryId, "Modbus.Net.DataQuery.Group." + _trigger.Key.Name); - IJobDetail job = JobBuilder.Create>() + IJobDetail job = JobBuilder.Create>() .WithIdentity(jobKey) .StoreDurably(true) .Build(); @@ -317,14 +278,14 @@ namespace Modbus.Net await _scheduler.AddJob(job, true); - return new MachineSetJobScheduler(_scheduler, _trigger, jobKey); + return new MachineSetJobScheduler(_scheduler, _trigger, jobKey); } } /// /// 写入数据任务 /// - public sealed class MachineSetJobScheduler + public sealed class MachineSetJobScheduler where TMachineKey: IEquatable where TReturnUnit : struct where TMachineMethod : IMachineMethod { private IScheduler _scheduler; @@ -354,17 +315,22 @@ namespace Modbus.Net /// 写入数据的设备实例 /// /// - public async Task To(string queryId, IMachineReflectionCall machine) + public async Task> To(string queryId, IMachineReflectionCall machine) { JobKey jobKey = JobKey.Create("Modbus.Net.DataQuery.Job." + queryId, "Modbus.Net.DataQuery.Group." + _trigger.Key.Name); - IJobDetail job = JobBuilder.Create() + IJobDetail job = JobBuilder.Create>() .WithIdentity(jobKey) .StoreDurably(true) - .Build(); + .Build(); + string methodName = typeof(TMachineMethod).Name; + if (methodName.Substring(0, 14) != "IMachineMethod") + { + throw new FormatException("IMachineMethod Name not match format exception"); + } job.JobDataMap.Put("Machine", machine); - job.JobDataMap.Put("Function", "Datas"); + job.JobDataMap.Put("Function", methodName.Remove(0,14)); var listener = _scheduler.ListenerManager.GetJobListener("Modbus.Net.DataQuery.Chain." + _trigger.Key.Name) as JobChainingJobListenerWithDataMap; if (listener == null) throw new NullReferenceException("Listener " + "Modbus.Net.DataQuery.Chain." + _trigger.Key.Name + " is null"); @@ -372,7 +338,7 @@ namespace Modbus.Net await _scheduler.AddJob(job, true); - return new MachineDealJobScheduler(_scheduler, _trigger, jobKey); + return new MachineDealJobScheduler(_scheduler, _trigger, jobKey); } /// @@ -382,9 +348,9 @@ namespace Modbus.Net /// 要获取数据的设备实例 /// 获取数据的方式 /// - public async Task From(string queryId, IMachineReflectionCall machine, MachineDataType machineDataType) + public async Task> From(string queryId, IMachineReflectionCall machine, MachineDataType machineDataType) { - return await new MachineGetJobScheduler(_scheduler, _trigger, _parentJobKey).From(queryId, machine, machineDataType); + return await new MachineGetJobScheduler(_scheduler, _trigger, _parentJobKey).From(queryId, machine, machineDataType); } /// @@ -400,7 +366,7 @@ namespace Modbus.Net /// /// 处理写返回任务 /// - public sealed class MachineDealJobScheduler + public sealed class MachineDealJobScheduler where TMachineKey : IEquatable where TReturnUnit : struct where TMachineMethod : IMachineMethod { private IScheduler _scheduler; @@ -423,7 +389,6 @@ namespace Modbus.Net _parentJobKey = parentJobKey; } - /// /// 处理写返回 /// @@ -432,22 +397,9 @@ namespace Modbus.Net /// 失败回调方法,参数为设备ID /// /// - public async Task Deal(string queryId = null, Func onSuccess = null, Func onFailure = null) - { - return await Deal(queryId, onSuccess, onFailure); - } - - /// - /// 处理写返回 - /// - /// 任务ID,每个触发器唯一 - /// 成功回调方法,参数为设备ID - /// 失败回调方法,参数为设备ID - /// - /// - public async Task Deal(string queryId = null, Func onSuccess = null, Func onFailure = null) where TMachineKey : IEquatable - { - if (queryId == null) return new MachineSetJobScheduler(_scheduler, _trigger, _parentJobKey); + public async Task> Deal(string queryId = null, Func onSuccess = null, Func onFailure = null) + { + if (queryId == null) return new MachineSetJobScheduler(_scheduler, _trigger, _parentJobKey); JobKey jobKey = JobKey.Create("Modbus.Net.DataQuery.Job." + queryId, "Modbus.Net.DataQuery.Group." + _trigger.Key.Name); IJobDetail job = JobBuilder.Create>() @@ -464,14 +416,14 @@ namespace Modbus.Net await _scheduler.AddJob(job, true); - return new MachineSetJobScheduler(_scheduler, _trigger, jobKey); + return new MachineSetJobScheduler(_scheduler, _trigger, jobKey); } } /// /// 获取数据任务 /// - public class MachineGetDataJob : IJob + public class MachineGetDataJob : IJob where TReturnUnit : struct { /// public async Task Execute(IJobExecutionContext context) @@ -482,7 +434,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 IMachineReflectionCall)!.InvokeGet>>((string)callFunction, new object[] { (MachineDataType)machineDataType }); context.JobDetail.JobDataMap.Put("Value", values); await context.Scheduler.AddJob(context.JobDetail, true, false); @@ -492,8 +444,7 @@ namespace Modbus.Net /// /// 处理数据任务 /// - /// - public class MachineQueryDataJob : IJob where TMachineKey : IEquatable + public class MachineQueryDataJob : IJob where TMachineKey : IEquatable where TReturnUnit : struct { /// public async Task Execute(IJobExecutionContext context) @@ -504,11 +455,11 @@ namespace Modbus.Net context.JobDetail.JobDataMap.TryGetValue("Machine", out machine); context.JobDetail.JobDataMap.TryGetValue("Value", out values); context.JobDetail.JobDataMap.TryGetValue("QueryMethod", out QueryMethod); - Func> QueryMethodDispatch = (Func>)QueryMethod; + Func, Dictionary> QueryMethodDispatch = (Func, Dictionary>)QueryMethod; if (QueryMethod != null && values != null) { - context.JobDetail.JobDataMap.Put("SetValue", QueryMethodDispatch(new DataReturnDef() { MachineId = machine == null ? null : ((IMachineProperty)machine).GetMachineIdString(), ReturnValues = (ReturnStruct>)values })); + context.JobDetail.JobDataMap.Put("SetValue", QueryMethodDispatch(new DataReturnDef() { MachineId = machine == null ? default : ((IMachineProperty)machine).Id, ReturnValues = (ReturnStruct>>)values })); await context.Scheduler.AddJob(context.JobDetail, true, false); } } @@ -517,7 +468,7 @@ namespace Modbus.Net /// /// 写数据任务 /// - public class MachineSetDataJob : IJob + public class MachineSetDataJob : IJob where TReturnUnit : struct { /// public async Task Execute(IJobExecutionContext context) @@ -533,7 +484,9 @@ namespace Modbus.Net context.JobDetail.JobDataMap.TryGetValue("SetValue", out valuesSet); context.JobDetail.JobDataMap.TryGetValue("Function", out callFunction); if (valuesSet == null && values != null) - valuesSet = ((ReturnStruct>)values).Datas.MapGetValuesToSetValues(); + { + valuesSet = ((ReturnStruct>>)values).Datas.MapGetValuesToSetValues(); + } if (valuesSet == null) { @@ -565,11 +518,11 @@ namespace Modbus.Net ReturnStruct successValue = (ReturnStruct)success; if (successValue.IsSuccess == true && onSuccess != null) { - await ((Func)onSuccess)(((IMachineProperty)machine).GetMachineIdString()); + await ((Func)onSuccess)(((IMachineProperty)machine).Id); } if (successValue.IsSuccess == false && onFailure != null) { - await ((Func)onFailure)(((IMachineProperty)machine).GetMachineIdString(), successValue.ErrorCode, successValue.ErrorMsg); + await ((Func)onFailure)(((IMachineProperty)machine).Id, successValue.ErrorCode, successValue.ErrorMsg); } context.JobDetail.JobDataMap.Remove("Success"); diff --git a/Modbus.Net/Modbus.Net/Job/MultipleMachinesJobSchedulerCreator.cs b/Modbus.Net/Modbus.Net/Job/MultipleMachinesJobSchedulerCreator.cs index 05a5a85..2826fb1 100644 --- a/Modbus.Net/Modbus.Net/Job/MultipleMachinesJobSchedulerCreator.cs +++ b/Modbus.Net/Modbus.Net/Job/MultipleMachinesJobSchedulerCreator.cs @@ -9,7 +9,7 @@ namespace Modbus.Net /// /// 豸 /// - public sealed class MultipleMachinesJobScheduler + public sealed class MultipleMachinesJobScheduler where TMachineKey: IEquatable where TReturnUnit : struct where TMachineMethod : IMachineMethod { private static int _machineCount = 0; @@ -21,7 +21,7 @@ namespace Modbus.Net /// ظΪѭ0Ϊִһ /// /// - public static ParallelLoopResult RunScheduler(IEnumerable> machines, Func, MachineGetJobScheduler, Task> machineJobTemplate, int count = 0, int intervalSecond = 1) where TKey : IEquatable + public static ParallelLoopResult RunScheduler(IEnumerable> machines, Func, MachineGetJobScheduler, Task> machineJobTemplate, int count = 0, int intervalSecond = 1) { _machineCount = machines.Count(); return Parallel.ForEach(machines, (machine, state, index) => @@ -29,25 +29,12 @@ namespace Modbus.Net Task.Factory.StartNew(async () => { Thread.Sleep((int)(intervalSecond * 1000.0 / _machineCount * index)); - var getJobScheduler = await MachineJobSchedulerCreator.CreateScheduler("Trigger" + index, count, intervalSecond); + var getJobScheduler = await MachineJobSchedulerCreator.CreateScheduler("Trigger" + index, count, intervalSecond); await machineJobTemplate(machine, getJobScheduler); }); }); } - /// - /// 豸 - /// - /// 豸ļ - /// 豸ģ - /// ظΪѭ0Ϊִһ - /// - /// - public static ParallelLoopResult RunScheduler(IEnumerable> machines, Func, MachineGetJobScheduler, Task> machineJobTemplate, int count = 0, int intervalSecond = 1) - { - return RunScheduler(machines, machineJobTemplate, count, intervalSecond); - } - /// /// ȡ /// @@ -56,7 +43,7 @@ namespace Modbus.Net { return Parallel.For(0, _machineCount, async index => { - await MachineJobSchedulerCreator.CancelJob("Trigger" + index); + await MachineJobSchedulerCreator.CancelJob("Trigger" + index); }); } } diff --git a/Modbus.Net/Modbus.Net/Linker/ProtocolLinker.cs b/Modbus.Net/Modbus.Net/Linker/ProtocolLinker.cs index aa4f15e..b9c72a5 100644 --- a/Modbus.Net/Modbus.Net/Linker/ProtocolLinker.cs +++ b/Modbus.Net/Modbus.Net/Linker/ProtocolLinker.cs @@ -47,7 +47,7 @@ namespace Modbus.Net var bytesExtend = Activator.CreateInstance(GetType().GetTypeInfo().Assembly.GetType(GetType().FullName + "BytesExtend")) as - IProtocolLinkerBytesExtend; + IProtocolLinkerBytesExtend; return bytesExtend?.BytesExtend(content); } @@ -62,7 +62,7 @@ namespace Modbus.Net var bytesExtend = Activator.CreateInstance(GetType().GetTypeInfo().Assembly.GetType(GetType().FullName + "BytesExtend")) as - IProtocolLinkerBytesExtend; + IProtocolLinkerBytesExtend; return bytesExtend?.BytesDecact(content); } diff --git a/Modbus.Net/Modbus.Net/Machine/AddressCombiner.cs b/Modbus.Net/Modbus.Net/Machine/AddressCombiner.cs index c059e0c..48558a7 100644 --- a/Modbus.Net/Modbus.Net/Machine/AddressCombiner.cs +++ b/Modbus.Net/Modbus.Net/Machine/AddressCombiner.cs @@ -4,13 +4,6 @@ using System.Linq; namespace Modbus.Net { - /// - /// 地址组合器,组合后的每一组地址将只需一次向设备进行通讯 - /// - public abstract class AddressCombiner : AddressCombiner - { - } - /// /// 地址组合器,组合后的每一组地址将只需一次向设备进行通讯 /// @@ -24,22 +17,6 @@ namespace Modbus.Net public abstract IEnumerable> Combine(IEnumerable> addresses); } - /// - /// 连续的地址将组合成一组,向设备进行通讯 - /// - public class AddressCombinerContinus : AddressCombinerContinus - { - /// - /// 构造函数 - /// - /// 地址转换器 - /// 单个发送协议允许的数据最长长度(字节) - public AddressCombinerContinus(AddressTranslator addressTranslator, int maxLength) - : base(addressTranslator, maxLength) - { - } - } - /// /// 连续的地址将组合成一组,向设备进行通讯 /// @@ -226,13 +203,6 @@ namespace Modbus.Net } } - /// - /// 单个地址变为一组,每一个地址都进行一次查询 - /// - public class AddressCombinerSingle : AddressCombinerSingle - { - } - /// /// 单个地址变为一组,每一个地址都进行一次查询 /// @@ -269,23 +239,6 @@ namespace Modbus.Net public int GapNumber { get; set; } } - /// - /// 可以调过多少数量的地址,把两个地址段变为一组通讯 - /// - public class AddressCombinerNumericJump : AddressCombinerNumericJump - { - /// - /// 构造函数 - /// - /// 需要跳过的字节个数 - /// 单个协议允许的数据最长长度(字节) - /// 地址转换器 - public AddressCombinerNumericJump(int jumpByteCount, int maxLength, AddressTranslator addressTranslator) - : base(jumpByteCount, maxLength, addressTranslator) - { - } - } - /// /// 可以调过多少数量的地址,把两个地址段变为一组通讯 /// @@ -382,23 +335,6 @@ namespace Modbus.Net } } - /// - /// 可以调过多少百分比的地址,把两个地址段变为一个 - /// - public class AddressCombinerPercentageJump : AddressCombinerPercentageJump - { - /// - /// 构造函数 - /// - /// 允许跳过的字节数除以待组合的地址的字节数的百分比 - /// 单个协议允许的数据最大长度 - /// 地址转换器 - public AddressCombinerPercentageJump(double percentage, int maxLength, AddressTranslator addressTranslator) - : base(percentage, maxLength, addressTranslator) - { - } - } - /// /// 可以调过多少百分比的地址,把两个地址段变为一个 /// diff --git a/Modbus.Net/Modbus.Net/Machine/BaseMachine.cs b/Modbus.Net/Modbus.Net/Machine/BaseMachine.cs index 204fe85..b4c270f 100644 --- a/Modbus.Net/Modbus.Net/Machine/BaseMachine.cs +++ b/Modbus.Net/Modbus.Net/Machine/BaseMachine.cs @@ -32,45 +32,6 @@ namespace Modbus.Net Id } - /// - /// 设备 - /// - public abstract class BaseMachine : BaseMachine - { - /// - /// 构造器 - /// - /// 设备的ID号 - /// 需要与设备通讯的地址 - protected BaseMachine(string id, IEnumerable getAddresses) : base(id, getAddresses) - { - } - - /// - /// 构造器 - /// - /// 设备的ID号 - /// 需要与设备通讯的地址 - /// 是否保持连接 - protected BaseMachine(string id, IEnumerable getAddresses, bool keepConnect) - : base(id, getAddresses, keepConnect) - { - } - - /// - /// 构造器 - /// - /// 设备的ID号 - /// 需要与设备通讯的地址 - /// 是否保持连接 - /// 从站地址 - /// 主站地址 - protected BaseMachine(string id, IEnumerable getAddresses, bool keepConnect, byte slaveAddress, - byte masterAddress) : base(id, getAddresses, keepConnect, slaveAddress, masterAddress) - { - } - } - /// /// 设备 /// @@ -193,7 +154,7 @@ namespace Modbus.Net /// 读取数据 /// /// 从设备读取的数据 - public ReturnStruct> GetDatas(MachineDataType getDataType) + public ReturnStruct>> GetDatas(MachineDataType getDataType) { return AsyncHelper.RunSync(() => GetDatasAsync(getDataType)); } @@ -203,17 +164,17 @@ namespace Modbus.Net /// 读取数据 /// /// 从设备读取的数据 - public async Task>> GetDatasAsync(MachineDataType getDataType) + public async Task>>> GetDatasAsync(MachineDataType getDataType) { try { - var ans = new Dictionary(); + var ans = new Dictionary>(); //检测并连接设备 if (!BaseUtility.IsConnected) await BaseUtility.ConnectAsync(); //如果无法连接,终止 if (!BaseUtility.IsConnected) return - new ReturnStruct>() + new ReturnStruct>>() { Datas = null, IsSuccess = false, @@ -238,7 +199,7 @@ namespace Modbus.Net //如果没有数据,终止 if (datas.IsSuccess == false || datas.Datas == null) { - return new ReturnStruct>() + return new ReturnStruct>>() { Datas = null, IsSuccess = false, @@ -252,7 +213,7 @@ namespace Modbus.Net BigEndianValueHelper.Instance.ByteLength[ communicateAddress.DataType.FullName])) { - return new ReturnStruct>() + return new ReturnStruct>>() { Datas = null, IsSuccess = false, @@ -310,14 +271,14 @@ namespace Modbus.Net { //如果没有数据返回空 if (datas.Datas.Length == 0) - ans.Add(key, new ReturnUnit + ans.Add(key, new ReturnUnit { DeviceValue = null, AddressUnit = address.MapAddressUnitTUnitKeyToAddressUnit(), }); else ans.Add(key, - new ReturnUnit + new ReturnUnit { DeviceValue = Convert.ToDouble( @@ -334,7 +295,7 @@ namespace Modbus.Net if (ErrorCount >= _maxErrorCount) Disconnect(); - return new ReturnStruct>() + return new ReturnStruct>>() { Datas = null, IsSuccess = false, @@ -350,7 +311,7 @@ namespace Modbus.Net //返回数据 if (ans.All(p => p.Value.DeviceValue == null)) ans = null; ErrorCount = 0; - return new ReturnStruct> + return new ReturnStruct>> { Datas = ans, IsSuccess = true, @@ -365,7 +326,7 @@ namespace Modbus.Net if (ErrorCount >= _maxErrorCount) Disconnect(); - return new ReturnStruct>() + return new ReturnStruct>>() { Datas = null, IsSuccess = false, @@ -676,17 +637,17 @@ namespace Modbus.Net } /// - public async Task> InvokeGet(string functionName, object[] parameters) + 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 await (Task>)ans; + return (Task>)ans; } /// - public async Task> InvokeSet(string functionName, object[] parameters, T datas) + public Task> InvokeSet(string functionName, object[] parameters, T datas) { var machineMethodType = GetType(); var machineMethod = this as IMachineMethod; @@ -695,7 +656,7 @@ namespace Modbus.Net Array.Copy(parameters, allParams, parameters.Length); allParams[parameters.Length] = datas; var ans = machineSetMethod.Invoke(machineMethod, allParams); - return await (Task>)ans; + return (Task>)ans; } /// @@ -716,15 +677,6 @@ namespace Modbus.Net return BaseUtility.Disconnect(); } - /// - /// 获取设备的Id,字符串格式 - /// - /// - public string GetMachineIdString() - { - return Id.ToString(); - } - /// /// 通过Id获取数据字段定义 /// @@ -758,13 +710,6 @@ namespace Modbus.Net } } - /// - /// 通讯单元 - /// - public class CommunicationUnit : CommunicationUnit - { - } - /// /// 通讯单元 /// @@ -811,12 +756,12 @@ namespace Modbus.Net /// /// 返回的数据单元 /// - public class ReturnUnit + public class ReturnUnit where TReturn : struct { /// /// 返回的数据 /// - public double? DeviceValue { get; set; } + public TReturn? DeviceValue { get; set; } /// /// 数据定义 @@ -906,34 +851,4 @@ namespace Modbus.Net return Area.ToUpper() == other.Area.ToUpper() && Address == other.Address || Id.Equals(other.Id); } } - - /// - /// AddressUnit扩展 - /// - public static class AddressUnitExtend - { - /// - /// 映射泛型AddressUnit到字符串型AddressUnit - /// - /// - /// - public static AddressUnit MapAddressUnitTUnitKeyToAddressUnit(this AddressUnit addressUnit) where TUnitKey : IEquatable - { - return new AddressUnit() - { - Id = addressUnit.ToString(), - Area = addressUnit.Area, - Address = addressUnit.Address, - SubAddress = addressUnit.SubAddress, - DataType = addressUnit.DataType, - Zoom = addressUnit.Zoom, - DecimalPos = addressUnit.DecimalPos, - CommunicationTag = addressUnit.CommunicationTag, - Name = addressUnit.Name, - Unit = addressUnit.Unit, - CanWrite = addressUnit.CanWrite, - UnitExtend = addressUnit.UnitExtend - }; - } - } } \ No newline at end of file diff --git a/Modbus.Net/Modbus.Net/Machine/BaseMachineExtend.cs b/Modbus.Net/Modbus.Net/Machine/BaseMachineExtend.cs index 0aad808..4a90002 100644 --- a/Modbus.Net/Modbus.Net/Machine/BaseMachineExtend.cs +++ b/Modbus.Net/Modbus.Net/Machine/BaseMachineExtend.cs @@ -1,4 +1,5 @@ -using System.Collections.Generic; +using System; +using System.Collections.Generic; using System.Linq; namespace Modbus.Net @@ -14,13 +15,43 @@ namespace Modbus.Net /// /// 获取的数据 /// 应该写入的数据 - public static Dictionary MapGetValuesToSetValues(this Dictionary getValues) + public static Dictionary MapGetValuesToSetValues(this Dictionary> getValues) where TSend : struct { if (getValues == null) return null; return (from getValue in getValues where getValue.Value.DeviceValue != null - select new KeyValuePair(getValue.Key, getValue.Value.DeviceValue.Value)).ToDictionary( + select new KeyValuePair(getValue.Key, getValue.Value.DeviceValue.Value)).ToDictionary( p => p.Key, p => p.Value); } } + + /// + /// AddressUnit扩展 + /// + public static class AddressUnitExtend + { + /// + /// 映射泛型AddressUnit到字符串型AddressUnit + /// + /// + /// + public static AddressUnit MapAddressUnitTUnitKeyToAddressUnit(this AddressUnit addressUnit) where TUnitKey : IEquatable + { + return new AddressUnit() + { + Id = addressUnit.ToString(), + Area = addressUnit.Area, + Address = addressUnit.Address, + SubAddress = addressUnit.SubAddress, + DataType = addressUnit.DataType, + Zoom = addressUnit.Zoom, + DecimalPos = addressUnit.DecimalPos, + CommunicationTag = addressUnit.CommunicationTag, + Name = addressUnit.Name, + Unit = addressUnit.Unit, + CanWrite = addressUnit.CanWrite, + UnitExtend = addressUnit.UnitExtend + }; + } + } } \ No newline at end of file diff --git a/Modbus.Net/Modbus.Net/Protocol/BaseProtocol.cs b/Modbus.Net/Modbus.Net/Protocol/BaseProtocol.cs index a24ae7d..ad6682e 100644 --- a/Modbus.Net/Modbus.Net/Protocol/BaseProtocol.cs +++ b/Modbus.Net/Modbus.Net/Protocol/BaseProtocol.cs @@ -8,7 +8,7 @@ namespace Modbus.Net /// /// 基本协议 /// - public abstract class BaseProtocol : BaseProtocol + public abstract class BaseProtocol : BaseProtocol, PipeUnit> { /// /// 构造器 @@ -42,7 +42,7 @@ namespace Modbus.Net /// 输入信息的结构化描述 /// 输出信息的结构化描述 public override async Task - SendReceiveAsync(ProtocolUnit unit, IInputStruct content) + SendReceiveAsync(ProtocolUnit unit, IInputStruct content) { if (content != null) { diff --git a/Modbus.Net/Modbus.Net/Protocol/PipeUnit.cs b/Modbus.Net/Modbus.Net/Protocol/PipeUnit.cs index 4d49d7b..53443e6 100644 --- a/Modbus.Net/Modbus.Net/Protocol/PipeUnit.cs +++ b/Modbus.Net/Modbus.Net/Protocol/PipeUnit.cs @@ -8,7 +8,7 @@ namespace Modbus.Net /// /// 管道单元 /// - public class PipeUnit : PipeUnit, ProtocolUnit> + public class PipeUnit : PipeUnit, ProtocolUnit> { /// /// 构造函数 @@ -26,7 +26,7 @@ namespace Modbus.Net /// 协议单元 /// 传递给输入结构的参数 /// 上次的管道是否成功执行 - protected PipeUnit(IProtocolLinker protocolLinker, ProtocolUnit protocolUnit, byte[] parameters, + protected PipeUnit(IProtocolLinker protocolLinker, ProtocolUnit protocolUnit, byte[] parameters, bool success) : base(protocolLinker, protocolUnit, parameters, success) { } @@ -44,7 +44,7 @@ namespace Modbus.Net var content = inputStructCreator.Invoke(ReturnParams); if (ProtocolLinker != null) return new PipeUnit(ProtocolLinker, null, - await ProtocolLinker.SendReceiveAsync(ProtocolUnit.TranslateContent(endian, content)), + await ProtocolLinker.SendReceiveAsync(ProtocolUnit.TranslateContent(endian, content)), true); } return new PipeUnit(ProtocolLinker, null, ReturnParams, false); @@ -57,7 +57,7 @@ namespace Modbus.Net /// 构造输入结构的函数 /// 发送完成之后新的管道实例 public new async Task SendReceiveAsync( - ProtocolUnit unit, + ProtocolUnit unit, Func inputStructCreator) { var receiveContent = await SendReceiveAsyncParamOut(unit, inputStructCreator); diff --git a/Modbus.Net/Modbus.Net/Protocol/ProtocolUnit.cs b/Modbus.Net/Modbus.Net/Protocol/ProtocolUnit.cs index 6de5503..bd50e86 100644 --- a/Modbus.Net/Modbus.Net/Protocol/ProtocolUnit.cs +++ b/Modbus.Net/Modbus.Net/Protocol/ProtocolUnit.cs @@ -2,13 +2,6 @@ namespace Modbus.Net { - /// - /// 协议单元 - /// - public abstract class ProtocolUnit : ProtocolUnit - { - } - /// /// 协议单元 /// diff --git a/Modbus.Net/Modbus.Net/Utility/BaseUtility.cs b/Modbus.Net/Modbus.Net/Utility/BaseUtility.cs index ae0e3bf..313f23f 100644 --- a/Modbus.Net/Modbus.Net/Utility/BaseUtility.cs +++ b/Modbus.Net/Modbus.Net/Utility/BaseUtility.cs @@ -27,19 +27,6 @@ public enum Endian namespace Modbus.Net { - /// - /// 基础Api入口 - /// - public abstract class BaseUtility : BaseUtility - { - /// - /// 构造器 - /// - protected BaseUtility(byte slaveAddress, byte masterAddress) : base(slaveAddress, masterAddress) - { - } - } - /// /// 基础Api入口 /// diff --git a/Samples/AnyType/AnyType.csproj b/Samples/AnyType/AnyType.csproj index f78e501..c56bc86 100644 --- a/Samples/AnyType/AnyType.csproj +++ b/Samples/AnyType/AnyType.csproj @@ -11,8 +11,4 @@ - - - - diff --git a/Samples/AnyType/Controllers/HomeController.cs b/Samples/AnyType/Controllers/HomeController.cs index c619e34..aadb87a 100644 --- a/Samples/AnyType/Controllers/HomeController.cs +++ b/Samples/AnyType/Controllers/HomeController.cs @@ -3,6 +3,8 @@ using Microsoft.AspNetCore.Mvc; using Modbus.Net; using Modbus.Net.Modbus; using System.Diagnostics; +using MachineJobSchedulerCreator = Modbus.Net.MachineJobSchedulerCreator; +using ModbusMachine = Modbus.Net.Modbus.ModbusMachine; namespace AnyType.Controllers { diff --git a/Samples/CrossLamp/Controllers/HomeController.cs b/Samples/CrossLamp/Controllers/HomeController.cs index cd0a49e..4e9c5dd 100644 --- a/Samples/CrossLamp/Controllers/HomeController.cs +++ b/Samples/CrossLamp/Controllers/HomeController.cs @@ -16,7 +16,7 @@ namespace CrossLamp.Controllers _logger = logger; } - private static BaseUtility? _utility = null; + private static IUtility? _utility = null; public ActionResult Index() { diff --git a/Samples/TripleAdd/Controllers/HomeController.cs b/Samples/TripleAdd/Controllers/HomeController.cs index 9131e0c..8f35725 100644 --- a/Samples/TripleAdd/Controllers/HomeController.cs +++ b/Samples/TripleAdd/Controllers/HomeController.cs @@ -15,8 +15,8 @@ namespace TripleAdd.Controllers _logger = logger; } - private static BaseUtility? utility; - private static BaseMachine? machine; + private static IUtility? utility; + private static IMachine? machine; public ActionResult Index() { @@ -54,15 +54,15 @@ namespace TripleAdd.Controllers { if (machine == null) { - machine = new ModbusMachine("1", ModbusType.Tcp, "192.168.0.161", new List() + machine = new ModbusMachine("1", ModbusType.Tcp, "192.168.0.161", new List() { new AddressUnit() {Id = "1", Area = "4X", Address = 1, CommunicationTag = "Add1", DataType = typeof(ushort), Zoom = 1, DecimalPos = 0}, new AddressUnit() {Id = "2", Area = "4X", Address = 2, CommunicationTag = "Add2", DataType = typeof(ushort), Zoom = 1, DecimalPos = 0}, new AddressUnit() {Id = "3", Area = "4X", Address = 3, CommunicationTag = "Add3", DataType = typeof(ushort), Zoom = 1, DecimalPos = 0}, new AddressUnit() {Id = "4", Area = "4X", Address = 4, CommunicationTag = "Ans", DataType = typeof(ushort), Zoom = 1, DecimalPos = 0}, }, 2, 0); - machine.AddressCombiner = new AddressCombinerContinus(machine.AddressTranslator, 100000); - machine.AddressCombinerSet = new AddressCombinerContinus(machine.AddressTranslator, 100000); + machine.AddressCombiner = new AddressCombinerContinus(machine.AddressTranslator, 100000); + machine.AddressCombinerSet = new AddressCombinerContinus(machine.AddressTranslator, 100000); } var resultFormat = (await machine.GetDatasAsync(MachineDataType.CommunicationTag)).Datas.MapGetValuesToSetValues(); return SetValue(new ushort[4] { (ushort)resultFormat["Add1"], (ushort)resultFormat["Add2"], (ushort)resultFormat["Add3"], (ushort)resultFormat["Ans"] }); diff --git a/Tests/Modbus.Net.Tests/EndianTest.cs b/Tests/Modbus.Net.Tests/EndianTest.cs index 90ed982..705070e 100644 --- a/Tests/Modbus.Net.Tests/EndianTest.cs +++ b/Tests/Modbus.Net.Tests/EndianTest.cs @@ -6,16 +6,16 @@ namespace Modbus.Net.Tests [TestClass] public class EndianTest { - private BaseMachine? _modbusTcpMachine; + private BaseMachine? _modbusTcpMachine; - private BaseMachine? _modbusTcpMachine2; + private BaseMachine? _modbusTcpMachine2; [TestInitialize] public void Init() { - _modbusTcpMachine = new ModbusMachine("1", ModbusType.Tcp, "127.0.0.1", null, true, 1, 0); + _modbusTcpMachine = new ModbusMachine("1", ModbusType.Tcp, "127.0.0.1", null, true, 1, 0); - _modbusTcpMachine2 = new ModbusMachine("2", ModbusType.Tcp, "127.0.0.1", null, true, 1, 0, Endian.LittleEndianLsb); + _modbusTcpMachine2 = new ModbusMachine("2", ModbusType.Tcp, "127.0.0.1", null, true, 1, 0, Endian.LittleEndianLsb); } [TestMethod] diff --git a/Tests/Modbus.Net.Tests/MachineMethodTest.cs b/Tests/Modbus.Net.Tests/MachineMethodTest.cs index 491b617..c7f6210 100644 --- a/Tests/Modbus.Net.Tests/MachineMethodTest.cs +++ b/Tests/Modbus.Net.Tests/MachineMethodTest.cs @@ -44,7 +44,7 @@ namespace Modbus.Net.Tests DataType = typeof(bool) } }, true, 2, 0); - var success = await baseMachine.GetMachineMethods().SetDatasAsync( + var success = await baseMachine.GetMachineMethods().SetDatasAsync( MachineDataType.Address, new Dictionary { @@ -53,9 +53,9 @@ namespace Modbus.Net.Tests } }); Assert.AreEqual(success, true); - var datas = await baseMachine.GetMachineMethods().GetDatasAsync(MachineDataType.Address); + var datas = await baseMachine.GetMachineMethods().GetDatasAsync(MachineDataType.Address); Assert.AreEqual(datas.Datas["0X 1.0"].DeviceValue, 1); - success = await baseMachine.GetMachineMethods().SetDatasAsync( + success = await baseMachine.GetMachineMethods().SetDatasAsync( MachineDataType.Address, new Dictionary { diff --git a/Tests/Modbus.Net.Tests/ModbusMultiStationTest.cs b/Tests/Modbus.Net.Tests/ModbusMultiStationTest.cs index 1df4a80..ac6beb2 100644 --- a/Tests/Modbus.Net.Tests/ModbusMultiStationTest.cs +++ b/Tests/Modbus.Net.Tests/ModbusMultiStationTest.cs @@ -6,15 +6,15 @@ namespace Modbus.Net.Tests [TestClass] public class ModbusMultiStationTest { - private BaseMachine? _modbusRtuMachine1; + private BaseMachine? _modbusRtuMachine1; - private BaseMachine? _modbusRtuMachine2; + private BaseMachine? _modbusRtuMachine2; [TestInitialize] public void Init() { - _modbusRtuMachine1 = new ModbusMachine("1", ModbusType.Rtu, "COM1", null, true, 1, 0); - _modbusRtuMachine2 = new ModbusMachine("2", ModbusType.Rtu, "COM1", null, true, 2, 0); + _modbusRtuMachine1 = new ModbusMachine("1", ModbusType.Rtu, "COM1", null, true, 1, 0); + _modbusRtuMachine2 = new ModbusMachine("2", ModbusType.Rtu, "COM1", null, true, 2, 0); } [TestMethod] diff --git a/Tests/Modbus.Net.Tests/ModbusTest.cs b/Tests/Modbus.Net.Tests/ModbusTest.cs index baa2a04..c148ee7 100644 --- a/Tests/Modbus.Net.Tests/ModbusTest.cs +++ b/Tests/Modbus.Net.Tests/ModbusTest.cs @@ -6,20 +6,20 @@ namespace Modbus.Net.Tests [TestClass] public class ModbusTest { - private BaseMachine? _modbusTcpMachine; + private BaseMachine? _modbusTcpMachine; - private BaseMachine? _modbusRtuMachine; + private BaseMachine? _modbusRtuMachine; - private BaseMachine? _modbusAsciiMachine; + private BaseMachine? _modbusAsciiMachine; [TestInitialize] public void Init() { - _modbusTcpMachine = new ModbusMachine("1", ModbusType.Tcp, "192.168.3.10", null, true, 2, 0); + _modbusTcpMachine = new ModbusMachine("1", ModbusType.Tcp, "192.168.3.10", null, true, 2, 0); - _modbusRtuMachine = new ModbusMachine("2", ModbusType.Rtu, "COM5", null, true, 2, 0); + _modbusRtuMachine = new ModbusMachine("2", ModbusType.Rtu, "COM5", null, true, 2, 0); - _modbusAsciiMachine = new ModbusMachine("3", ModbusType.Ascii, "COM5", null, true, 2, 0); + _modbusAsciiMachine = new ModbusMachine("3", ModbusType.Ascii, "COM5", null, true, 2, 0); } [TestMethod] diff --git a/Tests/Modbus.Net.Tests/SiemensTest.cs b/Tests/Modbus.Net.Tests/SiemensTest.cs index 6254b45..89d47ad 100644 --- a/Tests/Modbus.Net.Tests/SiemensTest.cs +++ b/Tests/Modbus.Net.Tests/SiemensTest.cs @@ -6,12 +6,12 @@ namespace Modbus.Net.Tests [TestClass] public class SiemensTest { - private BaseMachine? _siemensTcpMachine; + private BaseMachine? _siemensTcpMachine; [TestInitialize] public void Init() { - _siemensTcpMachine = new SiemensMachine("1", SiemensType.Tcp, "192.168.3.10", SiemensMachineModel.S7_1200, null, true, 2, 0); + _siemensTcpMachine = new SiemensMachine("1", SiemensType.Tcp, "192.168.3.10", SiemensMachineModel.S7_1200, null, true, 2, 0); } [TestMethod]