Modbus驱动增加ascii字符串处理

This commit is contained in:
iioter 2022-07-17 18:03:15 +08:00
parent d9f205a541
commit a3d635f192

View File

@ -6,6 +6,7 @@ using System.Collections.Generic;
using System.IO.Ports; using System.IO.Ports;
using System.Net; using System.Net;
using System.Net.Sockets; using System.Net.Sockets;
using System.Text;
namespace DriverModbusMaster namespace DriverModbusMaster
{ {
@ -334,44 +335,51 @@ namespace DriverModbusMaster
ret.StatusType = VaribaleStatusTypeEnum.Bad; ret.StatusType = VaribaleStatusTypeEnum.Bad;
else else
{ {
ushort startAddress; ushort startAddress, count;
if (!ushort.TryParse(ioarg.Address.Trim(), out startAddress)) ret = AnalyseAddress(ioarg, out startAddress, out count);
ret.StatusType = VaribaleStatusTypeEnum.AddressError; if(ret.StatusType!= VaribaleStatusTypeEnum.Good)
else return ret;
try
{ {
var count = GetModbusReadCount(3, ioarg.ValueType); var rawBuffers = new ushort[] { };
try if (FunCode == 3)
{ rawBuffers = master.ReadHoldingRegisters(SlaveAddress, startAddress, count);
var rawBuffers = new ushort[] { }; else if (FunCode == 4)
if (FunCode == 3) rawBuffers = master.ReadInputRegisters(SlaveAddress, startAddress, count);
rawBuffers = master.ReadHoldingRegisters(SlaveAddress, startAddress, count);
else if (FunCode == 4)
rawBuffers = master.ReadInputRegisters(SlaveAddress, startAddress, count);
var retBuffers = ChangeBuffersOrder(rawBuffers, ioarg.ValueType); var retBuffers = ChangeBuffersOrder(rawBuffers, ioarg.ValueType);
if (ioarg.ValueType.ToString().Contains("Uint16")) if (ioarg.ValueType == DataTypeEnum.AsciiString)
ret.Value = retBuffers[0]; retBuffers = rawBuffers;
else if (ioarg.ValueType.ToString().Contains("Int16"))
ret.Value = (short)retBuffers[0];
else if (ioarg.ValueType.ToString().Contains("Uint32"))
ret.Value = (UInt32)(retBuffers[0] << 16) + retBuffers[1];
else if (ioarg.ValueType.ToString().Contains("Int32"))
ret.Value = (Int32)(retBuffers[0] << 16) + retBuffers[1];
else if (ioarg.ValueType.ToString().Contains("Float"))
{
var bytes = new byte[] { (byte)(retBuffers[1] & 0xff), (byte)((retBuffers[1] >> 8) & 0xff), (byte)(retBuffers[0] & 0xff), (byte)((retBuffers[0] >> 8) & 0xff) };
ret.Value = BitConverter.ToSingle(bytes, 0);
}
} if (ioarg.ValueType.ToString().Contains("Uint16"))
catch (Exception ex) ret.Value = retBuffers[0];
else if (ioarg.ValueType.ToString().Contains("Int16"))
ret.Value = (short)retBuffers[0];
else if (ioarg.ValueType.ToString().Contains("Uint32"))
ret.Value = (UInt32)(retBuffers[0] << 16) + retBuffers[1];
else if (ioarg.ValueType.ToString().Contains("Int32"))
ret.Value = (Int32)(retBuffers[0] << 16) + retBuffers[1];
else if (ioarg.ValueType.ToString().Contains("Float"))
{ {
ret.StatusType = VaribaleStatusTypeEnum.Bad; var bytes = new byte[] { (byte)(retBuffers[1] & 0xff), (byte)((retBuffers[1] >> 8) & 0xff), (byte)(retBuffers[0] & 0xff), (byte)((retBuffers[0] >> 8) & 0xff) };
ret.Message = ex.Message; ret.Value = BitConverter.ToSingle(bytes, 0);
} }
else if (ioarg.ValueType.ToString().Contains("AsciiString"))
{
var str= Encoding.ASCII.GetString(GetBytes(retBuffers).ToArray());
if (str.Contains('\0'))
str = str.Split('\0')[0];
ret.Value = str;
}
}
catch (Exception ex)
{
ret.StatusType = VaribaleStatusTypeEnum.Bad;
ret.Message = ex.Message;
} }
}
}
return ret; return ret;
} }
@ -444,6 +452,47 @@ namespace DriverModbusMaster
return newBuffers; return newBuffers;
} }
private List<byte> GetBytes(ushort[] retBuffers)
{
List<byte> vs = new();
for (int i = 0; i < retBuffers.Length; i++)
{
vs.Add((byte)(retBuffers[i] & 0xFF));
vs.Add((byte)((retBuffers[i] & 0xFF00) >> 8));
}
return vs;
}
private DriverReturnValueModel AnalyseAddress(DriverAddressIoArgModel ioarg, out ushort StartAddress, out ushort ReadCount)
{
DriverReturnValueModel ret = new() { StatusType = VaribaleStatusTypeEnum.Good };
try
{
if (ioarg.ValueType == DataTypeEnum.AsciiString)
{
StartAddress = ushort.Parse(ioarg.Address.Split(',')[0]);
ReadCount = ushort.Parse(ioarg.Address.Split(',')[1]);
}
else
{
StartAddress = ushort.Parse(ioarg.Address);
ReadCount = GetModbusReadCount(3, ioarg.ValueType);
}
return ret;
}
catch (Exception ex)
{
ret.StatusType = VaribaleStatusTypeEnum.AddressError;
ret.Message = ex.Message;
StartAddress = 0;
ReadCount = 0;
return ret;
}
}
public async Task<RpcResponse> WriteAsync(string RequestId, string Method, DriverAddressIoArgModel Ioarg) public async Task<RpcResponse> WriteAsync(string RequestId, string Method, DriverAddressIoArgModel Ioarg)
{ {
RpcResponse rpcResponse = new() { IsSuccess = false }; RpcResponse rpcResponse = new() { IsSuccess = false };