crc and lrc check move to controller
This commit is contained in:
@@ -24,7 +24,21 @@ namespace Modbus.Net.Modbus
|
|||||||
public ModbusAsciiInTcpProtocolLinker(string ip, int port)
|
public ModbusAsciiInTcpProtocolLinker(string ip, int port)
|
||||||
: base(ip, port)
|
: base(ip, port)
|
||||||
{
|
{
|
||||||
((IConnectorWithController<byte[], byte[]>)BaseConnector).AddController(new FifoController(int.Parse(ConfigurationReader.GetValue("TCP:" + ip + ":" + port, "FetchSleepTime")), lengthCalc: content => { if (content[0] != 0x3a) return 0; for (int i = 1; i < content.Length; i++) { if (content[i - 1] == 0x0D && content[i] == 0x0A) return i + 1; } return -1; }, waitingListMaxCount: ConfigurationReader.GetValue("TCP:" + ip + ":" + port, "WaitingListCount") != null ? int.Parse(ConfigurationReader.GetValue("TCP:" + ip + ":" + port, "WaitingListCount")) : null));
|
((IConnectorWithController<byte[], byte[]>)BaseConnector).AddController(new FifoController(int.Parse(ConfigurationReader.GetValue("TCP:" + ip + ":" + port, "FetchSleepTime")),
|
||||||
|
lengthCalc: content =>
|
||||||
|
{
|
||||||
|
if (content[0] != 0x3a) return 0;
|
||||||
|
for (int i = 1; i < content.Length; i++)
|
||||||
|
{
|
||||||
|
if (content[i - 1] == 0x0D && content[i] == 0x0A) return i + 1;
|
||||||
|
}
|
||||||
|
return -1;
|
||||||
|
},
|
||||||
|
checkRightFunc: ContentCheck.LrcCheckRight,
|
||||||
|
waitingListMaxCount: ConfigurationReader.GetValue("TCP:" + ip + ":" + port, "WaitingListCount") != null
|
||||||
|
? int.Parse(ConfigurationReader.GetValue("TCP:" + ip + ":" + port, "WaitingListCount"))
|
||||||
|
: null
|
||||||
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -36,11 +50,8 @@ namespace Modbus.Net.Modbus
|
|||||||
{
|
{
|
||||||
//ProtocolLinker不会返回null
|
//ProtocolLinker不会返回null
|
||||||
if (base.CheckRight(content) != true) return false;
|
if (base.CheckRight(content) != true) return false;
|
||||||
//CRC校验失败
|
|
||||||
var contentString = Encoding.ASCII.GetString(content);
|
|
||||||
if (!Crc16.GetInstance().LrcEfficacy(contentString))
|
|
||||||
throw new ModbusProtocolErrorException(501);
|
|
||||||
//Modbus协议错误
|
//Modbus协议错误
|
||||||
|
var contentString = Encoding.ASCII.GetString(content);
|
||||||
if (byte.Parse(contentString.Substring(3, 2)) > 127)
|
if (byte.Parse(contentString.Substring(3, 2)) > 127)
|
||||||
throw new ModbusProtocolErrorException(byte.Parse(contentString.Substring(5, 2)));
|
throw new ModbusProtocolErrorException(byte.Parse(contentString.Substring(5, 2)));
|
||||||
return true;
|
return true;
|
||||||
|
|||||||
@@ -24,7 +24,22 @@ namespace Modbus.Net.Modbus
|
|||||||
public ModbusAsciiInUdpProtocolLinker(string ip, int port)
|
public ModbusAsciiInUdpProtocolLinker(string ip, int port)
|
||||||
: base(ip, port)
|
: base(ip, port)
|
||||||
{
|
{
|
||||||
((IConnectorWithController<byte[], byte[]>)BaseConnector).AddController(new FifoController(int.Parse(ConfigurationReader.GetValue("UDP:" + ip + ":" + port, "FetchSleepTime")), lengthCalc: content => { if (content[0] != 0x3a) return 0; for (int i = 1; i < content.Length; i++) { if (content[i - 1] == 0x0D && content[i] == 0x0A) return i + 1; } return -1; }, waitingListMaxCount: ConfigurationReader.GetValue("UDP:" + ip + ":" + port, "WaitingListCount") != null ? int.Parse(ConfigurationReader.GetValue("UDP:" + ip + ":" + port, "WaitingListCount")) : null));
|
((IConnectorWithController<byte[], byte[]>)BaseConnector).AddController(new FifoController(int.Parse(ConfigurationReader.GetValue("UDP:" + ip + ":" + port, "FetchSleepTime")),
|
||||||
|
lengthCalc: content =>
|
||||||
|
{
|
||||||
|
if (content[0] != 0x3a) return 0;
|
||||||
|
for (int i = 1; i < content.Length; i++)
|
||||||
|
{
|
||||||
|
if (content[i - 1] == 0x0D && content[i] == 0x0A)
|
||||||
|
return i + 1;
|
||||||
|
}
|
||||||
|
return -1;
|
||||||
|
},
|
||||||
|
checkRightFunc: ContentCheck.LrcCheckRight,
|
||||||
|
waitingListMaxCount: ConfigurationReader.GetValue("UDP:" + ip + ":" + port, "WaitingListCount") != null
|
||||||
|
? int.Parse(ConfigurationReader.GetValue("UDP:" + ip + ":" + port, "WaitingListCount"))
|
||||||
|
: null
|
||||||
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -36,11 +51,8 @@ namespace Modbus.Net.Modbus
|
|||||||
{
|
{
|
||||||
//ProtocolLinker不会返回null
|
//ProtocolLinker不会返回null
|
||||||
if (base.CheckRight(content) != true) return false;
|
if (base.CheckRight(content) != true) return false;
|
||||||
//CRC校验失败
|
|
||||||
var contentString = Encoding.ASCII.GetString(content);
|
|
||||||
if (!Crc16.GetInstance().LrcEfficacy(contentString))
|
|
||||||
throw new ModbusProtocolErrorException(501);
|
|
||||||
//Modbus协议错误
|
//Modbus协议错误
|
||||||
|
var contentString = Encoding.ASCII.GetString(content);
|
||||||
if (byte.Parse(contentString.Substring(3, 2)) > 127)
|
if (byte.Parse(contentString.Substring(3, 2)) > 127)
|
||||||
throw new ModbusProtocolErrorException(byte.Parse(contentString.Substring(5, 2)));
|
throw new ModbusProtocolErrorException(byte.Parse(contentString.Substring(5, 2)));
|
||||||
return true;
|
return true;
|
||||||
|
|||||||
@@ -16,7 +16,24 @@ namespace Modbus.Net.Modbus
|
|||||||
public ModbusAsciiProtocolLinker(string com, int slaveAddress)
|
public ModbusAsciiProtocolLinker(string com, int slaveAddress)
|
||||||
: base(com, slaveAddress)
|
: base(com, slaveAddress)
|
||||||
{
|
{
|
||||||
((IConnectorWithController<byte[], byte[]>)BaseConnector).AddController(new MatchController(new ICollection<(int, int)>[] { new List<(int, int)> { (1, 1), (2, 2) }, new List<(int, int)> { (3, 3), (4, 4) } }, int.Parse(ConfigurationReader.GetValue("COM:" + com + ":" + slaveAddress, "FetchSleepTime")), lengthCalc: content => { if (content[0] != 0x3a) return 0; for (int i = 1; i < content.Length; i++) { if (content[i - 1] == 0x0D && content[i] == 0x0A) return i + 1; } return -1; }, waitingListMaxCount: ConfigurationReader.GetValue("COM:" + com + ":" + slaveAddress, "WaitingListCount") != null ? int.Parse(ConfigurationReader.GetValue("COM:" + com + ":" + slaveAddress, "WaitingListCount")) : null));
|
((IConnectorWithController<byte[], byte[]>)BaseConnector).AddController(new MatchController(
|
||||||
|
new ICollection<(int, int)>[] {
|
||||||
|
new List<(int, int)> { (1, 1), (2, 2) }, new List<(int, int)> { (3, 3), (4, 4) } },
|
||||||
|
int.Parse(ConfigurationReader.GetValue("COM:" + com + ":" + slaveAddress, "FetchSleepTime")),
|
||||||
|
lengthCalc: content =>
|
||||||
|
{
|
||||||
|
if (content[0] != 0x3a) return 0;
|
||||||
|
for (int i = 1; i < content.Length; i++)
|
||||||
|
{
|
||||||
|
if (content[i - 1] == 0x0D && content[i] == 0x0A) return i + 1;
|
||||||
|
}
|
||||||
|
return -1;
|
||||||
|
},
|
||||||
|
checkRightFunc: ContentCheck.LrcCheckRight,
|
||||||
|
waitingListMaxCount: ConfigurationReader.GetValue("COM:" + com + ":" + slaveAddress, "WaitingListCount") != null ?
|
||||||
|
int.Parse(ConfigurationReader.GetValue("COM:" + com + ":" + slaveAddress, "WaitingListCount")) :
|
||||||
|
null
|
||||||
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -28,11 +45,8 @@ namespace Modbus.Net.Modbus
|
|||||||
{
|
{
|
||||||
//ProtocolLinker不会返回null
|
//ProtocolLinker不会返回null
|
||||||
if (base.CheckRight(content) != true) return false;
|
if (base.CheckRight(content) != true) return false;
|
||||||
//CRC校验失败
|
|
||||||
var contentString = Encoding.ASCII.GetString(content);
|
|
||||||
if (!Crc16.GetInstance().LrcEfficacy(contentString))
|
|
||||||
throw new ModbusProtocolErrorException(501);
|
|
||||||
//Modbus协议错误
|
//Modbus协议错误
|
||||||
|
var contentString = Encoding.ASCII.GetString(content);
|
||||||
if (byte.Parse(contentString.Substring(3, 2)) > 127)
|
if (byte.Parse(contentString.Substring(3, 2)) > 127)
|
||||||
throw new ModbusProtocolErrorException(byte.Parse(contentString.Substring(5, 2)));
|
throw new ModbusProtocolErrorException(byte.Parse(contentString.Substring(5, 2)));
|
||||||
return true;
|
return true;
|
||||||
|
|||||||
@@ -559,9 +559,7 @@ namespace Modbus.Net.Modbus
|
|||||||
{6, "SLAVE_DEVICE_BUSY"},
|
{6, "SLAVE_DEVICE_BUSY"},
|
||||||
{8, "MEMORY_PARITY_ERROR" },
|
{8, "MEMORY_PARITY_ERROR" },
|
||||||
{10, "GATEWAY_PATH_UNAVAILABLE"},
|
{10, "GATEWAY_PATH_UNAVAILABLE"},
|
||||||
{11, "GATEWAY_TARGET_DEVICE_FAILED_TO_RESPOND"},
|
{11, "GATEWAY_TARGET_DEVICE_FAILED_TO_RESPOND"}
|
||||||
{500, "TCP_ILLEGAL_LENGTH"},
|
|
||||||
{501, "RTU_ILLEGAL_CRC"}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|||||||
@@ -24,7 +24,18 @@ namespace Modbus.Net.Modbus
|
|||||||
public ModbusRtuInTcpProtocolLinker(string ip, int port)
|
public ModbusRtuInTcpProtocolLinker(string ip, int port)
|
||||||
: base(ip, port)
|
: base(ip, port)
|
||||||
{
|
{
|
||||||
((IConnectorWithController<byte[], byte[]>)BaseConnector).AddController(new FifoController(int.Parse(ConfigurationReader.GetValue("TCP:" + ip + ":" + port, "FetchSleepTime")), lengthCalc: content => { if (content[1] == 5 || content[1] == 6 || content[1] == 15 || content[1] == 16 || content[1] == 21) return 8; else return DuplicateWithCount.GetDuplcateFunc(new List<int> { 2 }, 5).Invoke(content); }, waitingListMaxCount: ConfigurationReader.GetValue("TCP:" + ip + ":" + port, "WaitingListCount") != null ? int.Parse(ConfigurationReader.GetValue("TCP:" + ip + ":" + port, "WaitingListCount")) : null));
|
((IConnectorWithController<byte[], byte[]>)BaseConnector).AddController(new FifoController(
|
||||||
|
int.Parse(ConfigurationReader.GetValue("TCP:" + ip + ":" + port, "FetchSleepTime")),
|
||||||
|
lengthCalc: content =>
|
||||||
|
{
|
||||||
|
if (content[1] == 5 || content[1] == 6 || content[1] == 15 || content[1] == 16 || content[1] == 21) return 8;
|
||||||
|
else return DuplicateWithCount.GetDuplcateFunc(new List<int> { 2 }, 5).Invoke(content);
|
||||||
|
},
|
||||||
|
checkRightFunc: ContentCheck.Crc16CheckRight,
|
||||||
|
waitingListMaxCount: ConfigurationReader.GetValue("TCP:" + ip + ":" + port, "WaitingListCount") != null ?
|
||||||
|
int.Parse(ConfigurationReader.GetValue("TCP:" + ip + ":" + port, "WaitingListCount")) :
|
||||||
|
null
|
||||||
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -36,9 +47,6 @@ namespace Modbus.Net.Modbus
|
|||||||
{
|
{
|
||||||
//ProtocolLinker的CheckRight不会返回null
|
//ProtocolLinker的CheckRight不会返回null
|
||||||
if (base.CheckRight(content) != true) return false;
|
if (base.CheckRight(content) != true) return false;
|
||||||
//CRC校验失败
|
|
||||||
if (!Crc16.GetInstance().CrcEfficacy(content))
|
|
||||||
throw new ModbusProtocolErrorException(501);
|
|
||||||
//Modbus协议错误
|
//Modbus协议错误
|
||||||
if (content[1] > 127)
|
if (content[1] > 127)
|
||||||
throw new ModbusProtocolErrorException(content[2]);
|
throw new ModbusProtocolErrorException(content[2]);
|
||||||
|
|||||||
@@ -24,7 +24,18 @@ namespace Modbus.Net.Modbus
|
|||||||
public ModbusRtuInUdpProtocolLinker(string ip, int port)
|
public ModbusRtuInUdpProtocolLinker(string ip, int port)
|
||||||
: base(ip, port)
|
: base(ip, port)
|
||||||
{
|
{
|
||||||
((IConnectorWithController<byte[], byte[]>)BaseConnector).AddController(new FifoController(int.Parse(ConfigurationReader.GetValue("UDP:" + ip + ":" + port, "FetchSleepTime")), lengthCalc: content => { if (content[1] == 5 || content[1] == 6 || content[1] == 15 || content[1] == 16 || content[1] == 21) return 8; else return DuplicateWithCount.GetDuplcateFunc(new List<int> { 2 }, 5).Invoke(content); }, waitingListMaxCount: ConfigurationReader.GetValue("UDP:" + ip + ":" + port, "WaitingListCount") != null ? int.Parse(ConfigurationReader.GetValue("UDP:" + ip + ":" + port, "WaitingListCount")) : null));
|
((IConnectorWithController<byte[], byte[]>)BaseConnector).AddController(new FifoController(
|
||||||
|
int.Parse(ConfigurationReader.GetValue("UDP:" + ip + ":" + port, "FetchSleepTime")),
|
||||||
|
lengthCalc: content =>
|
||||||
|
{
|
||||||
|
if (content[1] == 5 || content[1] == 6 || content[1] == 15 || content[1] == 16 || content[1] == 21) return 8;
|
||||||
|
else return DuplicateWithCount.GetDuplcateFunc(new List<int> { 2 }, 5).Invoke(content);
|
||||||
|
},
|
||||||
|
checkRightFunc: ContentCheck.Crc16CheckRight,
|
||||||
|
waitingListMaxCount: ConfigurationReader.GetValue("UDP:" + ip + ":" + port, "WaitingListCount") != null ?
|
||||||
|
int.Parse(ConfigurationReader.GetValue("UDP:" + ip + ":" + port, "WaitingListCount")) :
|
||||||
|
null
|
||||||
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -36,9 +47,6 @@ namespace Modbus.Net.Modbus
|
|||||||
{
|
{
|
||||||
//ProtocolLinker的CheckRight不会返回null
|
//ProtocolLinker的CheckRight不会返回null
|
||||||
if (base.CheckRight(content) != true) return false;
|
if (base.CheckRight(content) != true) return false;
|
||||||
//CRC校验失败
|
|
||||||
if (!Crc16.GetInstance().CrcEfficacy(content))
|
|
||||||
throw new ModbusProtocolErrorException(501);
|
|
||||||
//Modbus协议错误
|
//Modbus协议错误
|
||||||
if (content[1] > 127)
|
if (content[1] > 127)
|
||||||
throw new ModbusProtocolErrorException(content[2]);
|
throw new ModbusProtocolErrorException(content[2]);
|
||||||
|
|||||||
@@ -15,7 +15,19 @@ namespace Modbus.Net.Modbus
|
|||||||
public ModbusRtuProtocolLinker(string com, int slaveAddress)
|
public ModbusRtuProtocolLinker(string com, int slaveAddress)
|
||||||
: base(com, slaveAddress)
|
: base(com, slaveAddress)
|
||||||
{
|
{
|
||||||
((IConnectorWithController<byte[], byte[]>)BaseConnector).AddController(new MatchController(new ICollection<(int, int)>[] { new List<(int, int)> { (0, 0) }, new List<(int, int)> { (1, 1) } }, int.Parse(ConfigurationReader.GetValue("COM:" + com + ":" + slaveAddress, "FetchSleepTime")), lengthCalc: content => { if (content[1] == 5 || content[1] == 6 || content[1] == 15 || content[1] == 16 || content[1] == 21) return 8; else return DuplicateWithCount.GetDuplcateFunc(new List<int> { 2 }, 5).Invoke(content); }, waitingListMaxCount: ConfigurationReader.GetValue("COM:" + com + ":" + slaveAddress, "WaitingListCount") != null ? int.Parse(ConfigurationReader.GetValue("COM:" + com + ":" + slaveAddress, "WaitingListCount")) : null));
|
((IConnectorWithController<byte[], byte[]>)BaseConnector).AddController(new MatchController(
|
||||||
|
new ICollection<(int, int)>[] { new List<(int, int)> { (0, 0) }, new List<(int, int)> { (1, 1) } },
|
||||||
|
int.Parse(ConfigurationReader.GetValue("COM:" + com + ":" + slaveAddress, "FetchSleepTime")),
|
||||||
|
lengthCalc: content =>
|
||||||
|
{
|
||||||
|
if (content[1] == 5 || content[1] == 6 || content[1] == 15 || content[1] == 16 || content[1] == 21) return 8;
|
||||||
|
else return DuplicateWithCount.GetDuplcateFunc(new List<int> { 2 }, 5).Invoke(content);
|
||||||
|
},
|
||||||
|
checkRightFunc: ContentCheck.Crc16CheckRight,
|
||||||
|
waitingListMaxCount: ConfigurationReader.GetValue("COM:" + com + ":" + slaveAddress, "WaitingListCount") != null ?
|
||||||
|
int.Parse(ConfigurationReader.GetValue("COM:" + com + ":" + slaveAddress, "WaitingListCount")) :
|
||||||
|
null
|
||||||
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|||||||
@@ -11,8 +11,8 @@ namespace Modbus.Net
|
|||||||
{
|
{
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public ModbusTcpMatchDirectlySendController(ICollection<(int, int)>[] keyMatches,
|
public ModbusTcpMatchDirectlySendController(ICollection<(int, int)>[] keyMatches,
|
||||||
Func<byte[], int> lengthCalc = null, int? waitingListMaxCount = null) : base(keyMatches,
|
Func<byte[], int> lengthCalc = null, Func<byte[], bool?> checkRightFunc = null, int? waitingListMaxCount = null) : base(keyMatches,
|
||||||
lengthCalc, waitingListMaxCount)
|
lengthCalc, checkRightFunc, waitingListMaxCount)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -23,7 +23,13 @@ namespace Modbus.Net.Modbus
|
|||||||
/// <param name="port">端口</param>
|
/// <param name="port">端口</param>
|
||||||
public ModbusTcpProtocolLinker(string ip, int port) : base(ip, port)
|
public ModbusTcpProtocolLinker(string ip, int port) : base(ip, port)
|
||||||
{
|
{
|
||||||
((IConnectorWithController<byte[], byte[]>)BaseConnector).AddController(new ModbusTcpMatchDirectlySendController(new ICollection<(int, int)>[] { new List<(int, int)> { (0, 0), (1, 1) } }, lengthCalc: DuplicateWithCount.GetDuplcateFunc(new List<int> { 4, 5 }, 6), waitingListMaxCount: ConfigurationReader.GetValue("TCP:" + ip + ":" + port, "WaitingListCount") != null ? int.Parse(ConfigurationReader.GetValue("TCP:" + ip + ":" + port, "WaitingListCount")) : null));
|
((IConnectorWithController<byte[], byte[]>)BaseConnector).AddController(new ModbusTcpMatchDirectlySendController(
|
||||||
|
new ICollection<(int, int)>[] { new List<(int, int)> { (0, 0), (1, 1) } },
|
||||||
|
lengthCalc: DuplicateWithCount.GetDuplcateFunc(new List<int> { 4, 5 }, 6),
|
||||||
|
waitingListMaxCount: ConfigurationReader.GetValue("TCP:" + ip + ":" + port, "WaitingListCount") != null ?
|
||||||
|
int.Parse(ConfigurationReader.GetValue("TCP:" + ip + ":" + port, "WaitingListCount")) :
|
||||||
|
null
|
||||||
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -35,9 +41,6 @@ namespace Modbus.Net.Modbus
|
|||||||
{
|
{
|
||||||
//ProtocolLinker的CheckRight不会返回null
|
//ProtocolLinker的CheckRight不会返回null
|
||||||
if (base.CheckRight(content) != true) return false;
|
if (base.CheckRight(content) != true) return false;
|
||||||
//长度校验失败
|
|
||||||
if (content[5] != content.Length - 6)
|
|
||||||
throw new ModbusProtocolErrorException(500);
|
|
||||||
//Modbus协议错误
|
//Modbus协议错误
|
||||||
if (content[7] > 127)
|
if (content[7] > 127)
|
||||||
throw new ModbusProtocolErrorException(content[2] > 0 ? content[2] : content[8]);
|
throw new ModbusProtocolErrorException(content[2] > 0 ? content[2] : content[8]);
|
||||||
|
|||||||
@@ -23,7 +23,13 @@ namespace Modbus.Net.Modbus
|
|||||||
/// <param name="port">端口</param>
|
/// <param name="port">端口</param>
|
||||||
public ModbusUdpProtocolLinker(string ip, int port) : base(ip, port)
|
public ModbusUdpProtocolLinker(string ip, int port) : base(ip, port)
|
||||||
{
|
{
|
||||||
((IConnectorWithController<byte[], byte[]>)BaseConnector).AddController(new FifoController(int.Parse(ConfigurationReader.GetValue("UDP:" + ip + ":" + port, "FetchSleepTime")), lengthCalc: DuplicateWithCount.GetDuplcateFunc(new List<int> { 4, 5 }, 6), waitingListMaxCount: ConfigurationReader.GetValue("UDP:" + ip + ":" + port, "WaitingListCount") != null ? int.Parse(ConfigurationReader.GetValue("UDP:" + ip + ":" + port, "WaitingListCount")) : null));
|
((IConnectorWithController<byte[], byte[]>)BaseConnector).AddController(new FifoController(
|
||||||
|
int.Parse(ConfigurationReader.GetValue("UDP:" + ip + ":" + port, "FetchSleepTime")),
|
||||||
|
lengthCalc: DuplicateWithCount.GetDuplcateFunc(new List<int> { 4, 5 }, 6),
|
||||||
|
waitingListMaxCount: ConfigurationReader.GetValue("UDP:" + ip + ":" + port, "WaitingListCount") != null ?
|
||||||
|
int.Parse(ConfigurationReader.GetValue("UDP:" + ip + ":" + port, "WaitingListCount")) :
|
||||||
|
null
|
||||||
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -35,9 +41,6 @@ namespace Modbus.Net.Modbus
|
|||||||
{
|
{
|
||||||
//ProtocolLinker的CheckRight不会返回null
|
//ProtocolLinker的CheckRight不会返回null
|
||||||
if (base.CheckRight(content) != true) return false;
|
if (base.CheckRight(content) != true) return false;
|
||||||
//长度校验失败
|
|
||||||
if (content[5] != content.Length - 6)
|
|
||||||
throw new ModbusProtocolErrorException(500);
|
|
||||||
//Modbus协议错误
|
//Modbus协议错误
|
||||||
if (content[7] > 127)
|
if (content[7] > 127)
|
||||||
throw new ModbusProtocolErrorException(content[2] > 0 ? content[2] : content[8]);
|
throw new ModbusProtocolErrorException(content[2] > 0 ? content[2] : content[8]);
|
||||||
|
|||||||
@@ -18,7 +18,14 @@ namespace Modbus.Net.Siemens
|
|||||||
public SiemensPpiProtocolLinker(string com, int slaveAddress)
|
public SiemensPpiProtocolLinker(string com, int slaveAddress)
|
||||||
: base(com, slaveAddress)
|
: base(com, slaveAddress)
|
||||||
{
|
{
|
||||||
((IConnectorWithController<byte[], byte[]>)BaseConnector).AddController(new FifoController(int.Parse(ConfigurationReader.GetValue("COM:" + com + ":" + slaveAddress, "FetchSleepTime")), lengthCalc: DuplicateWithCount.GetDuplcateFunc(new List<int> { 1 }, 6), waitingListMaxCount: ConfigurationReader.GetValue("COM:" + com + ":" + slaveAddress, "WaitingListCount") != null ? int.Parse(ConfigurationReader.GetValue("COM:" + com + ":" + slaveAddress, "WaitingListCount")) : null));
|
((IConnectorWithController<byte[], byte[]>)BaseConnector).AddController(new FifoController(
|
||||||
|
int.Parse(ConfigurationReader.GetValue("COM:" + com + ":" + slaveAddress, "FetchSleepTime")),
|
||||||
|
lengthCalc: DuplicateWithCount.GetDuplcateFunc(new List<int> { 1 }, 6),
|
||||||
|
checkRightFunc: ContentCheck.FcsCheckRight,
|
||||||
|
waitingListMaxCount: ConfigurationReader.GetValue("COM:" + com + ":" + slaveAddress, "WaitingListCount") != null ?
|
||||||
|
int.Parse(ConfigurationReader.GetValue("COM:" + com + ":" + slaveAddress, "WaitingListCount")) :
|
||||||
|
null
|
||||||
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -86,14 +93,9 @@ namespace Modbus.Net.Siemens
|
|||||||
public override bool? CheckRight(byte[] content)
|
public override bool? CheckRight(byte[] content)
|
||||||
{
|
{
|
||||||
if (base.CheckRight(content) != true) return false;
|
if (base.CheckRight(content) != true) return false;
|
||||||
var fcsCheck = 0;
|
|
||||||
if (content.Length == 1 && content[0] == 0xe5)
|
if (content.Length == 1 && content[0] == 0xe5)
|
||||||
return true;
|
return true;
|
||||||
if (content.Length == 6 && content[3] == 0) return true;
|
if (content.Length == 6 && content[3] == 0) return true;
|
||||||
for (var i = 4; i < content.Length - 2; i++)
|
|
||||||
fcsCheck += content[i];
|
|
||||||
fcsCheck = fcsCheck % 256;
|
|
||||||
if (fcsCheck != content[content.Length - 2]) return false;
|
|
||||||
if (content[content.Length - 1] != 0x16) return false;
|
if (content[content.Length - 1] != 0x16) return false;
|
||||||
if (content[1] != content.Length - 6) return false;
|
if (content[1] != content.Length - 6) return false;
|
||||||
return true;
|
return true;
|
||||||
|
|||||||
@@ -25,7 +25,13 @@ namespace Modbus.Net.Siemens
|
|||||||
public SiemensTcpProtocolLinker(string ip, int port)
|
public SiemensTcpProtocolLinker(string ip, int port)
|
||||||
: base(ip, port)
|
: base(ip, port)
|
||||||
{
|
{
|
||||||
((IConnectorWithController<byte[], byte[]>)BaseConnector).AddController(new MatchDirectlySendController(new ICollection<(int, int)>[] { new List<(int, int)> { (11, 11), (12, 12) } }, DuplicateWithCount.GetDuplcateFunc(new List<int> { 2, 3 }, 0), waitingListMaxCount: ConfigurationReader.GetValue("TCP:" + ip + ":" + port, "WaitingListCount") != null ? int.Parse(ConfigurationReader.GetValue("TCP:" + ip + ":" + port, "WaitingListCount")) : null));
|
((IConnectorWithController<byte[], byte[]>)BaseConnector).AddController(new MatchDirectlySendController(
|
||||||
|
new ICollection<(int, int)>[] { new List<(int, int)> { (11, 11), (12, 12) } },
|
||||||
|
lengthCalc: DuplicateWithCount.GetDuplcateFunc(new List<int> { 2, 3 }, 0),
|
||||||
|
waitingListMaxCount: ConfigurationReader.GetValue("TCP:" + ip + ":" + port, "WaitingListCount") != null ?
|
||||||
|
int.Parse(ConfigurationReader.GetValue("TCP:" + ip + ":" + port, "WaitingListCount")) :
|
||||||
|
null
|
||||||
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|||||||
@@ -22,18 +22,25 @@ namespace Modbus.Net
|
|||||||
protected Task SendingThread { get; set; }
|
protected Task SendingThread { get; set; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 包切分位置
|
/// 包切分位置函数
|
||||||
/// </summary>
|
/// </summary>
|
||||||
protected Func<byte[], int> LengthCalc { get; }
|
protected Func<byte[], int> LengthCalc { get; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// 包校验函数
|
||||||
|
/// </summary>
|
||||||
|
protected Func<byte[], bool?> CheckRightFunc { get; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 构造器
|
/// 构造器
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="lengthCalc">包长度计算函数</param>
|
/// <param name="lengthCalc">包长度计算函数</param>
|
||||||
protected BaseController(Func<byte[], int> lengthCalc = null)
|
/// <param name="checkRightFunc">包校验函数</param>
|
||||||
|
protected BaseController(Func<byte[], int> lengthCalc = null, Func<byte[], bool?> checkRightFunc = null)
|
||||||
{
|
{
|
||||||
WaitingMessages = new List<MessageWaitingDef>();
|
WaitingMessages = new List<MessageWaitingDef>();
|
||||||
LengthCalc = lengthCalc;
|
LengthCalc = lengthCalc;
|
||||||
|
CheckRightFunc = checkRightFunc;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
@@ -118,7 +125,13 @@ namespace Modbus.Net
|
|||||||
duplicatedMessages = new List<byte[]>();
|
duplicatedMessages = new List<byte[]>();
|
||||||
while (receiveMessageCopy.Length >= length)
|
while (receiveMessageCopy.Length >= length)
|
||||||
{
|
{
|
||||||
duplicatedMessages.Add(receiveMessageCopy.Take(length.Value).ToArray());
|
var duplicateMessage = receiveMessageCopy.Take(length.Value).ToArray();
|
||||||
|
if (CheckRightFunc != null && CheckRightFunc(duplicateMessage) == false)
|
||||||
|
{
|
||||||
|
receiveMessageCopy = receiveMessageCopy.TakeLast(receiveMessage.Length - 1).ToArray();
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
duplicatedMessages.Add(duplicateMessage);
|
||||||
receiveMessageCopy = receiveMessageCopy.TakeLast(receiveMessage.Length - length.Value).ToArray();
|
receiveMessageCopy = receiveMessageCopy.TakeLast(receiveMessage.Length - length.Value).ToArray();
|
||||||
if (receiveMessageCopy.Length == 0) break;
|
if (receiveMessageCopy.Length == 0) break;
|
||||||
length = LengthCalc?.Invoke(receiveMessageCopy);
|
length = LengthCalc?.Invoke(receiveMessageCopy);
|
||||||
@@ -147,8 +160,6 @@ namespace Modbus.Net
|
|||||||
return ans;
|
return ans;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 从等待队列中匹配信息
|
/// 从等待队列中匹配信息
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|||||||
68
Modbus.Net/Modbus.Net/Controller/ContentCheck.cs
Normal file
68
Modbus.Net/Modbus.Net/Controller/ContentCheck.cs
Normal file
@@ -0,0 +1,68 @@
|
|||||||
|
using System.Text;
|
||||||
|
|
||||||
|
namespace Modbus.Net
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 数据检查类
|
||||||
|
/// </summary>
|
||||||
|
public static class ContentCheck
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// 检查接收的数据是否正确
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="content">接收协议的内容</param>
|
||||||
|
/// <returns>协议是否是正确的</returns>
|
||||||
|
public static bool? CheckRight(byte[] content)
|
||||||
|
{
|
||||||
|
if (content == null || content.Length == 0)
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Lrc校验
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="content">接收协议的内容</param>
|
||||||
|
/// <returns>协议是否是正确的</returns>
|
||||||
|
public static bool? LrcCheckRight(byte[] content)
|
||||||
|
{
|
||||||
|
var baseCheck = CheckRight(content);
|
||||||
|
if (baseCheck != true) return baseCheck;
|
||||||
|
var contentString = Encoding.ASCII.GetString(content);
|
||||||
|
if (!Crc16.GetInstance().LrcEfficacy(contentString))
|
||||||
|
return false;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Crc16校验
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="content">接收协议的内容</param>
|
||||||
|
/// <returns>协议是否是正确的</returns>
|
||||||
|
public static bool? Crc16CheckRight(byte[] content)
|
||||||
|
{
|
||||||
|
var baseCheck = CheckRight(content);
|
||||||
|
if (baseCheck != true) return baseCheck;
|
||||||
|
if (!Crc16.GetInstance().CrcEfficacy(content))
|
||||||
|
return false;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Fcs校验
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="content">接收协议的内容</param>
|
||||||
|
/// <returns>协议是否是正确的</returns>
|
||||||
|
public static bool? FcsCheckRight(byte[] content)
|
||||||
|
{
|
||||||
|
var fcsCheck = 0;
|
||||||
|
for (var i = 4; i < content.Length - 2; i++)
|
||||||
|
fcsCheck += content[i];
|
||||||
|
fcsCheck = fcsCheck % 256;
|
||||||
|
if (fcsCheck != content[content.Length - 2]) return false;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -31,9 +31,10 @@ namespace Modbus.Net
|
|||||||
/// <param name="acquireTime">间隔时间</param>
|
/// <param name="acquireTime">间隔时间</param>
|
||||||
/// <param name="activateSema">是否开启信号量</param>
|
/// <param name="activateSema">是否开启信号量</param>
|
||||||
/// <param name="lengthCalc">包切分长度函数</param>
|
/// <param name="lengthCalc">包切分长度函数</param>
|
||||||
|
/// <param name="checkRightFunc">包校验函数</param>
|
||||||
/// <param name="waitingListMaxCount">包等待队列长度</param>
|
/// <param name="waitingListMaxCount">包等待队列长度</param>
|
||||||
public FifoController(int acquireTime, bool activateSema = true, Func<byte[], int> lengthCalc = null, int? waitingListMaxCount = null)
|
public FifoController(int acquireTime, bool activateSema = true, Func<byte[], int> lengthCalc = null, Func<byte[], bool?> checkRightFunc = null, int? waitingListMaxCount = null)
|
||||||
: base(lengthCalc)
|
: base(lengthCalc, checkRightFunc)
|
||||||
{
|
{
|
||||||
_waitingListMaxCount = int.Parse(waitingListMaxCount != null ? waitingListMaxCount.ToString() : null ?? ConfigurationReader.GetValueDirect("Controller", "WaitingListCount"));
|
_waitingListMaxCount = int.Parse(waitingListMaxCount != null ? waitingListMaxCount.ToString() : null ?? ConfigurationReader.GetValueDirect("Controller", "WaitingListCount"));
|
||||||
if (activateSema)
|
if (activateSema)
|
||||||
|
|||||||
@@ -21,9 +21,10 @@ namespace Modbus.Net
|
|||||||
/// <param name="acquireTime">获取间隔</param>
|
/// <param name="acquireTime">获取间隔</param>
|
||||||
/// <param name="activateSema">是否开启信号量</param>
|
/// <param name="activateSema">是否开启信号量</param>
|
||||||
/// <param name="lengthCalc">包长度计算</param>
|
/// <param name="lengthCalc">包长度计算</param>
|
||||||
|
/// <param name="checkRightFunc">包校验函数</param>
|
||||||
/// <param name="waitingListMaxCount">包等待队列长度</param>
|
/// <param name="waitingListMaxCount">包等待队列长度</param>
|
||||||
public MatchController(ICollection<(int, int)>[] keyMatches, int acquireTime, bool activateSema = true,
|
public MatchController(ICollection<(int, int)>[] keyMatches, int acquireTime, bool activateSema = true,
|
||||||
Func<byte[], int> lengthCalc = null, int? waitingListMaxCount = null) : base(acquireTime, activateSema, lengthCalc, waitingListMaxCount)
|
Func<byte[], int> lengthCalc = null, Func<byte[], bool?> checkRightFunc = null, int? waitingListMaxCount = null) : base(acquireTime, activateSema, lengthCalc, checkRightFunc, waitingListMaxCount)
|
||||||
{
|
{
|
||||||
KeyMatches = keyMatches;
|
KeyMatches = keyMatches;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,8 +10,8 @@ namespace Modbus.Net
|
|||||||
{
|
{
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public MatchDirectlySendController(ICollection<(int, int)>[] keyMatches,
|
public MatchDirectlySendController(ICollection<(int, int)>[] keyMatches,
|
||||||
Func<byte[], int> lengthCalc = null, int? waitingListMaxCount = null) : base(keyMatches,
|
Func<byte[], int> lengthCalc = null, Func<byte[], bool?> checkRightFunc = null, int? waitingListMaxCount = null) : base(keyMatches,
|
||||||
0, false, lengthCalc, waitingListMaxCount)
|
0, false, lengthCalc, checkRightFunc, waitingListMaxCount)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user