Modbus ASCII Fix

This commit is contained in:
luosheng
2023-03-22 07:40:15 +08:00
parent d8b84f4b5c
commit 970deb48be
5 changed files with 22 additions and 12 deletions

View File

@@ -24,7 +24,7 @@ namespace Modbus.Net.Modbus
public ModbusAsciiInTcpProtocolLinker(string ip, int port)
: base(ip, port)
{
((BaseConnector)BaseConnector).AddController(new FifoController(int.Parse(ConfigurationReader.GetValue("TCP:" + ip + ":" + port, "FetchSleepTime")), lengthCalc: content => { var ans = 0; for (int i = 9; i < 13; i++) { ans = ans * 10 + (content[i] - 48); } return ans; }, waitingListMaxCount: ConfigurationReader.GetValue("TCP:" + ip + ":" + port, "WaitingListCount") != null ? int.Parse(ConfigurationReader.GetValue("TCP:" + ip + ":" + port, "WaitingListCount")) : null));
((BaseConnector)BaseConnector).AddController(new FifoController(int.Parse(ConfigurationReader.GetValue("TCP:" + ip + ":" + port, "FetchSleepTime")), lengthCalc: content => { if (content[0] != 0x3a) return -1; 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));
}
/// <summary>

View File

@@ -24,7 +24,7 @@ namespace Modbus.Net.Modbus
public ModbusAsciiInUdpProtocolLinker(string ip, int port)
: base(ip, port)
{
((BaseConnector)BaseConnector).AddController(new FifoController(int.Parse(ConfigurationReader.GetValue("UDP:" + ip + ":" + port, "FetchSleepTime")), lengthCalc: content => { var ans = 0; for (int i = 9; i < 13; i++) { ans = ans * 10 + (content[i] - 48); } return ans; }, waitingListMaxCount: ConfigurationReader.GetValue("UDP:" + ip + ":" + port, "WaitingListCount") != null ? int.Parse(ConfigurationReader.GetValue("UDP:" + ip + ":" + port, "WaitingListCount")) : null));
((BaseConnector)BaseConnector).AddController(new FifoController(int.Parse(ConfigurationReader.GetValue("UDP:" + ip + ":" + port, "FetchSleepTime")), lengthCalc: content => { if (content[0] != 0x3a) return -1; 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));
}
/// <summary>

View File

@@ -16,7 +16,7 @@ namespace Modbus.Net.Modbus
public ModbusAsciiProtocolLinker(string com, int slaveAddress)
: base(com, slaveAddress)
{
((BaseConnector)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 => { var ans = 0; for (int i = 9; i < 13; i++) { ans = ans * 10 + (content[i] - 48); } return ans; }, waitingListMaxCount: ConfigurationReader.GetValue("COM:" + com + ":" + slaveAddress, "WaitingListCount") != null ? int.Parse(ConfigurationReader.GetValue("COM:" + com + ":" + slaveAddress, "WaitingListCount")) : null));
((BaseConnector)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 -1; 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));
}
/// <summary>

View File

@@ -85,7 +85,7 @@ namespace Modbus.Net
/// <summary>
/// 缓冲的字节流
/// </summary>
private List<byte> cachedBytes = new List<byte>();
private List<byte> _cachedBytes = new List<byte>();
/// <summary>
/// 构造器
@@ -488,23 +488,32 @@ namespace Modbus.Net
logger.LogDebug(
$"Com client {ConnectionToken} receive msg: {string.Concat(returnBytes.Select(p => " " + p.ToString("X2")))}");
cachedBytes.AddRange(returnBytes);
var isMessageConfirmed = Controller.ConfirmMessage(cachedBytes.ToArray());
lock (_cachedBytes)
{
_cachedBytes.AddRange(returnBytes);
}
var isMessageConfirmed = Controller.ConfirmMessage(_cachedBytes.ToArray());
foreach (var confirmed in isMessageConfirmed)
{
if (confirmed.Item2 == false)
{
//主动传输事件
}
cachedBytes.RemoveRange(0, confirmed.Item1.Length);
lock (_cachedBytes)
{
_cachedBytes.RemoveRange(0, confirmed.Item1.Length);
}
if (isMessageConfirmed.Count > 0 && cachedBytes.Count > 0)
}
if (isMessageConfirmed.Count > 0 && _cachedBytes.Count > 0)
{
logger.LogError("Com client {ConnectionToken} cached msg error: {Length}", ConnectionToken,
cachedBytes.Count);
_cachedBytes.Count);
logger.LogError(
$"Com client {ConnectionToken} cached msg: {string.Concat(cachedBytes.Select(p => " " + p.ToString("X2")))}");
cachedBytes.Clear();
$"Com client {ConnectionToken} cached msg: {string.Concat(_cachedBytes.Select(p => " " + p.ToString("X2")))}");
lock (_cachedBytes)
{
_cachedBytes.Clear();
}
}
RefreshReceiveCount();
}

View File

@@ -111,7 +111,7 @@ namespace Modbus.Net
Array.Copy(receiveMessage, receiveMessageCopy, receiveMessage.Length);
var length = LengthCalc?.Invoke(receiveMessageCopy);
List<byte[]> duplicatedMessages;
if (length == null) return ans;
if (length == null || length == -1) return ans;
else
{
duplicatedMessages = new List<byte[]>();
@@ -119,6 +119,7 @@ namespace Modbus.Net
{
duplicatedMessages.Add(receiveMessageCopy.Take(length.Value).ToArray());
receiveMessageCopy = receiveMessageCopy.TakeLast(receiveMessage.Length - length.Value).ToArray();
if (receiveMessageCopy.Length == 0) break;
length = LengthCalc?.Invoke(receiveMessageCopy);
if (length == -1) break;
}