Assert Crc check messages
This commit is contained in:
parent
e3a4aace09
commit
661201617c
|
|
@ -88,6 +88,9 @@ public class ModbusRtuClient : ModbusClient
|
|||
|
||||
// TX
|
||||
cmd.Data.Concat(crc).ToList().Apply(Channel.Write);
|
||||
//var fullRequest = cmd.Data.Concat(crc).ToArray();
|
||||
//Console.WriteLine("📤 C# Modbus TX: " + BitConverter.ToString(fullRequest));
|
||||
|
||||
|
||||
// RX
|
||||
var response = Channel
|
||||
|
|
@ -127,11 +130,21 @@ public class ModbusRtuClient : ModbusClient
|
|||
|
||||
public static ArraySegment<Byte> AssertCrc(ArraySegment<Byte> data)
|
||||
{
|
||||
if (data.Count < 3)
|
||||
throw new ArgumentException("Data too short to contain CRC", nameof(data));
|
||||
|
||||
var expectedCrc = data.SkipLast(CrcSize).Apply(CalcCrc);
|
||||
var actualCrc = data.TakeLast(CrcSize);
|
||||
|
||||
if (!actualCrc.SequenceEqual(expectedCrc))
|
||||
{
|
||||
Console.WriteLine($"❌ CRC mismatch!");
|
||||
Console.WriteLine($"Expected CRC: {BitConverter.ToString(expectedCrc.ToArray())}");
|
||||
Console.WriteLine($"Actual CRC: {BitConverter.ToString(actualCrc.ToArray())}");
|
||||
Console.WriteLine($"Raw data: {BitConverter.ToString(data.ToArray())}");
|
||||
|
||||
throw new CrcException(expectedCrc, actualCrc);
|
||||
}
|
||||
|
||||
return data;
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue