diff --git a/HybirdFrameworkCore/Utils/ModelConvert.cs b/HybirdFrameworkCore/Utils/ModelConvert.cs index cb8684c..50e37bd 100644 --- a/HybirdFrameworkCore/Utils/ModelConvert.cs +++ b/HybirdFrameworkCore/Utils/ModelConvert.cs @@ -6,20 +6,7 @@ namespace HybirdFrameworkCore.Utils; public static class ModelConvert { - public static readonly Type BOOLEAN = typeof(bool); - public static readonly Type BYTE = typeof(byte); - public static readonly Type SBYTE = typeof(sbyte); - public static readonly Type SHORT = typeof(short); - public static readonly Type USHORT = typeof(ushort); - public static readonly Type INT = typeof(int); - public static readonly Type UINT = typeof(uint); - public static readonly Type LONG = typeof(long); - public static readonly Type ULONG = typeof(ulong); - public static readonly Type FLOAT = typeof(float); - public static readonly Type DOUBLE = typeof(double); - public static readonly Type STRING = typeof(string); - - public static T Declode(byte[] bytes) where T : class, new() + public static T Decode(byte[] bytes) where T : class, new() { T t = new T(); @@ -102,7 +89,6 @@ public static class ModelConvert { double offset = attribute.Offset; double scale = attribute.Scale; - Type propertyType = field.PropertyType; object? value = field.GetValue(t); return BitUtls.Value2Bytes(value, scale, offset); diff --git a/HybirdFrameworkDriver/ModbusTcpMaster/ModbusDecoder.cs b/HybirdFrameworkDriver/ModbusTcpMaster/ModbusDecoder.cs index ac87888..1280a60 100644 --- a/HybirdFrameworkDriver/ModbusTcpMaster/ModbusDecoder.cs +++ b/HybirdFrameworkDriver/ModbusTcpMaster/ModbusDecoder.cs @@ -15,7 +15,7 @@ public static class ModbusDecoder .Select(p => p.GetValue(t)).ToList(); int startRegisterNo = Int32.MaxValue; - foreach (object field in fields) + foreach (object? field in fields) { if (field != null) { @@ -27,7 +27,7 @@ public static class ModbusDecoder } } - foreach (object field in fields) + foreach (object? field in fields) { switch (field) { diff --git a/HybirdFrameworkDriver/ModbusTcpMaster/ModbusTcpMaster.cs b/HybirdFrameworkDriver/ModbusTcpMaster/ModbusTcpMaster.cs index 15e8dfe..0316fd0 100644 --- a/HybirdFrameworkDriver/ModbusTcpMaster/ModbusTcpMaster.cs +++ b/HybirdFrameworkDriver/ModbusTcpMaster/ModbusTcpMaster.cs @@ -1,5 +1,6 @@ using System.Collections; using HslCommunication; +using HslCommunication.Core; using HslCommunication.ModBus; using HybirdFrameworkCore.Utils; using log4net; @@ -14,6 +15,8 @@ public class ModbusTcpMaster public string Ip { get; set; } = "192.168.1.5"; public int Port { get; set; } = 502; + public DataFormat DataFormat { get; set; } = DataFormat.ABCD; + public int Duration { get; set; } = 1000; public bool Connected { get; set; } = false; @@ -40,6 +43,7 @@ public class ModbusTcpMaster if (ModbusTcpNet == null) { ModbusTcpNet = new ModbusTcpNet(Ip, Port); + ModbusTcpNet.DataFormat = DataFormat; OperateResult result = ModbusTcpNet.ConnectServer(); if (result.IsSuccess) { @@ -83,14 +87,11 @@ public class ModbusTcpMaster { if (ReadAction != null) { - // ReadAction(this); while (!StopFlag) { try { - ReadAction(this); - Thread.Sleep(Duration); } catch (Exception e) @@ -99,6 +100,7 @@ public class ModbusTcpMaster } } } + GetLog().Info("stop listen"); } @@ -140,25 +142,27 @@ public class ModbusTcpMaster ModbusDataType dataType = property.Type; int start = property.Start; int length = property.Length; + int registerNo = property.RegisterNo - 40000; byte[] setValue = BitUtls.Value2Bytes(value, property.Scale, property.Offset); OperateResult operateResult; + switch (dataType) { case ModbusDataType.Byte: start = start % 2 == 0 ? start / 2 : start / 2 + 1; - operateResult = ModbusTcpNet.Write("x=6;" + (property.RegisterNo - 40000 + start), setValue); + operateResult = ModbusTcpNet.Write("x=6;" + (registerNo + start), setValue); result = operateResult.IsSuccess; break; case ModbusDataType.Register: - operateResult = ModbusTcpNet.Write("x=16;" + (property.RegisterNo - 40000 + start), setValue); + operateResult = ModbusTcpNet.Write("x=16;" + (registerNo + start), setValue); result = operateResult.IsSuccess; break; case ModbusDataType.Bit: - start = start % 16 == 0 ? start / 16 : start / 16 + 1; length = length % 8 == 0 ? length / 8 : length / 8 + 1; - OperateResult readResult = ModbusTcpNet.Read("x=3;" + (property.RegisterNo - 40000 + start), (ushort)length); + OperateResult readResult = + ModbusTcpNet.Read("x=3;" + (registerNo + start), (ushort)length); if (readResult.IsSuccess) { @@ -173,7 +177,7 @@ public class ModbusTcpMaster bitArray.CopyTo(bytes, 0); - operateResult = ModbusTcpNet.Write("x=6;" + (property.RegisterNo - 40000 + start), bytes); + operateResult = ModbusTcpNet.Write("x=6;" + (registerNo + start), bytes); result = operateResult.IsSuccess; }