Skip to content

Commit

Permalink
借助新版SpanWriter.Position的修改能力,支持ModbusRtuMessage的CRC计算
Browse files Browse the repository at this point in the history
  • Loading branch information
nnhy committed Dec 25, 2024
1 parent e34a609 commit 6d3d9e4
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 27 deletions.
24 changes: 7 additions & 17 deletions NewLife.Modbus/Drivers/ModbusDriver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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],
};
}

/// <summary>原始数据转寄存器数组</summary>
Expand All @@ -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()];
Expand Down
34 changes: 34 additions & 0 deletions NewLife.Modbus/ModbusHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,24 @@ public static UInt16 Crc(Byte[] data, Int32 offset, Int32 count = -1)
return u;
}

/// <summary>Crc校验</summary>
/// <param name="data">数据</param>
/// <returns></returns>
public static UInt16 Crc(ReadOnlySpan<Byte> 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;
}

/// <summary>Crc校验</summary>
/// <param name="data">数据流</param>
/// <param name="count">数量</param>
Expand Down Expand Up @@ -79,6 +97,22 @@ public static Byte Lrc(Byte[] data, Int32 offset, Int32 count = -1)
return (Byte)((rs ^ 0xFF) + 1);
}

/// <summary>LRC校验</summary>
/// <param name="data">数据</param>
/// <returns></returns>
public static Byte Lrc(ReadOnlySpan<Byte> 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);
}

/// <summary>LRC校验</summary>
/// <param name="stream"></param>
/// <returns></returns>
Expand Down
2 changes: 1 addition & 1 deletion NewLife.Modbus/NewLife.Modbus.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="NewLife.Core" Version="11.1.2024.1212-beta1042" />
<PackageReference Include="NewLife.Core" Version="11.2.2024.1214-beta0109" />
</ItemGroup>

<ItemGroup>
Expand Down
6 changes: 4 additions & 2 deletions NewLife.Modbus/Protocols/ModbusRtuMessage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
6 changes: 3 additions & 3 deletions NewLife.ModbusRTU/NewLife.ModbusRTU.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,13 @@
<PackageReference Include="System.IO.Ports" Version="6.0.0" />
</ItemGroup>
<ItemGroup Condition="'$(TargetFramework)'=='net6.0'">
<PackageReference Include="System.IO.Ports" Version="9.0.0" />
<PackageReference Include="System.IO.Ports" Version="6.0.0" />
</ItemGroup>
<ItemGroup Condition="'$(TargetFramework)'=='net7.0'">
<PackageReference Include="System.IO.Ports" Version="9.0.0" />
<PackageReference Include="System.IO.Ports" Version="7.0.0" />
</ItemGroup>
<ItemGroup Condition="'$(TargetFramework)'=='net8.0'">
<PackageReference Include="System.IO.Ports" Version="9.0.0" />
<PackageReference Include="System.IO.Ports" Version="8.0.0" />
</ItemGroup>
<ItemGroup Condition="'$(TargetFramework)'=='net9.0'">
<PackageReference Include="System.IO.Ports" Version="9.0.0" />
Expand Down
2 changes: 1 addition & 1 deletion Test/Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="NewLife.Core" Version="11.1.2024.1212-beta1042" />
<PackageReference Include="NewLife.Core" Version="11.2.2024.1214-beta0109" />
</ItemGroup>

<ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion WinModbus/WinModbus.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="NewLife.Core" Version="11.1.2024.1212-beta1042" />
<PackageReference Include="NewLife.Core" Version="11.2.2024.1214-beta0109" />
</ItemGroup>

<ItemGroup>
Expand Down
4 changes: 2 additions & 2 deletions XUnitTest/XUnitTest.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="9.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.12.0" />
<PackageReference Include="Moq" Version="4.20.72" />
<PackageReference Include="NewLife.Core" Version="11.1.2024.1212-beta1042" />
<PackageReference Include="NewLife.Core" Version="11.2.2024.1214-beta0109" />
<PackageReference Include="xunit" Version="2.9.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.8.2">
<PackageReference Include="xunit.runner.visualstudio" Version="3.0.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
Expand Down

0 comments on commit 6d3d9e4

Please sign in to comment.