From e8ef4abe216c0f559cd50c86c6005f46e33d9752 Mon Sep 17 00:00:00 2001 From: smartwyy <645583145@qq.com> Date: Mon, 27 May 2024 15:12:10 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E8=A7=A3=E6=9E=90=E8=A7=84?= =?UTF-8?q?=E5=88=99=E6=94=AF=E6=8C=81byte=E6=95=B0=E7=BB=84=E5=92=8Cstrin?= =?UTF-8?q?g=EF=BC=8Cmodbus=E5=A2=9E=E5=8A=A0=E6=89=B9=E9=87=8F=E5=86=99?= =?UTF-8?q?=E5=85=A5=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- HybirdFrameworkCore/Utils/BitUtls.cs | 10 +++------- .../ModbusTcpMaster/ModbusTcpMaster.cs | 17 +++++++++++++++++ 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/HybirdFrameworkCore/Utils/BitUtls.cs b/HybirdFrameworkCore/Utils/BitUtls.cs index 8c4f63a..fcc87b8 100644 --- a/HybirdFrameworkCore/Utils/BitUtls.cs +++ b/HybirdFrameworkCore/Utils/BitUtls.cs @@ -54,17 +54,13 @@ public static class BitUtls if (propertyType == STRING) { - return Convert.ChangeType(Encoding.ASCII.GetString(bytes, start, length/8), propertyType); + byte[] sub = Sub(bytes, start, length); + return Convert.ChangeType(Encoding.ASCII.GetString(sub), propertyType); } if (propertyType == BYTEARRAY) { - length = length / 8; - var bt = new byte[length]; - - for (var i = start; i < start + length; i++) bt[i - start] = bytes[i]; - - return bt; + return Sub(bytes, start, length); } throw new ArgumentException($"参数类型{propertyType}不支持encode!"); diff --git a/HybirdFrameworkDriver/ModbusTcpMaster/ModbusTcpMaster.cs b/HybirdFrameworkDriver/ModbusTcpMaster/ModbusTcpMaster.cs index 2787bca..205a783 100644 --- a/HybirdFrameworkDriver/ModbusTcpMaster/ModbusTcpMaster.cs +++ b/HybirdFrameworkDriver/ModbusTcpMaster/ModbusTcpMaster.cs @@ -184,6 +184,23 @@ public class ModbusTcpMaster return ModbusTcpNet.ReadDiscrete(address, length); } + /// + /// 将数据写入到Modbus的寄存器上去,需要指定起始地址和数据内容,如果富文本地址不指定,默认使用的功能码是 0x10
+ /// To write data to Modbus registers, you need to specify the start address and data content. If the rich text address is not specified, the default function code is 0x10 + ///
+ /// 起始地址,比如"100","x=4;100","s=1;100","s=1;x=4;100" + /// 写入的数据,长度根据data的长度来指示 + /// 返回写入结果 + /// 富地址格式,支持携带站号信息,功能码信息,具体参照类的示例代码 + /// + /// 此处演示批量写入的示例 + /// + /// + public OperateResult Write(string address, byte[] value) + { + return ModbusTcpNet.Write(address, value); + } + public bool WriteValue(ModbusProperty property) {