diff --git a/Modbus.Net/Modbus.Net.Modbus/ModbusAsciiInTcpProtocol.cs b/Modbus.Net/Modbus.Net.Modbus/ModbusAsciiInTcpProtocol.cs index e1add41..f57ca0c 100644 --- a/Modbus.Net/Modbus.Net.Modbus/ModbusAsciiInTcpProtocol.cs +++ b/Modbus.Net/Modbus.Net.Modbus/ModbusAsciiInTcpProtocol.cs @@ -26,7 +26,7 @@ namespace Modbus.Net.Modbus public ModbusAsciiInTcpProtocol(string ip, byte slaveAddress, byte masterAddress) : base(slaveAddress, masterAddress) { - ProtocolLinker = new ModbusAsciiInTcpProtocolLinker(ip, slaveAddress); + ProtocolLinker = new ModbusAsciiInTcpProtocolLinker(ip); } /// @@ -39,7 +39,7 @@ namespace Modbus.Net.Modbus public ModbusAsciiInTcpProtocol(string ip, int port, byte slaveAddress, byte masterAddress) : base(slaveAddress, masterAddress) { - ProtocolLinker = new ModbusTcpProtocolLinker(ip, port); + ProtocolLinker = new ModbusAsciiInTcpProtocolLinker(ip, port); } } } \ No newline at end of file diff --git a/Modbus.Net/Modbus.Net.Modbus/ModbusAsciiInUdpProtocol.cs b/Modbus.Net/Modbus.Net.Modbus/ModbusAsciiInUdpProtocol.cs new file mode 100644 index 0000000..b102f7a --- /dev/null +++ b/Modbus.Net/Modbus.Net.Modbus/ModbusAsciiInUdpProtocol.cs @@ -0,0 +1,47 @@ +using System; +using System.Collections.Generic; +using System.Configuration; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Modbus.Net.Modbus +{ + public class ModbusAsciiInUdpProtocol : ModbusProtocol + { + /// + /// 构造函数 + /// + /// 从站号 + /// 主站号 + public ModbusAsciiInUdpProtocol(byte slaveAddress, byte masterAddress) + : this(ConfigurationManager.AppSettings["IP"], slaveAddress, masterAddress) + { + } + + /// + /// 构造函数 + /// + /// ip地址 + /// 从站号 + /// 主站号 + public ModbusAsciiInUdpProtocol(string ip, byte slaveAddress, byte masterAddress) + : base(slaveAddress, masterAddress) + { + ProtocolLinker = new ModbusAsciiInUdpProtocolLinker(ip); + } + + /// + /// 构造函数 + /// + /// ip地址 + /// 端口 + /// 从站号 + /// 主站号 + public ModbusAsciiInUdpProtocol(string ip, int port, byte slaveAddress, byte masterAddress) + : base(slaveAddress, masterAddress) + { + ProtocolLinker = new ModbusAsciiInUdpProtocolLinker(ip, port); + } + } +} diff --git a/Modbus.Net/Modbus.Net.Modbus/ModbusAsciiInUdpProtocolLinker.cs b/Modbus.Net/Modbus.Net.Modbus/ModbusAsciiInUdpProtocolLinker.cs new file mode 100644 index 0000000..70b19d6 --- /dev/null +++ b/Modbus.Net/Modbus.Net.Modbus/ModbusAsciiInUdpProtocolLinker.cs @@ -0,0 +1,51 @@ +using System; +using System.Collections.Generic; +using System.Configuration; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Modbus.Net.Modbus +{ + public class ModbusAsciiInUdpProtocolLinker : UdpProtocolLinker + { + /// + /// 构造函数 + /// + /// IP地址 + public ModbusAsciiInUdpProtocolLinker(string ip) + : base(ip, int.Parse(ConfigurationManager.AppSettings["ModbusPort"] ?? "502")) + { + } + + /// + /// 构造函数 + /// + /// ip地址 + /// 端口号 + public ModbusAsciiInUdpProtocolLinker(string ip, int port) + : base(ip, port) + { + ((BaseConnector)BaseConnector).AddController(new FifoController(0)); + } + + /// + /// 校验返回数据是否正确 + /// + /// 返回的数据 + /// 校验是否正确 + public override bool? CheckRight(byte[] content) + { + //ProtocolLinker不会返回null + if (base.CheckRight(content) != true) return false; + //CRC校验失败 + var contentString = Encoding.ASCII.GetString(content); + if (!Crc16.GetInstance().LrcEfficacy(contentString)) + throw new ModbusProtocolErrorException(501); + //Modbus协议错误 + if (byte.Parse(contentString.Substring(3, 2)) > 127) + throw new ModbusProtocolErrorException(byte.Parse(contentString.Substring(5, 2))); + return true; + } + } +} diff --git a/Modbus.Net/Modbus.Net.Modbus/ModbusProtocolLinkerBytesExtend.cs b/Modbus.Net/Modbus.Net.Modbus/ModbusProtocolLinkerBytesExtend.cs index 6be2851..b9db51b 100644 --- a/Modbus.Net/Modbus.Net.Modbus/ModbusProtocolLinkerBytesExtend.cs +++ b/Modbus.Net/Modbus.Net.Modbus/ModbusProtocolLinkerBytesExtend.cs @@ -5,6 +5,22 @@ using System.Text; namespace Modbus.Net.Modbus { + /// + /// Udp字节伸缩 + /// + public class ModbusRtuInUdpProtocolLinkerBytesExtend : ModbusRtuProtocolLinkerBytesExtend + { + + } + + /// + /// Udp字节伸缩 + /// + public class ModbusAsciiInUdpProtocolLinkerBytesExtend : ModbusAsciiProtocolLinkerBytesExtend + { + + } + /// /// Udp字节伸缩 /// diff --git a/Modbus.Net/Modbus.Net.Modbus/ModbusRtuInTcpProtocol.cs b/Modbus.Net/Modbus.Net.Modbus/ModbusRtuInTcpProtocol.cs index 298fb91..a592cc8 100644 --- a/Modbus.Net/Modbus.Net.Modbus/ModbusRtuInTcpProtocol.cs +++ b/Modbus.Net/Modbus.Net.Modbus/ModbusRtuInTcpProtocol.cs @@ -27,7 +27,7 @@ namespace Modbus.Net.Modbus public ModbusRtuInTcpProtocol(string ip, byte slaveAddress, byte masterAddress) : base(slaveAddress, masterAddress) { - ProtocolLinker = new ModbusTcpProtocolLinker(ip); + ProtocolLinker = new ModbusRtuInTcpProtocolLinker(ip); } /// diff --git a/Modbus.Net/Modbus.Net.Modbus/ModbusRtuInUdpProtocol.cs b/Modbus.Net/Modbus.Net.Modbus/ModbusRtuInUdpProtocol.cs new file mode 100644 index 0000000..9ae03db --- /dev/null +++ b/Modbus.Net/Modbus.Net.Modbus/ModbusRtuInUdpProtocol.cs @@ -0,0 +1,47 @@ +using System; +using System.Collections.Generic; +using System.Configuration; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Modbus.Net.Modbus +{ + public class ModbusRtuInUdpProtocol : ModbusProtocol + { + /// + /// 构造函数 + /// + /// 从站号 + /// 主站号 + public ModbusRtuInUdpProtocol(byte slaveAddress, byte masterAddress) + : this(ConfigurationManager.AppSettings["IP"], slaveAddress, masterAddress) + { + } + + /// + /// 构造函数 + /// + /// ip地址 + /// 从站号 + /// 主站号 + public ModbusRtuInUdpProtocol(string ip, byte slaveAddress, byte masterAddress) + : base(slaveAddress, masterAddress) + { + ProtocolLinker = new ModbusRtuInUdpProtocolLinker(ip); + } + + /// + /// 构造函数 + /// + /// ip地址 + /// 端口号 + /// 从站号 + /// 主站号 + public ModbusRtuInUdpProtocol(string ip, int port, byte slaveAddress, byte masterAddress) + : base(slaveAddress, masterAddress) + { + ProtocolLinker = new ModbusRtuInUdpProtocolLinker(ip, port); + } + } +} diff --git a/Modbus.Net/Modbus.Net.Modbus/ModbusRtuInUdpProtocolLinker.cs b/Modbus.Net/Modbus.Net.Modbus/ModbusRtuInUdpProtocolLinker.cs new file mode 100644 index 0000000..55b937d --- /dev/null +++ b/Modbus.Net/Modbus.Net.Modbus/ModbusRtuInUdpProtocolLinker.cs @@ -0,0 +1,51 @@ +using System; +using System.Collections.Generic; +using System.Configuration; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Modbus.Net.Modbus +{ + public class ModbusRtuInUdpProtocolLinker : UdpProtocolLinker + { + /// + /// 构造函数 + /// + /// IP地址 + public ModbusRtuInUdpProtocolLinker(string ip) + : base(ip, int.Parse(ConfigurationManager.AppSettings["ModbusPort"] ?? "502")) + { + } + + /// + /// 构造函数 + /// + /// IP地址 + /// 端口号 + public ModbusRtuInUdpProtocolLinker(string ip, int port) + : base(ip, port) + { + ((BaseConnector)BaseConnector).AddController(new FifoController(0)); + } + + /// + /// 校验返回数据 + /// + /// 设备返回的数据 + /// 数据是否正确 + public override bool? CheckRight(byte[] content) + { + //ProtocolLinker的CheckRight不会返回null + if (base.CheckRight(content) != true) return false; + //CRC校验失败 + if (!Crc16.GetInstance().CrcEfficacy(content)) + throw new ModbusProtocolErrorException(501); + //Modbus协议错误 + if (content[1] > 127) + throw new ModbusProtocolErrorException(content[2]); + return true; + } + } +} +} diff --git a/Modbus.Net/Modbus.Net.Modbus/ModbusUtility.cs b/Modbus.Net/Modbus.Net.Modbus/ModbusUtility.cs index d4405db..82da0b2 100644 --- a/Modbus.Net/Modbus.Net.Modbus/ModbusUtility.cs +++ b/Modbus.Net/Modbus.Net.Modbus/ModbusUtility.cs @@ -33,10 +33,21 @@ namespace Modbus.Net.Modbus /// Ascii连接Tcp透传 /// AsciiInTcp = 4, + /// /// Udp连接 /// - Udp = 5 + Udp = 5, + + /// + /// Rtu连接Udp透传 + /// + RtuInUdp = 6, + + /// + /// Ascii连接Udp透传 + /// + AsciiInUdp = 7 } /// @@ -175,7 +186,7 @@ namespace Modbus.Net.Modbus : new ModbusAsciiProtocol(ConnectionString, SlaveAddress, MasterAddress); break; } - //Rtu协议 + //Rtu协议Tcp透传 case ModbusType.RtuInTcp: { Wrapper = ConnectionString == null @@ -186,7 +197,7 @@ namespace Modbus.Net.Modbus MasterAddress)); break; } - //Ascii协议 + //Ascii协议Tcp透传 case ModbusType.AsciiInTcp: { Wrapper = ConnectionString == null @@ -197,7 +208,7 @@ namespace Modbus.Net.Modbus MasterAddress)); break; } - //Udp协议 + //Tcp协议Udp透传 case ModbusType.Udp: { Wrapper = ConnectionString == null @@ -208,6 +219,28 @@ namespace Modbus.Net.Modbus MasterAddress)); break; } + //Rtu协议Udp透传 + case ModbusType.RtuInUdp: + { + Wrapper = ConnectionString == null + ? new ModbusRtuInUdpProtocol(SlaveAddress, MasterAddress) + : (ConnectionStringPort == null + ? new ModbusRtuInUdpProtocol(ConnectionString, SlaveAddress, MasterAddress) + : new ModbusRtuInUdpProtocol(ConnectionStringIp, ConnectionStringPort.Value, SlaveAddress, + MasterAddress)); + break; + } + //Rtu协议Udp透传 + case ModbusType.AsciiInUdp: + { + Wrapper = ConnectionString == null + ? new ModbusAsciiInUdpProtocol(SlaveAddress, MasterAddress) + : (ConnectionStringPort == null + ? new ModbusAsciiInUdpProtocol(ConnectionString, SlaveAddress, MasterAddress) + : new ModbusAsciiInUdpProtocol(ConnectionStringIp, ConnectionStringPort.Value, SlaveAddress, + MasterAddress)); + break; + } } } } diff --git a/README.md b/README.md index ca3ee99..523fb96 100644 --- a/README.md +++ b/README.md @@ -109,10 +109,9 @@ RoadMap * New UdpConnector (Complete) ### Version 1.4.2 -* Serial Port Connection with Multiple Master Station (In Road) -* PPI Remake (In Road) -* Siemens MPI Support (In Road) -* Siemens MultiStation PPI Support (In Road) +* PPI Remake (Coding) +* Modbus Udp (Coding) +* Full Async/Sync SerialPort and Ethenet Communication (Coding) ### Version 1.4.3 * Machine and Utility Builder (In Road) @@ -121,10 +120,13 @@ RoadMap * Unite TransServer.Net into Modbus.Net (In Road) * Passive Connector and Controller (In Road) * Interfaces Architecture (In Road) -* Dependency Injection (In Road) * ValueHelper remake to interface, users can add their own value translate function (In Road) * New Zoom (In Road) * New TaskManager (In Road) +* Mutiple Address fields in Machine (In Road) +* Serial Port Connection with Multiple Master Station (In Road) +* Siemens MPI Support (In Road) +* Siemens MultiStation PPI Support (In Road) ### Version 1.6.X * English comment (In Road) @@ -134,4 +136,4 @@ RoadMap * Rename to Transport.Net (In Road) * Puzzle Builder (In Road) * Protocol Builer (In Road) - +* Dependency Injection (In Road)