From 6d3d9e40937641208aa4546283675cc1ceb4b9ff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=99=BA=E8=83=BD=E5=A4=A7=E7=9F=B3=E5=A4=B4?= Date: Wed, 25 Dec 2024 23:52:23 +0800 Subject: [PATCH] =?UTF-8?q?=E5=80=9F=E5=8A=A9=E6=96=B0=E7=89=88SpanWriter.?= =?UTF-8?q?Position=E7=9A=84=E4=BF=AE=E6=94=B9=E8=83=BD=E5=8A=9B=EF=BC=8C?= =?UTF-8?q?=E6=94=AF=E6=8C=81ModbusRtuMessage=E7=9A=84CRC=E8=AE=A1?= =?UTF-8?q?=E7=AE=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- NewLife.Modbus/Drivers/ModbusDriver.cs | 24 ++++---------- NewLife.Modbus/ModbusHelper.cs | 34 ++++++++++++++++++++ NewLife.Modbus/NewLife.Modbus.csproj | 2 +- NewLife.Modbus/Protocols/ModbusRtuMessage.cs | 6 ++-- NewLife.ModbusRTU/NewLife.ModbusRTU.csproj | 6 ++-- Test/Test.csproj | 2 +- WinModbus/WinModbus.csproj | 2 +- XUnitTest/XUnitTest.csproj | 4 +-- 8 files changed, 53 insertions(+), 27 deletions(-) diff --git a/NewLife.Modbus/Drivers/ModbusDriver.cs b/NewLife.Modbus/Drivers/ModbusDriver.cs index d2b4f7d..026b3b2 100644 --- a/NewLife.Modbus/Drivers/ModbusDriver.cs +++ b/NewLife.Modbus/Drivers/ModbusDriver.cs @@ -379,23 +379,13 @@ protected virtual UInt16[] ConvertToCoil(Object data, IPoint point, ThingSpec sp } if (type == null) return null; - switch (type.GetTypeCode()) + return type.GetTypeCode() switch { - case TypeCode.Boolean: - case TypeCode.Byte: - case TypeCode.SByte: - return data.ToBoolean() ? [(UInt16)0xFF00] : [(UInt16)0x00]; - case TypeCode.Int16: - case TypeCode.UInt16: - case TypeCode.Int32: - case TypeCode.UInt32: - return data.ToInt() > 0 ? [(UInt16)0xFF00] : [(UInt16)0x00]; - case TypeCode.Int64: - case TypeCode.UInt64: - return data.ToLong() > 0 ? [(UInt16)0xFF00] : [(UInt16)0x00]; - default: - return data.ToBoolean() ? [(UInt16)0xFF00] : [(UInt16)0x00]; - } + TypeCode.Boolean or TypeCode.Byte or TypeCode.SByte => data.ToBoolean() ? [0xFF00] : [0x00], + TypeCode.Int16 or TypeCode.UInt16 or TypeCode.Int32 or TypeCode.UInt32 => data.ToInt() > 0 ? [0xFF00] : [0x00], + TypeCode.Int64 or TypeCode.UInt64 => data.ToLong() > 0 ? [0xFF00] : [0x00], + _ => data.ToBoolean() ? [0xFF00] : [0x00], + }; } /// 原始数据转寄存器数组 @@ -419,7 +409,7 @@ protected virtual UInt16[] ConvertToRegister(Object data, IPoint point, ThingSpe case TypeCode.Boolean: case TypeCode.Byte: case TypeCode.SByte: - return data.ToBoolean() ? [(UInt16)0xFF00] : [(UInt16)0x00]; + return data.ToBoolean() ? [0xFF00] : [0x00]; case TypeCode.Int16: case TypeCode.UInt16: return [(UInt16)data.ToInt()]; diff --git a/NewLife.Modbus/ModbusHelper.cs b/NewLife.Modbus/ModbusHelper.cs index 429d9a9..dd8775e 100644 --- a/NewLife.Modbus/ModbusHelper.cs +++ b/NewLife.Modbus/ModbusHelper.cs @@ -35,6 +35,24 @@ public static UInt16 Crc(Byte[] data, Int32 offset, Int32 count = -1) return u; } + /// Crc校验 + /// 数据 + /// + public static UInt16 Crc(ReadOnlySpan data) + { + if (data.Length == 0) return 0; + + UInt16 u = 0xFFFF; + for (var i = 0; i < data.Length; i++) + { + var b = data[i]; + u = (UInt16)(crc_ta[(b ^ u) & 15] ^ (u >> 4)); + u = (UInt16)(crc_ta[((b >> 4) ^ u) & 15] ^ (u >> 4)); + } + + return u; + } + /// Crc校验 /// 数据流 /// 数量 @@ -79,6 +97,22 @@ public static Byte Lrc(Byte[] data, Int32 offset, Int32 count = -1) return (Byte)((rs ^ 0xFF) + 1); } + /// LRC校验 + /// 数据 + /// + public static Byte Lrc(ReadOnlySpan data) + { + if (data.Length < 1) return 0; + + Byte rs = 0; + for (var i = 0; i < data.Length; i++) + { + rs += data[i]; + } + + return (Byte)((rs ^ 0xFF) + 1); + } + /// LRC校验 /// /// diff --git a/NewLife.Modbus/NewLife.Modbus.csproj b/NewLife.Modbus/NewLife.Modbus.csproj index 2515c79..1f680c1 100644 --- a/NewLife.Modbus/NewLife.Modbus.csproj +++ b/NewLife.Modbus/NewLife.Modbus.csproj @@ -44,7 +44,7 @@ all runtime; build; native; contentfiles; analyzers; buildtransitive - + diff --git a/NewLife.Modbus/Protocols/ModbusRtuMessage.cs b/NewLife.Modbus/Protocols/ModbusRtuMessage.cs index bf092b2..2500edb 100644 --- a/NewLife.Modbus/Protocols/ModbusRtuMessage.cs +++ b/NewLife.Modbus/Protocols/ModbusRtuMessage.cs @@ -63,9 +63,11 @@ public override Boolean Write(ref SpanWriter writer) var p = writer.Position; if (!base.Write(ref writer)) return false; - //writer.Position = p; - //Crc2 = ModbusHelper.Crc(writer); + var size = writer.Position - p; + writer.Position = p; + Crc2 = ModbusHelper.Crc(writer.Span[..size]); + writer.Position = p + size; writer.Write(Crc2.GetBytes(true)); return true; diff --git a/NewLife.ModbusRTU/NewLife.ModbusRTU.csproj b/NewLife.ModbusRTU/NewLife.ModbusRTU.csproj index 5783b6d..a3fc3a0 100644 --- a/NewLife.ModbusRTU/NewLife.ModbusRTU.csproj +++ b/NewLife.ModbusRTU/NewLife.ModbusRTU.csproj @@ -49,13 +49,13 @@ - + - + - + diff --git a/Test/Test.csproj b/Test/Test.csproj index 4cd49ac..7897456 100644 --- a/Test/Test.csproj +++ b/Test/Test.csproj @@ -16,7 +16,7 @@ - + diff --git a/WinModbus/WinModbus.csproj b/WinModbus/WinModbus.csproj index a219ae5..cf0ddbb 100644 --- a/WinModbus/WinModbus.csproj +++ b/WinModbus/WinModbus.csproj @@ -18,7 +18,7 @@ - + diff --git a/XUnitTest/XUnitTest.csproj b/XUnitTest/XUnitTest.csproj index f629093..21845b8 100644 --- a/XUnitTest/XUnitTest.csproj +++ b/XUnitTest/XUnitTest.csproj @@ -14,9 +14,9 @@ - + - + all runtime; build; native; contentfiles; analyzers; buildtransitive