mirror of
				https://gitee.com/ThingsGateway/ThingsGateway.git
				synced 2025-11-04 17:43:58 +08:00 
			
		
		
		
	修复串口基类 缓存包 失效错误;完善modbusrtu长度校验
This commit is contained in:
		@@ -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
 | 
			
		||||
            {
 | 
			
		||||
 
 | 
			
		||||
@@ -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
 | 
			
		||||
            {
 | 
			
		||||
 
 | 
			
		||||
@@ -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;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
@@ -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 };
 | 
			
		||||
 
 | 
			
		||||
@@ -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;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user