From 51ded333d9c592361c7d6e7c97ae8fac188dfa2a Mon Sep 17 00:00:00 2001 From: parallelbgls Date: Wed, 21 May 2025 12:04:12 +0800 Subject: [PATCH] Fix --- .../Modbus.Net.BigEndian3412ValueHelper.cs | 8 ++++---- Modbus.Net/Modbus.Net/Helper/ValueHelper.cs | 20 ++++++++++++++----- Modbus.Net/Modbus.Net/Machine/BaseMachine.cs | 4 ++++ .../Modbus.Net.Tests/appsettings.default.json | 7 ++++--- 4 files changed, 27 insertions(+), 12 deletions(-) diff --git a/Modbus.Net/Modbus.Net.BigEndian3412/Modbus.Net.BigEndian3412ValueHelper.cs b/Modbus.Net/Modbus.Net.BigEndian3412/Modbus.Net.BigEndian3412ValueHelper.cs index 3501caa..859d78c 100644 --- a/Modbus.Net/Modbus.Net.BigEndian3412/Modbus.Net.BigEndian3412ValueHelper.cs +++ b/Modbus.Net/Modbus.Net.BigEndian3412/Modbus.Net.BigEndian3412ValueHelper.cs @@ -28,9 +28,9 @@ namespace Modbus.Net /// /// 是否为大端 /// - protected new bool LittleEndian => false; + public override bool LittleEndian => true; - protected new bool LittleEndianBit => false; + public override bool LittleEndianBit => false; /// /// 覆盖的获取实例的方法 @@ -154,9 +154,9 @@ namespace Modbus.Net /// /// 是否为大端 /// - protected new bool LittleEndian => true; + public override bool LittleEndian => true; - protected new bool LittleEndianBit => false; + public override bool LittleEndianBit => true; /// /// 覆盖的获取实例的方法 diff --git a/Modbus.Net/Modbus.Net/Helper/ValueHelper.cs b/Modbus.Net/Modbus.Net/Helper/ValueHelper.cs index d8de9f0..a0d8fb3 100644 --- a/Modbus.Net/Modbus.Net/Helper/ValueHelper.cs +++ b/Modbus.Net/Modbus.Net/Helper/ValueHelper.cs @@ -12,6 +12,16 @@ namespace Modbus.Net /// public abstract class ValueHelper { + /// + /// 是否为小端格式 + /// + public abstract bool LittleEndian { get; } + + /// + /// 位是否为小端格式 + /// + public abstract bool LittleEndianBit { get; } + /// /// 兼容数据类型对应的字节长度 /// @@ -347,12 +357,12 @@ namespace Modbus.Net /// /// 协议中的内容构造是否小端的,默认是小端构造协议。 /// - public static bool LittleEndian => true; + public override bool LittleEndian => true; /// /// 协议中的比特位内容构造是否小端的,默认是小端构造协议。 /// - public static bool LittleEndianBit => true; + public override bool LittleEndianBit => true; /// /// 将一个byte数字转换为一个byte元素的数组。 @@ -1228,7 +1238,7 @@ namespace Modbus.Net /// /// 是否为大端 /// - protected new bool LittleEndian => false; + public override bool LittleEndian => false; /// /// 覆盖的获取实例的方法 @@ -1471,12 +1481,12 @@ namespace Modbus.Net /// /// 是否为小端 /// - protected new bool LittleEndian => false; + public override bool LittleEndian => false; /// /// 是否为小端位 /// - protected new bool LittleEndianBit => false; + public override bool LittleEndianBit => false; /// /// 覆盖的实例获取方法 diff --git a/Modbus.Net/Modbus.Net/Machine/BaseMachine.cs b/Modbus.Net/Modbus.Net/Machine/BaseMachine.cs index 7fabd1e..ae1df2c 100644 --- a/Modbus.Net/Modbus.Net/Machine/BaseMachine.cs +++ b/Modbus.Net/Modbus.Net/Machine/BaseMachine.cs @@ -149,6 +149,10 @@ namespace Modbus.Net var localMainPos = (int)localPos; //字节坐标的子地址位置 var localSubPos = (int)((localPos - localMainPos) * 8); + if (ValueHelper.GetInstance(BaseUtility.Endian).LittleEndianBit == true && AddressTranslator.GetAreaByteLength(communicateAddress.Area) > 1 && ValueHelper.ByteLength[address.DataType.FullName] < AddressTranslator.GetAreaByteLength(communicateAddress.Area)) + { + localMainPos =(int)(2 * ((int)(localMainPos / AddressTranslator.GetAreaByteLength(communicateAddress.Area)) * AddressTranslator.GetAreaByteLength(communicateAddress.Area)) + (AddressTranslator.GetAreaByteLength(communicateAddress.Area) - 1) - localMainPos); + } //根据类型选择返回结果的键是通讯标识还是地址 string key; diff --git a/Tests/Modbus.Net.Tests/appsettings.default.json b/Tests/Modbus.Net.Tests/appsettings.default.json index 44e92a9..edc739c 100644 --- a/Tests/Modbus.Net.Tests/appsettings.default.json +++ b/Tests/Modbus.Net.Tests/appsettings.default.json @@ -3,7 +3,7 @@ "TCP": { "ConnectionTimeout": "5000", "FetchSleepTime": "100", - "FullDuplex": "False", + "FullDuplex": "True", "Modbus": { "ModbusPort": "502", "IP": "192.168.1.1" @@ -16,7 +16,7 @@ "UDP": { "ConnectionTimeout": "5000", "FetchSleepTime": "100", - "FullDuplex": "False", + "FullDuplex": "True", "Modbus": { "ModbusPort": "502", "IP": "192.168.1.1" @@ -46,7 +46,8 @@ "Host": "opc.tcp://localhost/test" }, "Controller": { - "WaitingListCount": "100" + "WaitingListCount": "100", + "NoResponse": false } } } \ No newline at end of file