2017-12-28 Update 1 Add UdpConnector
This commit is contained in:
@@ -5,6 +5,14 @@ using System.Text;
|
||||
|
||||
namespace Modbus.Net.Modbus
|
||||
{
|
||||
/// <summary>
|
||||
/// Udp字节伸缩
|
||||
/// </summary>
|
||||
public class ModbusUdpProtocolLinkerBytesExtend : ModbusTcpProtocolLinkerBytesExtend
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Rtu透传字节伸缩
|
||||
/// </summary>
|
||||
|
||||
45
Modbus.Net/Modbus.Net.Modbus/ModbusUdpProtocol.cs
Normal file
45
Modbus.Net/Modbus.Net.Modbus/ModbusUdpProtocol.cs
Normal file
@@ -0,0 +1,45 @@
|
||||
using System.Configuration;
|
||||
|
||||
namespace Modbus.Net.Modbus
|
||||
{
|
||||
/// <summary>
|
||||
/// Modbus/Udp协议
|
||||
/// </summary>
|
||||
public class ModbusUdpProtocol : ModbusProtocol
|
||||
{
|
||||
/// <summary>
|
||||
/// 构造函数
|
||||
/// </summary>
|
||||
/// <param name="slaveAddress">从站号</param>
|
||||
/// <param name="masterAddress">主站号</param>
|
||||
public ModbusUdpProtocol(byte slaveAddress, byte masterAddress)
|
||||
: this(ConfigurationManager.AppSettings["IP"], slaveAddress, masterAddress)
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 构造函数
|
||||
/// </summary>
|
||||
/// <param name="ip">ip地址</param>
|
||||
/// <param name="slaveAddress">从站号</param>
|
||||
/// <param name="masterAddress">主站号</param>
|
||||
public ModbusUdpProtocol(string ip, byte slaveAddress, byte masterAddress)
|
||||
: base(slaveAddress, masterAddress)
|
||||
{
|
||||
ProtocolLinker = new ModbusUdpProtocolLinker(ip);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 构造函数
|
||||
/// </summary>
|
||||
/// <param name="ip">ip地址</param>
|
||||
/// <param name="port">端口</param>
|
||||
/// <param name="slaveAddress">从站号</param>
|
||||
/// <param name="masterAddress">主站号</param>
|
||||
public ModbusUdpProtocol(string ip, int port, byte slaveAddress, byte masterAddress)
|
||||
: base(slaveAddress, masterAddress)
|
||||
{
|
||||
ProtocolLinker = new ModbusUdpProtocolLinker(ip, port);
|
||||
}
|
||||
}
|
||||
}
|
||||
47
Modbus.Net/Modbus.Net.Modbus/ModbusUdpProtocolLinker.cs
Normal file
47
Modbus.Net/Modbus.Net.Modbus/ModbusUdpProtocolLinker.cs
Normal file
@@ -0,0 +1,47 @@
|
||||
using System.Configuration;
|
||||
|
||||
namespace Modbus.Net.Modbus
|
||||
{
|
||||
/// <summary>
|
||||
/// Modbus/Udp协议连接器
|
||||
/// </summary>
|
||||
public class ModbusUdpProtocolLinker : UdpProtocolLinker
|
||||
{
|
||||
/// <summary>
|
||||
/// 构造函数
|
||||
/// </summary>
|
||||
/// <param name="ip">IP地址</param>
|
||||
public ModbusUdpProtocolLinker(string ip)
|
||||
: this(ip, int.Parse(ConfigurationManager.AppSettings["ModbusPort"] ?? "502"))
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 构造函数
|
||||
/// </summary>
|
||||
/// <param name="ip">IP地址</param>
|
||||
/// <param name="port">端口</param>
|
||||
public ModbusUdpProtocolLinker(string ip, int port) : base(ip, port)
|
||||
{
|
||||
((BaseConnector)BaseConnector).AddController(new FifoController(0));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 校验返回数据
|
||||
/// </summary>
|
||||
/// <param name="content">设备返回的数据</param>
|
||||
/// <returns>数据是否正确</returns>
|
||||
public override bool? CheckRight(byte[] content)
|
||||
{
|
||||
//ProtocolLinker的CheckRight不会返回null
|
||||
if (base.CheckRight(content) != true) return false;
|
||||
//长度校验失败
|
||||
if (content[5] != content.Length - 6)
|
||||
throw new ModbusProtocolErrorException(500);
|
||||
//Modbus协议错误
|
||||
if (content[7] > 127)
|
||||
throw new ModbusProtocolErrorException(content[2] > 0 ? content[2] : content[8]);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -33,6 +33,10 @@ namespace Modbus.Net.Modbus
|
||||
/// Ascii连接Tcp透传
|
||||
/// </summary>
|
||||
AsciiInTcp = 4,
|
||||
/// <summary>
|
||||
/// Udp连接
|
||||
/// </summary>
|
||||
Udp = 5
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -193,6 +197,17 @@ namespace Modbus.Net.Modbus
|
||||
MasterAddress));
|
||||
break;
|
||||
}
|
||||
//Udp协议
|
||||
case ModbusType.Udp:
|
||||
{
|
||||
Wrapper = ConnectionString == null
|
||||
? new ModbusUdpProtocol(SlaveAddress, MasterAddress)
|
||||
: (ConnectionStringPort == null
|
||||
? new ModbusUdpProtocol(ConnectionString, SlaveAddress, MasterAddress)
|
||||
: new ModbusUdpProtocol(ConnectionStringIp, ConnectionStringPort.Value, SlaveAddress,
|
||||
MasterAddress));
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user