编解码bug修复

master
smartwyy 7 months ago
parent 2dd3d2d313
commit 0adcea06f8

@ -75,6 +75,15 @@ internal class Program
Console.WriteLine(b); 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() private static void Test1()

@ -1,4 +1,6 @@
namespace ConsoleStarter; using HybirdFrameworkCore.Autofac.Attribute;
namespace ConsoleStarter;
public class TestPerson public class TestPerson
{ {
@ -29,6 +31,7 @@ public class TestPerson
public long lg { get; set; } public long lg { get; set; }
public ulong ulg { get; set; } public ulong ulg { get; set; }
[Property(start:2, length:4)]
public float ft { get; set; } public float ft { get; set; }
public double de { get; set; } public double de { get; set; }

@ -6,15 +6,17 @@ public class PropertyAttribute : System.Attribute
public readonly int Length; public readonly int Length;
public readonly PropertyReadConstant Type; public readonly PropertyReadConstant Type;
public readonly double Scale; public readonly double Scale;
public readonly int Round;
public readonly double Offset; public readonly double Offset;
public PropertyAttribute(int start, int length, PropertyReadConstant type = PropertyReadConstant.Bit, 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.Start = start;
this.Length = length; this.Length = length;
this.Type = type; this.Type = type;
this.Scale = scale; this.Scale = scale;
this.Round = round;
this.Offset = offset; this.Offset = offset;
} }
} }

@ -132,8 +132,16 @@ public static class BitUtls
public static float Byte2Float(byte[] bytes, int startBit, int length, double factor) public static float Byte2Float(byte[] bytes, int startBit, int length, double factor)
{ {
uint d = Byte2UInt16(bytes, startBit, length); if (length < 17)
return (float)(d * factor); {
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) public static double Byte2Double(byte[] bytes, int startBit, int length, double factor)

@ -80,12 +80,12 @@ public static class ModelConvert
else if (propertyType == FLOAT) else if (propertyType == FLOAT)
{ {
field.SetValue(t, 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) else if (propertyType == DOUBLE)
{ {
field.SetValue(t, 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) else if (propertyType == STRING)
{ {
@ -129,10 +129,11 @@ public static class ModelConvert
byte[] value = GetPropertyValue(t, field, attribute); byte[] value = GetPropertyValue(t, field, attribute);
start = attribute.Start; start = attribute.Start;
length = PropertyReadConstant.Byte == attribute.Type ? attribute.Length * 8 : attribute.Length; 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++) 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;
} }
} }

@ -15,6 +15,7 @@
<PackageReference Include="DotNetty.Common" Version="0.7.5" /> <PackageReference Include="DotNetty.Common" Version="0.7.5" />
<PackageReference Include="DotNetty.Handlers" Version="0.7.5" /> <PackageReference Include="DotNetty.Handlers" Version="0.7.5" />
<PackageReference Include="DotNetty.Transport" Version="0.7.5" /> <PackageReference Include="DotNetty.Transport" Version="0.7.5" />
<PackageReference Include="HslCommunication" Version="11.1.1" />
<PackageReference Include="log4net" Version="2.0.15" /> <PackageReference Include="log4net" Version="2.0.15" />
</ItemGroup> </ItemGroup>

@ -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;
}
}
Loading…
Cancel
Save