修改解析规则支持byte数组和string,modbus增加批量写入接口

master
smartwyy 5 months ago
parent d061b8b069
commit e8ef4abe21

@ -54,17 +54,13 @@ public static class BitUtls
if (propertyType == STRING) 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) if (propertyType == BYTEARRAY)
{ {
length = length / 8; return Sub(bytes, start, length);
var bt = new byte[length];
for (var i = start; i < start + length; i++) bt[i - start] = bytes[i];
return bt;
} }
throw new ArgumentException($"参数类型{propertyType}不支持encode"); throw new ArgumentException($"参数类型{propertyType}不支持encode");

@ -184,6 +184,23 @@ public class ModbusTcpMaster
return ModbusTcpNet.ReadDiscrete(address, length); return ModbusTcpNet.ReadDiscrete(address, length);
} }
/// <summary>
/// 将数据写入到Modbus的寄存器上去需要指定起始地址和数据内容如果富文本地址不指定默认使用的功能码是 0x10<br />
/// 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
/// </summary>
/// <param name="address">起始地址,比如"100""x=4;100""s=1;100","s=1;x=4;100"</param>
/// <param name="value">写入的数据长度根据data的长度来指示</param>
/// <returns>返回写入结果</returns>
/// <remarks>富地址格式,支持携带站号信息,功能码信息,具体参照类的示例代码</remarks>
/// <example>
/// 此处演示批量写入的示例
/// <code lang="cs" source="HslCommunication_Net45.Test\Documentation\Samples\Modbus\Modbus.cs" region="WriteExample1" title="Write示例" />
/// </example>
public OperateResult Write(string address, byte[] value)
{
return ModbusTcpNet.Write(address, value);
}
public bool WriteValue<T>(ModbusProperty<T> property) public bool WriteValue<T>(ModbusProperty<T> property)
{ {

Loading…
Cancel
Save