diff --git a/Modbus.Net/Modbus.Net.Modbus/AddressTranslatorModbus.cs b/Modbus.Net/Modbus.Net.Modbus/AddressTranslatorModbus.cs index c92cb40..87fd69e 100644 --- a/Modbus.Net/Modbus.Net.Modbus/AddressTranslatorModbus.cs +++ b/Modbus.Net/Modbus.Net.Modbus/AddressTranslatorModbus.cs @@ -3,6 +3,9 @@ using System.Linq; namespace Modbus.Net.Modbus { + /// + /// Modbus地址翻译器基类 + /// public abstract class ModbusTranslatorBase : AddressTranslator { /// diff --git a/Modbus.Net/Modbus.Net.Modbus/ModbusAsciiInTcpProtocol.cs b/Modbus.Net/Modbus.Net.Modbus/ModbusAsciiInTcpProtocol.cs index 4256257..b365e52 100644 --- a/Modbus.Net/Modbus.Net.Modbus/ModbusAsciiInTcpProtocol.cs +++ b/Modbus.Net/Modbus.Net.Modbus/ModbusAsciiInTcpProtocol.cs @@ -21,7 +21,7 @@ namespace Modbus.Net.Modbus /// /// 构造函数 /// - /// 串口地址 + /// ip地址 /// 从站号 /// 主站号 /// 端格式 diff --git a/Modbus.Net/Modbus.Net.Modbus/ModbusMachine.cs b/Modbus.Net/Modbus.Net.Modbus/ModbusMachine.cs index 1fd6606..42a4c17 100644 --- a/Modbus.Net/Modbus.Net.Modbus/ModbusMachine.cs +++ b/Modbus.Net/Modbus.Net.Modbus/ModbusMachine.cs @@ -12,6 +12,7 @@ namespace Modbus.Net.Modbus /// /// 构造函数 /// + /// 设备的ID号 /// 连接类型 /// 连接地址 /// 读写的地址 @@ -33,6 +34,7 @@ namespace Modbus.Net.Modbus /// /// 构造函数 /// + /// 设备的ID号 /// 连接类型 /// 连接地址 /// 读写的地址 @@ -55,6 +57,7 @@ namespace Modbus.Net.Modbus /// /// 构造函数 /// + /// 设备的ID号 /// 连接类型 /// 连接地址 /// 读写的地址 @@ -76,6 +79,7 @@ namespace Modbus.Net.Modbus /// /// 构造函数 /// + /// 设备的ID号 /// 连接类型 /// 连接地址 /// 读写的地址 diff --git a/Modbus.Net/Modbus.Net.Modbus/ModbusProtocol.cs b/Modbus.Net/Modbus.Net.Modbus/ModbusProtocol.cs index fe10626..0a52249 100644 --- a/Modbus.Net/Modbus.Net.Modbus/ModbusProtocol.cs +++ b/Modbus.Net/Modbus.Net.Modbus/ModbusProtocol.cs @@ -396,7 +396,6 @@ namespace Modbus.Net.Modbus var translateAddress = addressTranslator.AddressTranslate(startAddress, false, true); FunctionCode = (byte) translateAddress.Area; StartAddress = (ushort) translateAddress.Address; - int a = 0, b = 0; var writeByteValue = FunctionCode == (byte) ModbusProtocolWriteDataFunctionCode.WriteSingleCoil ? ((bool) writeValue diff --git a/Modbus.Net/Modbus.Net.Modbus/ModbusProtocolLinkerBytesExtend.cs b/Modbus.Net/Modbus.Net.Modbus/ModbusProtocolLinkerBytesExtend.cs index 4063964..2cd46ff 100644 --- a/Modbus.Net/Modbus.Net.Modbus/ModbusProtocolLinkerBytesExtend.cs +++ b/Modbus.Net/Modbus.Net.Modbus/ModbusProtocolLinkerBytesExtend.cs @@ -5,11 +5,17 @@ using System.Text; namespace Modbus.Net.Modbus { + /// + /// Rtu透传字节伸缩 + /// public class ModbusRtuInTcpProtocolLinkerBytesExtend : ModbusRtuProtocolLinkerBytesExtend { } + /// + /// Ascii透传字节伸缩 + /// public class ModbusAsciiInTcpProtocolLinkerBytesExtend : ModbusAsciiProtocolLinkerBytesExtend { diff --git a/Modbus.Net/Modbus.Net.Modbus/ModbusRtuInTcpProtocol.cs b/Modbus.Net/Modbus.Net.Modbus/ModbusRtuInTcpProtocol.cs index a209c04..e5d8102 100644 --- a/Modbus.Net/Modbus.Net.Modbus/ModbusRtuInTcpProtocol.cs +++ b/Modbus.Net/Modbus.Net.Modbus/ModbusRtuInTcpProtocol.cs @@ -35,7 +35,8 @@ namespace Modbus.Net.Modbus /// /// 构造函数 /// - /// 串口 + /// ip地址 + /// 端口号 /// 从站号 /// 主站号 /// 端格式 diff --git a/Modbus.Net/Modbus.Net.Modbus/ModbusUtility.cs b/Modbus.Net/Modbus.Net.Modbus/ModbusUtility.cs index 3d0e578..d2eed40 100644 --- a/Modbus.Net/Modbus.Net.Modbus/ModbusUtility.cs +++ b/Modbus.Net/Modbus.Net.Modbus/ModbusUtility.cs @@ -35,6 +35,9 @@ namespace Modbus.Net.Modbus AsciiInTcp = 4, } + /// + /// 写单个单元的方法接口 + /// public interface IUtilityMethodWriteSingle : IUtilityMethod { /// diff --git a/Modbus.Net/Modbus.Net.OPC/FBox/FBoxOpcDaManchine.cs b/Modbus.Net/Modbus.Net.OPC/FBox/FBoxOpcDaManchine.cs index f0741d1..d8511ad 100644 --- a/Modbus.Net/Modbus.Net.OPC/FBox/FBoxOpcDaManchine.cs +++ b/Modbus.Net/Modbus.Net.OPC/FBox/FBoxOpcDaManchine.cs @@ -11,6 +11,7 @@ namespace Modbus.Net.OPC.FBox /// /// 构造函数 /// + /// 设备的ID号 /// 页名称 /// 设备名称 /// 获取地址 @@ -36,6 +37,7 @@ namespace Modbus.Net.OPC.FBox /// /// 构造函数 /// + /// 设备的ID号 /// 页名称 /// 设备名称 /// 获取地址 diff --git a/Modbus.Net/Modbus.Net.OPC/OpcDaMachine.cs b/Modbus.Net/Modbus.Net.OPC/OpcDaMachine.cs index 805ec17..898ab56 100644 --- a/Modbus.Net/Modbus.Net.OPC/OpcDaMachine.cs +++ b/Modbus.Net/Modbus.Net.OPC/OpcDaMachine.cs @@ -14,6 +14,7 @@ namespace Modbus.Net.OPC /// /// 构造函数 /// + /// 设备的ID号 /// 连接地址 /// 需要读写的数据 /// 是否保持连接 @@ -29,6 +30,7 @@ namespace Modbus.Net.OPC /// /// 构造函数 /// + /// 设备的ID号 /// 连接地址 /// 需要读写的数据 public OpcDaMachine(TKey id, string connectionString, IEnumerable> getAddresses) @@ -45,6 +47,7 @@ namespace Modbus.Net.OPC /// /// 构造函数 /// + /// 设备的ID号 /// 连接地址 /// 需要读写的数据 /// 是否保持连接 @@ -60,6 +63,7 @@ namespace Modbus.Net.OPC /// /// 构造函数 /// + /// 设备的ID号 /// 连接地址 /// 需要读写的数据 public OpcDaMachine(string id, string connectionString, IEnumerable getAddresses) diff --git a/Modbus.Net/Modbus.Net.OPC/OpcMachine.cs b/Modbus.Net/Modbus.Net.OPC/OpcMachine.cs index 8709a86..01aa686 100644 --- a/Modbus.Net/Modbus.Net.OPC/OpcMachine.cs +++ b/Modbus.Net/Modbus.Net.OPC/OpcMachine.cs @@ -12,6 +12,7 @@ namespace Modbus.Net.OPC /// /// 构造函数 /// + /// 设备的ID号 /// 需要读写的地址 /// 是否保持连接 protected OpcMachine(TKey id, IEnumerable> getAddresses, bool keepConnect) @@ -30,6 +31,7 @@ namespace Modbus.Net.OPC /// /// 构造函数 /// + /// 设备的ID号 /// 需要读写的地址 /// 是否保持连接 protected OpcMachine(string id, IEnumerable getAddresses, bool keepConnect) diff --git a/Modbus.Net/Modbus.Net.OPC/OpcUaMachine.cs b/Modbus.Net/Modbus.Net.OPC/OpcUaMachine.cs index d4657b9..dd0db53 100644 --- a/Modbus.Net/Modbus.Net.OPC/OpcUaMachine.cs +++ b/Modbus.Net/Modbus.Net.OPC/OpcUaMachine.cs @@ -14,6 +14,7 @@ namespace Modbus.Net.OPC /// /// 构造函数 /// + /// 设备的ID号 /// 连接地址 /// 需要读写的数据 /// 是否保持连接 @@ -29,6 +30,7 @@ namespace Modbus.Net.OPC /// /// 构造函数 /// + /// 设备的ID号 /// 连接地址 /// 需要读写的数据 public OpcUaMachine(TKey id, string connectionString, IEnumerable> getAddresses) @@ -45,6 +47,7 @@ namespace Modbus.Net.OPC /// /// 构造函数 /// + /// 设备的ID号 /// 连接地址 /// 需要读写的数据 /// 是否保持连接 @@ -60,6 +63,7 @@ namespace Modbus.Net.OPC /// /// 构造函数 /// + /// 设备的ID号 /// 连接地址 /// 需要读写的数据 public OpcUaMachine(string id, string connectionString, IEnumerable getAddresses) diff --git a/Modbus.Net/Modbus.Net/BaseConnector.cs b/Modbus.Net/Modbus.Net/BaseConnector.cs index b6ce5d8..de185da 100644 --- a/Modbus.Net/Modbus.Net/BaseConnector.cs +++ b/Modbus.Net/Modbus.Net/BaseConnector.cs @@ -18,11 +18,18 @@ namespace Modbus.Net //protected event MessageReturnDelegate MessageReturn; + /// + /// 增加传输控制器 + /// + /// 传输控制器 public void AddController(IController controller) { Controller = controller; } + /// + /// 传输控制器 + /// protected IController Controller { get; set; } /// diff --git a/Modbus.Net/Modbus.Net/BaseController.cs b/Modbus.Net/Modbus.Net/BaseController.cs index c870ad8..9547bcd 100644 --- a/Modbus.Net/Modbus.Net/BaseController.cs +++ b/Modbus.Net/Modbus.Net/BaseController.cs @@ -7,18 +7,30 @@ using Serilog; namespace Modbus.Net { + /// + /// + /// public abstract class BaseController : IController { + /// + /// ȴϢ + /// protected List WaitingMessages { get; set; } + /// + /// Ϣά߳ + /// protected Task SendingThread { get; set; } - protected bool _taskCancel = false; + /// + /// + /// protected BaseController() { WaitingMessages = new List(); } + /// public MessageWaitingDef AddMessage(byte[] sendMessage) { var def = new MessageWaitingDef @@ -32,13 +44,15 @@ namespace Modbus.Net return def; } + /// + /// Ϣʵڲ + /// protected abstract void SendingMessageControlInner(); - public void SendStop() - { - _taskCancel = true; - } + /// + public abstract void SendStop(); + /// public void SendStart() { if (SendingThread == null) @@ -47,6 +61,7 @@ namespace Modbus.Net } } + /// public void Clear() { lock (WaitingMessages) @@ -55,6 +70,10 @@ namespace Modbus.Net } } + /// + /// Ϣӵ + /// + /// ҪӵϢϢ protected virtual void AddMessageToList(MessageWaitingDef def) { lock (WaitingMessages) @@ -63,8 +82,14 @@ namespace Modbus.Net } } + /// + /// ȡϢļؼ + /// + /// ȷϵϢ + /// Ϣļؼ protected abstract string GetKeyFromMessage(byte[] message); + /// public bool ConfirmMessage(byte[] receiveMessage) { var def = GetMessageFromWaitingList(receiveMessage); @@ -81,8 +106,15 @@ namespace Modbus.Net return false; } + /// + /// ӵȴƥϢ + /// + /// صϢ + /// ӵȴƥϢ protected abstract MessageWaitingDef GetMessageFromWaitingList(byte[] receiveMessage); + /// + /// public void ForceRemoveWaitingMessage(MessageWaitingDef def) { lock (WaitingMessages) @@ -92,16 +124,34 @@ namespace Modbus.Net } } + /// + /// ȴϢĶ + /// public class MessageWaitingDef { + /// + /// ϢĹؼ + /// public string Key { get; set; } + /// + /// ͵Ϣ + /// public byte[] SendMessage { get; set; } + /// + /// յϢ + /// public byte[] ReceiveMessage { get; set; } + /// + /// ͵ź + /// public EventWaitHandle SendMutex { get; set; } + /// + /// յź + /// public EventWaitHandle ReceiveMutex { get; set; } } } \ No newline at end of file diff --git a/Modbus.Net/Modbus.Net/BaseMachine.cs b/Modbus.Net/Modbus.Net/BaseMachine.cs index fe58d17..bacfe6a 100644 --- a/Modbus.Net/Modbus.Net/BaseMachine.cs +++ b/Modbus.Net/Modbus.Net/BaseMachine.cs @@ -92,6 +92,7 @@ namespace Modbus.Net /// /// 构造器 /// + /// 设备的ID号 /// 需要与设备通讯的地址 protected BaseMachine(string id, IEnumerable getAddresses) : base(id, getAddresses) { @@ -100,6 +101,7 @@ namespace Modbus.Net /// /// 构造器 /// + /// 设备的ID号 /// 需要与设备通讯的地址 /// 是否保持连接 protected BaseMachine(string id, IEnumerable getAddresses, bool keepConnect) @@ -110,6 +112,7 @@ namespace Modbus.Net /// /// 构造器 /// + /// 设备的ID号 /// 需要与设备通讯的地址 /// 是否保持连接 /// 从站地址 @@ -134,6 +137,7 @@ namespace Modbus.Net /// /// 构造器 /// + /// 设备的ID号 /// 需要与设备通讯的地址 protected BaseMachine(TKey id, IEnumerable> getAddresses) : this(id, getAddresses, false) @@ -143,6 +147,7 @@ namespace Modbus.Net /// /// 构造器 /// + /// 设备的ID号 /// 需要与设备通讯的地址 /// 是否保持连接 protected BaseMachine(TKey id, IEnumerable> getAddresses, bool keepConnect) @@ -155,6 +160,7 @@ namespace Modbus.Net /// /// 构造器 /// + /// 设备的ID号 /// 需要与设备通讯的地址 /// 是否保持连接 /// 从站地址 @@ -188,8 +194,8 @@ namespace Modbus.Net /// public AddressTranslator AddressTranslator { - get { return BaseUtility.AddressTranslator; } - set { BaseUtility.AddressTranslator = value; } + get => BaseUtility.AddressTranslator; + set => BaseUtility.AddressTranslator = value; } /// diff --git a/Modbus.Net/Modbus.Net/ComConnector.cs b/Modbus.Net/Modbus.Net/ComConnector.cs index 37deaf3..cb998ae 100644 --- a/Modbus.Net/Modbus.Net/ComConnector.cs +++ b/Modbus.Net/Modbus.Net/ComConnector.cs @@ -377,6 +377,9 @@ namespace Modbus.Net return returnBytes.ByteToString(); } + /// + /// 确认串口是否已经打开,如果没有打开则尝试打开两次,连续失败直接释放连接资源 + /// protected void CheckOpen() { if (!SerialPort.IsOpen) @@ -439,6 +442,10 @@ namespace Modbus.Net return messageSendingdef; } + /// + /// 发送数据,不确认 + /// + /// 需要发送的数据 protected override async Task SendMsgWithoutConfirm(byte[] message) { using (await SerialPort.Lock.LockAsync()) @@ -459,11 +466,13 @@ namespace Modbus.Net } } + /// protected override void ReceiveMsgThreadStart() { _receiveThread = Task.Run(()=>ReceiveMessage()); } + /// protected override void ReceiveMsgThreadStop() { _taskCancel = true; diff --git a/Modbus.Net/Modbus.Net/FifoController.cs b/Modbus.Net/Modbus.Net/FifoController.cs index 8989c2a..bb1296e 100644 --- a/Modbus.Net/Modbus.Net/FifoController.cs +++ b/Modbus.Net/Modbus.Net/FifoController.cs @@ -8,17 +8,30 @@ using Serilog; namespace Modbus.Net { + /// + /// 先入先出式控制器 + /// public class FifoController : BaseController { private MessageWaitingDef _currentSendingPos; + private bool _taskCancel = false; + + /// + /// 间隔时间 + /// public int AcquireTime { get; } + /// + /// 构造器 + /// + /// 间隔时间 public FifoController(int acquireTime) { AcquireTime = acquireTime; } + /// protected override void SendingMessageControlInner() { try @@ -64,11 +77,19 @@ namespace Modbus.Net } + /// + public override void SendStop() + { + _taskCancel = true; + } + + /// protected override string GetKeyFromMessage(byte[] message) { return null; } + /// protected override MessageWaitingDef GetMessageFromWaitingList(byte[] receiveMessage) { return WaitingMessages.FirstOrDefault(); diff --git a/Modbus.Net/Modbus.Net/IController.cs b/Modbus.Net/Modbus.Net/IController.cs index 2ab2e38..b4cda35 100644 --- a/Modbus.Net/Modbus.Net/IController.cs +++ b/Modbus.Net/Modbus.Net/IController.cs @@ -1,17 +1,43 @@ namespace Modbus.Net { + /// + /// ӿ + /// public interface IController { + /// + /// Ϣ + /// + /// Ҫ͵Ϣ + /// MessageWaitingDef AddMessage(byte[] sendMessage); - void SendStop(); - + /// + /// ߳ + /// void SendStart(); + /// + /// رմ߳ + /// + void SendStop(); + + /// + /// д͵Ϣ + /// void Clear(); + /// + /// صϢ󶨵͵ϢϣϢȷ + /// + /// صϢ + /// Ƿȷ bool ConfirmMessage(byte[] receiveMessage); + /// + /// ûκηʱǿɾȴϵϢ + /// + /// ҪǿɾϢ void ForceRemoveWaitingMessage(MessageWaitingDef def); } } diff --git a/Modbus.Net/Modbus.Net/IProtocol.cs b/Modbus.Net/Modbus.Net/IProtocol.cs index 376e0d3..57a1d05 100644 --- a/Modbus.Net/Modbus.Net/IProtocol.cs +++ b/Modbus.Net/Modbus.Net/IProtocol.cs @@ -9,6 +9,7 @@ namespace Modbus.Net /// 向Connector传入的类型 /// 从Connector返回的类型 /// 协议单元的类型 + /// 管道的类型 public interface IProtocol where TProtocolUnit : class, IProtocolFormatting where TParamOut : class diff --git a/Modbus.Net/Modbus.Net/MatchController.cs b/Modbus.Net/Modbus.Net/MatchController.cs index 883abc7..48f7e02 100644 --- a/Modbus.Net/Modbus.Net/MatchController.cs +++ b/Modbus.Net/Modbus.Net/MatchController.cs @@ -8,20 +8,37 @@ using Serilog; namespace Modbus.Net { + /// + /// 通讯号匹配模式的控制器 + /// public class MatchController : BaseController { private MessageWaitingDef _currentSendingPos; + private bool _taskCancel = false; + + /// + /// 获取间隔 + /// public int AcquireTime { get; } + /// + /// 匹配字典 + /// protected ICollection[] KeyMatches { get; } + /// + /// 构造器 + /// + /// 匹配字典,每个Collection代表一个匹配集合,每一个匹配集合中的数字代表需要匹配的位置,最后计算出来的数字是所有位置数字按照集合排序后叠放在一起 + /// 获取间隔 public MatchController(ICollection[] keyMatches, int acquireTime) { KeyMatches = keyMatches; AcquireTime = acquireTime; } + /// protected override void SendingMessageControlInner() { try @@ -67,6 +84,13 @@ namespace Modbus.Net } + /// + public override void SendStop() + { + _taskCancel = false; + } + + /// protected override string GetKeyFromMessage(byte[] message) { string ans = ""; @@ -82,6 +106,7 @@ namespace Modbus.Net return ans; } + /// protected override MessageWaitingDef GetMessageFromWaitingList(byte[] receiveMessage) { var returnKey = GetKeyFromMessage(receiveMessage); diff --git a/Modbus.Net/Modbus.Net/SerialPortHelper.cs b/Modbus.Net/Modbus.Net/SerialPortHelper.cs index 52938c9..afc58e4 100644 --- a/Modbus.Net/Modbus.Net/SerialPortHelper.cs +++ b/Modbus.Net/Modbus.Net/SerialPortHelper.cs @@ -1,12 +1,11 @@ -using System; -using System.Collections.Generic; -using System.Globalization; -using System.Linq; +using System.Globalization; using System.Text; -using System.Threading.Tasks; namespace Modbus.Net { + /// + /// 串口辅助类 + /// public static class SerialPortHelper { /// diff --git a/Modbus.Net/Modbus.Net/TcpConnector.cs b/Modbus.Net/Modbus.Net/TcpConnector.cs index 147da72..b55234a 100644 --- a/Modbus.Net/Modbus.Net/TcpConnector.cs +++ b/Modbus.Net/Modbus.Net/TcpConnector.cs @@ -237,6 +237,10 @@ namespace Modbus.Net return messageSendingdef; } + /// + /// 发送信息,不进行返回确认 + /// + /// 发送的信息 protected override async Task SendMsgWithoutConfirm(byte[] message) { var datagram = message; @@ -261,11 +265,17 @@ namespace Modbus.Net } } + /// + /// 启动获取线程 + /// protected override void ReceiveMsgThreadStart() { _receiveThread = Task.Run(ReceiveMessage); } + /// + /// 停止获取线程 + /// protected override void ReceiveMsgThreadStop() { _taskCancel = true; @@ -317,7 +327,7 @@ namespace Modbus.Net /// /// 接收消息,并转换成字符串 /// - /// + /// 消息的长度 private byte[] CheckReplyDatagram(int len) { var replyMessage = new byte[len]; diff --git a/Samples/CrossLamp/Controllers/HomeController.cs b/Samples/CrossLamp/Controllers/HomeController.cs index c79501d..4355337 100644 --- a/Samples/CrossLamp/Controllers/HomeController.cs +++ b/Samples/CrossLamp/Controllers/HomeController.cs @@ -58,7 +58,7 @@ namespace CrossLamp.Controllers light.SetStart(lamps[6]); return Json(light, JsonRequestBehavior.AllowGet); } - catch (Exception e) + catch (Exception) { return null; }