From 0adcea06f82d89616a1eb864eca7a234751558ef Mon Sep 17 00:00:00 2001 From: smartwyy <645583145@qq.com> Date: Tue, 16 Apr 2024 14:35:23 +0800 Subject: [PATCH] =?UTF-8?q?=E7=BC=96=E8=A7=A3=E7=A0=81bug=E4=BF=AE?= =?UTF-8?q?=E5=A4=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ConsoleStarter/Program.cs | 9 +++++++ ConsoleStarter/TestPerson.cs | 5 +++- .../Autofac/Attribute/PropertyAttribute.cs | 4 +++- HybirdFrameworkCore/Utils/BitUtls.cs | 12 ++++++++-- HybirdFrameworkCore/Utils/ModelConvert.cs | 7 +++--- .../HybirdFrameworkDriver.csproj | 1 + .../ModbusTcpMaster/ModbusTcpMaster.cs | 24 +++++++++++++++++++ 7 files changed, 55 insertions(+), 7 deletions(-) create mode 100644 HybirdFrameworkDriver/ModbusTcpMaster/ModbusTcpMaster.cs diff --git a/ConsoleStarter/Program.cs b/ConsoleStarter/Program.cs index bbcddf0..40e233d 100644 --- a/ConsoleStarter/Program.cs +++ b/ConsoleStarter/Program.cs @@ -75,6 +75,15 @@ internal class Program Console.WriteLine(b); } + BitArray array = new BitArray(16); + array[0] = true; + byte[] ab = new byte[2]; + array.CopyTo(ab, 0); + foreach (var b in ab) + { + Console.WriteLine(b); + } + } private static void Test1() diff --git a/ConsoleStarter/TestPerson.cs b/ConsoleStarter/TestPerson.cs index 9bb8d03..982e360 100644 --- a/ConsoleStarter/TestPerson.cs +++ b/ConsoleStarter/TestPerson.cs @@ -1,4 +1,6 @@ -namespace ConsoleStarter; +using HybirdFrameworkCore.Autofac.Attribute; + +namespace ConsoleStarter; public class TestPerson { @@ -29,6 +31,7 @@ public class TestPerson public long lg { get; set; } public ulong ulg { get; set; } + [Property(start:2, length:4)] public float ft { get; set; } public double de { get; set; } diff --git a/HybirdFrameworkCore/Autofac/Attribute/PropertyAttribute.cs b/HybirdFrameworkCore/Autofac/Attribute/PropertyAttribute.cs index 6080643..1ce5488 100644 --- a/HybirdFrameworkCore/Autofac/Attribute/PropertyAttribute.cs +++ b/HybirdFrameworkCore/Autofac/Attribute/PropertyAttribute.cs @@ -6,15 +6,17 @@ public class PropertyAttribute : System.Attribute public readonly int Length; public readonly PropertyReadConstant Type; public readonly double Scale; + public readonly int Round; public readonly double Offset; public PropertyAttribute(int start, int length, PropertyReadConstant type = PropertyReadConstant.Bit, - double scale = 1, double offset = 0) + double scale = 1, int round = 0, double offset = 0) { this.Start = start; this.Length = length; this.Type = type; this.Scale = scale; + this.Round = round; this.Offset = offset; } } diff --git a/HybirdFrameworkCore/Utils/BitUtls.cs b/HybirdFrameworkCore/Utils/BitUtls.cs index 49611a0..be579f6 100644 --- a/HybirdFrameworkCore/Utils/BitUtls.cs +++ b/HybirdFrameworkCore/Utils/BitUtls.cs @@ -132,8 +132,16 @@ public static class BitUtls public static float Byte2Float(byte[] bytes, int startBit, int length, double factor) { - uint d = Byte2UInt16(bytes, startBit, length); - return (float)(d * factor); + if (length < 17) + { + Int32 d = Byte2Int16(bytes, startBit, length); + return (float)(d * factor); + } + else + { + Int32 d = Byte2Int32(bytes, startBit, length); + return (float)(d * factor); + } } public static double Byte2Double(byte[] bytes, int startBit, int length, double factor) diff --git a/HybirdFrameworkCore/Utils/ModelConvert.cs b/HybirdFrameworkCore/Utils/ModelConvert.cs index a2a2090..d5c0760 100644 --- a/HybirdFrameworkCore/Utils/ModelConvert.cs +++ b/HybirdFrameworkCore/Utils/ModelConvert.cs @@ -80,12 +80,12 @@ public static class ModelConvert else if (propertyType == FLOAT) { field.SetValue(t, - Convert.ChangeType(BitUtls.Byte2Float(bytes, start, length, scale) - offset, propertyType), null); + Convert.ChangeType(Math.Round(BitUtls.Byte2Float(bytes, start, length, scale) - offset, attribute.Round), propertyType), null); } else if (propertyType == DOUBLE) { field.SetValue(t, - Convert.ChangeType(BitUtls.Byte2Double(bytes, start, length, scale) - offset, propertyType), null); + Convert.ChangeType(Math.Round(BitUtls.Byte2Double(bytes, start, length, scale) - offset, attribute.Round), propertyType), null); } else if (propertyType == STRING) { @@ -129,10 +129,11 @@ public static class ModelConvert byte[] value = GetPropertyValue(t, field, attribute); start = attribute.Start; length = PropertyReadConstant.Byte == attribute.Type ? attribute.Length * 8 : attribute.Length; + length = length > value.Length * 8 ? value.Length * 8 : length; for (int i = 0; i < length; i++) { - bitArray[start + i] = (value[i % 8] & (1 << i % 8)) > 0; + bitArray[start + i] = (value[i / 8] & (1 << i % 8)) > 0; } } diff --git a/HybirdFrameworkDriver/HybirdFrameworkDriver.csproj b/HybirdFrameworkDriver/HybirdFrameworkDriver.csproj index 5a7cb30..a487379 100644 --- a/HybirdFrameworkDriver/HybirdFrameworkDriver.csproj +++ b/HybirdFrameworkDriver/HybirdFrameworkDriver.csproj @@ -15,6 +15,7 @@ + diff --git a/HybirdFrameworkDriver/ModbusTcpMaster/ModbusTcpMaster.cs b/HybirdFrameworkDriver/ModbusTcpMaster/ModbusTcpMaster.cs new file mode 100644 index 0000000..7e98461 --- /dev/null +++ b/HybirdFrameworkDriver/ModbusTcpMaster/ModbusTcpMaster.cs @@ -0,0 +1,24 @@ +using log4net; + +namespace HybirdFrameworkDriver.ModbusTcpMaster; + +public class ModbusTcpMaster +{ + private static readonly ILog Log = LogManager.GetLogger(typeof(ModbusTcpMaster)); + + public string Ip { get; set; } + public int Port { get; set; } + + public int Duration { get; set; } + public bool Connected { get; set; } + + ILog GetLog() + { + return Log; + } + + public bool Connect() + { + return Connected; + } +} \ No newline at end of file