diff --git a/NewLife.Modbus/Drivers/ModbusDriver.cs b/NewLife.Modbus/Drivers/ModbusDriver.cs index 3bb9182..b6a8cfb 100644 --- a/NewLife.Modbus/Drivers/ModbusDriver.cs +++ b/NewLife.Modbus/Drivers/ModbusDriver.cs @@ -159,7 +159,24 @@ public override IDictionary 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 BuildSegments(IList points, ModbusParameter p) @@ -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) { @@ -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}]"); } @@ -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]; } } @@ -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; diff --git a/NewLife.Modbus/ModbusHelper.cs b/NewLife.Modbus/ModbusHelper.cs index fd3af9b..429d9a9 100644 --- a/NewLife.Modbus/ModbusHelper.cs +++ b/NewLife.Modbus/ModbusHelper.cs @@ -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,]; /// Crc校验 /// 数据 diff --git a/NewLife.Modbus/NewLife.Modbus.csproj b/NewLife.Modbus/NewLife.Modbus.csproj index bf3b24f..a3a79d4 100644 --- a/NewLife.Modbus/NewLife.Modbus.csproj +++ b/NewLife.Modbus/NewLife.Modbus.csproj @@ -41,11 +41,11 @@ all runtime; build; native; contentfiles; analyzers; buildtransitive - + - + diff --git a/NewLife.Modbus/Protocols/ModbusAddress.cs b/NewLife.Modbus/Protocols/ModbusAddress.cs index 4e1bcfe..7116765 100644 --- a/NewLife.Modbus/Protocols/ModbusAddress.cs +++ b/NewLife.Modbus/Protocols/ModbusAddress.cs @@ -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(); diff --git a/NewLife.ModbusRTU/NewLife.ModbusRTU.csproj b/NewLife.ModbusRTU/NewLife.ModbusRTU.csproj index 40d877d..aeba977 100644 --- a/NewLife.ModbusRTU/NewLife.ModbusRTU.csproj +++ b/NewLife.ModbusRTU/NewLife.ModbusRTU.csproj @@ -41,7 +41,7 @@ all runtime; build; native; contentfiles; analyzers; buildtransitive - + diff --git a/Test/Test.csproj b/Test/Test.csproj index 4057130..27ce9f0 100644 --- a/Test/Test.csproj +++ b/Test/Test.csproj @@ -16,7 +16,7 @@ - + diff --git a/WinModbus/WinModbus.csproj b/WinModbus/WinModbus.csproj index be25c82..aaa0d9f 100644 --- a/WinModbus/WinModbus.csproj +++ b/WinModbus/WinModbus.csproj @@ -18,7 +18,7 @@ - + diff --git a/XUnitTest/XUnitTest.csproj b/XUnitTest/XUnitTest.csproj index 71987cb..57a8832 100644 --- a/XUnitTest/XUnitTest.csproj +++ b/XUnitTest/XUnitTest.csproj @@ -14,7 +14,7 @@ - + all