Skip to content

Commit

Permalink
借助物模型转换数据类型
Browse files Browse the repository at this point in the history
  • Loading branch information
nnhy committed Dec 18, 2023
1 parent eebbe70 commit 821466b
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 22 deletions.
53 changes: 39 additions & 14 deletions NewLife.Modbus/Drivers/ModbusDriver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,24 @@ public override IDictionary<String, Object> Read(INode node, IPoint[] points)
}

// 分割数据
return Dispatch(points, list);
var rs = Dispatch(points, list);

// 借助物模型转换数据类型
var spec = node.Device?.Specification;
if (spec != null)
{
foreach (var item in rs)
{
var pt = points.FirstOrDefault(e => e.Name == item.Key);
if (pt != null && item.Value is Byte[] data)
{
var v = spec.DecodeByThingModel(data, pt);
if (v != null) rs[item.Key] = v;
}
}
}

return rs;
}

internal IList<Segment> BuildSegments(IList<IPoint> points, ModbusParameter p)
Expand Down Expand Up @@ -310,6 +327,14 @@ public override Object Write(INode node, IPoint point, Object value)
var code = maddr.GetWriteCode();
if (code == 0) code = n.WriteCode;

// 借助物模型转换数据类型
var spec = node.Device?.Specification;
if (spec != null && value is not Byte[])
{
// 普通数值转为字节数组
value = spec.EncodeByThingModel(value, point);
}

UInt16[] vs;
if (value is Byte[] buf)
{
Expand All @@ -323,9 +348,9 @@ public override Object Write(INode node, IPoint point, Object value)
{
// 根据写入操作码决定转换为线圈还是寄存器
if (code == FunctionCodes.WriteCoil || code == FunctionCodes.WriteCoils)
vs = ConvertToCoil(value, point, n.Device?.Specification);
vs = ConvertToCoil(value, point, spec);
else
vs = ConvertToRegister(value, point, n.Device?.Specification);
vs = ConvertToRegister(value, point, spec);

if (vs == null) throw new NotSupportedException($"点位[{point.Name}][Type={point.Type}]不支持数据[{value}]");
}
Expand Down Expand Up @@ -358,17 +383,17 @@ protected virtual UInt16[] ConvertToCoil(Object data, IPoint point, ThingSpec sp
case TypeCode.Boolean:
case TypeCode.Byte:
case TypeCode.SByte:
return data.ToBoolean() ? new[] { (UInt16)0xFF00 } : new[] { (UInt16)0x00 };
return data.ToBoolean() ? [(UInt16)0xFF00] : [(UInt16)0x00];
case TypeCode.Int16:
case TypeCode.UInt16:
case TypeCode.Int32:
case TypeCode.UInt32:
return data.ToInt() > 0 ? new[] { (UInt16)0xFF00 } : new[] { (UInt16)0x00 };
return data.ToInt() > 0 ? [(UInt16)0xFF00] : [(UInt16)0x00];
case TypeCode.Int64:
case TypeCode.UInt64:
return data.ToLong() > 0 ? new[] { (UInt16)0xFF00 } : new[] { (UInt16)0x00 };
return data.ToLong() > 0 ? [(UInt16)0xFF00] : [(UInt16)0x00];
default:
return data.ToBoolean() ? new[] { (UInt16)0xFF00 } : new[] { (UInt16)0x00 };
return data.ToBoolean() ? [(UInt16)0xFF00] : [(UInt16)0x00];
}
}

