修复串口基类 缓存包 失效错误;完善modbusrtu长度校验

This commit is contained in:
Kimdiego2098
2023-08-28 19:25:22 +08:00
parent 8ca445aec0
commit 2f86ccc4bf
5 changed files with 17 additions and 4 deletions

View File

@@ -84,7 +84,13 @@ public abstract class ReadWriteDevicesTcpDataHandleAdapter<TRequest> : CustomDat
byteBlock.Read(out byte[] body, request.BodyLength);
var bytes = request.HeadBytes.SpliceArray(body);
return GetResponse(byteBlock, request, body, bytes);
var result = GetResponse(byteBlock, request, body, bytes);
if (result == FilterResult.Cache)
{
byteBlock.Pos = pos;//回退游标
}
return result;
}
else
{

View File

@@ -675,7 +675,7 @@ public class SerialSessionBase : BaseSerial, ISerialSession
byte[] buffer = new byte[2048];
var byteBlock = (ByteBlock)e.UserToken;
int num = MainSerialPort.Read(buffer, 0, MainSerialPort.BytesToRead);
byteBlock.Write(buffer, byteBlock.Len, num);
byteBlock.Write(buffer, 0, num);
this.HandleBuffer(byteBlock);
try
{

View File

@@ -22,13 +22,13 @@ public class DLT645_2007Message : MessageBase, IMessage
/// <inheritdoc/>
public override bool CheckHeadBytes(byte[] head)
{
BodyLength = -1;
return true;
}
/// <inheritdoc/>
protected override void SendBytesThen()
{
BodyLength = -1;
}
}

View File

@@ -124,6 +124,13 @@ internal class ModbusHelper
{
if (response.Length < 3)
return new OperResult<byte[], FilterResult>("数据长度不足" + response.ToHexString()) { Content2 = FilterResult.Cache };
if (response[1] >= 0x80)//错误码
return new OperResult<byte[], FilterResult>(GetDescriptionByErrorCode(response[2])) { Content2 = FilterResult.Success };
if ((response.Length < response[2] + 5))
return new OperResult<byte[], FilterResult>("数据长度不足" + response.ToHexString()) { Content2 = FilterResult.Cache };
var data = response.SelectMiddle(0, response[2] + 5);
if (crcCheck && !EasyCRC16.CheckCRC16(data))
return new OperResult<byte[], FilterResult>("Crc校验失败" + DataTransUtil.ByteToHexString(data, ' ')) { Content2 = FilterResult.Success };

View File

@@ -22,13 +22,13 @@ public class ModbusRtuMessage : MessageBase, IMessage
/// <inheritdoc/>
public override bool CheckHeadBytes(byte[] head)
{
BodyLength = -1;
return true;
}
/// <inheritdoc/>
protected override void SendBytesThen()
{
BodyLength = -1;
}
}