编解码bug修复

zw
smartwyy 7 months ago
parent 2dd3d2d313
commit 0adcea06f8

@ -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()

@ -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; }

@ -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;
}
}

@ -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)

@ -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;
}
}

@ -15,6 +15,7 @@
<PackageReference Include="DotNetty.Common" Version="0.7.5" />
<PackageReference Include="DotNetty.Handlers" 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" />
</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