Expand All @@ -393,41 +418,41 @@ protected virtual UInt16[] ConvertToRegister(Object data, IPoint point, ThingSpe
case TypeCode.Boolean:
case TypeCode.Byte:
case TypeCode.SByte:
return data.ToBoolean() ? new[] { (UInt16)0xFF00 } : new[] { (UInt16)0x00 };
return data.ToBoolean() ? [(UInt16)0xFF00] : [(UInt16)0x00];
case TypeCode.Int16:
case TypeCode.UInt16:
return new[] { (UInt16)data.ToInt() };
return [(UInt16)data.ToInt()];
case TypeCode.Int32:
case TypeCode.UInt32:
{
var n = data.ToInt();
return new[] { (UInt16)(n >> 16), (UInt16)(n & 0xFFFF) };
return [(UInt16)(n >> 16), (UInt16)(n & 0xFFFF)];
}
case TypeCode.Int64:
case TypeCode.UInt64:
{
var n = data.ToLong();
return new[] { (UInt16)(n >> 48), (UInt16)(n >> 32), (UInt16)(n >> 16), (UInt16)(n & 0xFFFF) };
return [(UInt16)(n >> 48), (UInt16)(n >> 32), (UInt16)(n >> 16), (UInt16)(n & 0xFFFF)];
}
case TypeCode.Single:
{
var d = (Single)data.ToDouble();
//var n = BitConverter.SingleToInt32Bits(d);
var n = (UInt32)d;
return new[] { (UInt16)(n >> 16), (UInt16)(n & 0xFFFF) };
return [(UInt16)(n >> 16), (UInt16)(n & 0xFFFF)];
}
case TypeCode.Double:
{
var d = (Double)data.ToDouble();
//var n = BitConverter.DoubleToInt64Bits(d);
var n = (UInt64)d;
return new[] { (UInt16)(n >> 48), (UInt16)(n >> 32), (UInt16)(n >> 16), (UInt16)(n & 0xFFFF) };
return [(UInt16)(n >> 48), (UInt16)(n >> 32), (UInt16)(n >> 16), (UInt16)(n & 0xFFFF)];
}
case TypeCode.Decimal:
{
var d = data.ToDecimal();
var n = (UInt64)d;
return new[] { (UInt16)(n >> 48), (UInt16)(n >> 32), (UInt16)(n >> 16), (UInt16)(n & 0xFFFF) };
return [(UInt16)(n >> 48), (UInt16)(n >> 32), (UInt16)(n >> 16), (UInt16)(n & 0xFFFF)];
}
//case TypeCode.String:
// break;
Expand Down
2 changes: 1 addition & 1 deletion NewLife.Modbus/ModbusHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public static class ModbusHelper
public static Byte[] GetAsciiBytes(params Byte[] numbers) => Encoding.UTF8.GetBytes(numbers.SelectMany((Byte n) => n.ToString("X2")).ToArray());

#region CRC
private static readonly UInt16[] crc_ta = new UInt16[16] { 0x0000, 0xCC01, 0xD801, 0x1400, 0xF001, 0x3C00, 0x2800, 0xE401, 0xA001, 0x6C00, 0x7800, 0xB401, 0x5000, 0x9C01, 0x8801, 0x4400, };
private static readonly UInt16[] crc_ta = [0x0000, 0xCC01, 0xD801, 0x1400, 0xF001, 0x3C00, 0x2800, 0xE401, 0xA001, 0x6C00, 0x7800, 0xB401, 0x5000, 0x9C01, 0x8801, 0x4400,];

/// <summary>Crc校验</summary>
/// <param name="data">数据</param>
Expand Down
4 changes: 2 additions & 2 deletions NewLife.Modbus/NewLife.Modbus.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,11 @@
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="NewLife.Core" Version="10.6.2023.1201" />
<PackageReference Include="NewLife.Core" Version="10.6.2023.1218-beta1120" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="NewLife.IoT" Version="2.0.2023.1210-beta1616" />
<PackageReference Include="NewLife.IoT" Version="2.0.2023.1218-beta1648" />
</ItemGroup>

<ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion NewLife.Modbus/Protocols/ModbusAddress.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public static Boolean TryParse(String address, out ModbusAddress modbusAddress)

// 去掉冒号后面的位域
var addr = address;
var p = addr.IndexOfAny(new[] { ':', '.' });
var p = addr.IndexOfAny([':', '.']);
if (p > 0) addr = addr.Substring(0, p);

modbusAddress = new ModbusAddress();
Expand Down
2 changes: 1 addition & 1 deletion NewLife.ModbusRTU/NewLife.ModbusRTU.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="NewLife.Core" Version="10.6.2023.1201" />
<PackageReference Include="NewLife.Core" Version="10.6.2023.1218-beta1120" />
</ItemGroup>
<ItemGroup Condition="'$(TargetFramework)'=='net461' or '$(TargetFramework)'=='netstandard2.0' or '$(TargetFramework)'=='netstandard2.1'">
<PackageReference Include="System.IO.Ports" Version="6.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="10.6.2023.1201" />
<PackageReference Include="NewLife.Core" Version="10.6.2023.1218-beta1120" />
</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="10.6.2023.1201" />
<PackageReference Include="NewLife.Core" Version="10.6.2023.1218-beta1120" />
</ItemGroup>

<ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion XUnitTest/XUnitTest.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="8.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageReference Include="Moq" Version="4.20.70" />
<PackageReference Include="NewLife.Core" Version="10.6.2023.1201" />
<PackageReference Include="NewLife.Core" Version="10.6.2023.1218-beta1120" />
<PackageReference Include="xunit" Version="2.6.3" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.5">
<PrivateAssets>all</PrivateAssets>
Expand Down

0 comments on commit 821466b

Please sign in to comment.