修改modbus驱动,和基于125个寄存器的数据推iotdb的处理。
This commit is contained in:
parent
5e8d11198f
commit
996b570057
@ -331,47 +331,70 @@ namespace DriverModbusMaster
|
|||||||
{
|
{
|
||||||
DriverReturnValueModel ret = new() { StatusType = VaribaleStatusTypeEnum.Good };
|
DriverReturnValueModel ret = new() { StatusType = VaribaleStatusTypeEnum.Good };
|
||||||
if (!IsConnected)
|
if (!IsConnected)
|
||||||
ret.StatusType = VaribaleStatusTypeEnum.Bad;
|
|
||||||
else
|
|
||||||
{
|
{
|
||||||
ushort startAddress;
|
|
||||||
if (!ushort.TryParse(ioarg.Address.Trim(), out startAddress))
|
ret.StatusType = VaribaleStatusTypeEnum.Bad;
|
||||||
ret.StatusType = VaribaleStatusTypeEnum.AddressError;
|
return ret;
|
||||||
else
|
}
|
||||||
|
var userSetAddrArray = ioarg.Address.Trim().Split(',');
|
||||||
|
ushort startAddress = 0;
|
||||||
|
if (!ushort.TryParse(ioarg.Address.Trim(), out startAddress))
|
||||||
|
{
|
||||||
|
if (userSetAddrArray.Length >1 && !ushort.TryParse(userSetAddrArray[0].Trim(), out startAddress))
|
||||||
{
|
{
|
||||||
var count = GetModbusReadCount(3, ioarg.ValueType);
|
ret.StatusType = VaribaleStatusTypeEnum.AddressError;
|
||||||
try
|
return ret;
|
||||||
{
|
}
|
||||||
var rawBuffers = new ushort[] { };
|
}
|
||||||
if (FunCode == 3)
|
|
||||||
rawBuffers = master.ReadHoldingRegisters(SlaveAddress, startAddress, count);
|
|
||||||
else if (FunCode == 4)
|
|
||||||
rawBuffers = master.ReadInputRegisters(SlaveAddress, startAddress, count);
|
|
||||||
|
|
||||||
var retBuffers = ChangeBuffersOrder(rawBuffers, ioarg.ValueType);
|
var count = GetModbusReadCount(3, ioarg.ValueType);
|
||||||
if (ioarg.ValueType.ToString().Contains("Uint16"))
|
if (ioarg.ValueType == DataTypeEnum.Any)
|
||||||
ret.Value = retBuffers[0];
|
{
|
||||||
else if (ioarg.ValueType.ToString().Contains("Int16"))
|
count = 125;
|
||||||
ret.Value = (short)retBuffers[0];
|
if (userSetAddrArray.Length>1)
|
||||||
else if (ioarg.ValueType.ToString().Contains("Uint32"))
|
{
|
||||||
ret.Value = (UInt32)(retBuffers[0] << 16) + retBuffers[1];
|
if(!ushort.TryParse(userSetAddrArray[1], out count))
|
||||||
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);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
catch (Exception ex)
|
|
||||||
{
|
{
|
||||||
ret.StatusType = VaribaleStatusTypeEnum.Bad;
|
//转换失败!
|
||||||
ret.Message = ex.Message;
|
count = 125;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var rawBuffers = new ushort[] { };
|
||||||
|
if (FunCode == 3)
|
||||||
|
rawBuffers = master.ReadHoldingRegisters(SlaveAddress, startAddress, count);
|
||||||
|
else if (FunCode == 4)
|
||||||
|
rawBuffers = master.ReadInputRegisters(SlaveAddress, startAddress, count);
|
||||||
|
|
||||||
|
var retBuffers = ChangeBuffersOrder(rawBuffers, ioarg.ValueType);
|
||||||
|
if (ioarg.ValueType.ToString().Contains("Uint16"))
|
||||||
|
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"))
|
||||||
|
{
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
else if (ioarg.ValueType.ToString().Contains("Any"))
|
||||||
|
{
|
||||||
|
ret.Value = retBuffers;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
ret.StatusType = VaribaleStatusTypeEnum.Bad;
|
||||||
|
ret.Message = ex.Message;
|
||||||
|
}
|
||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -388,6 +411,9 @@ namespace DriverModbusMaster
|
|||||||
//预留了大小端转换的
|
//预留了大小端转换的
|
||||||
private ushort[] ChangeBuffersOrder(ushort[] buffers, DataTypeEnum dataType)
|
private ushort[] ChangeBuffersOrder(ushort[] buffers, DataTypeEnum dataType)
|
||||||
{
|
{
|
||||||
|
if (dataType == DataTypeEnum.Any)
|
||||||
|
return buffers;
|
||||||
|
|
||||||
var newBuffers = new ushort[buffers.Length];
|
var newBuffers = new ushort[buffers.Length];
|
||||||
if (dataType.ToString().Contains("32") || dataType.ToString().Contains("Float"))
|
if (dataType.ToString().Contains("32") || dataType.ToString().Contains("Float"))
|
||||||
{
|
{
|
||||||
|
@ -492,19 +492,44 @@ namespace Plugin
|
|||||||
break;
|
break;
|
||||||
case IoTPlatformType.IotDB:
|
case IoTPlatformType.IotDB:
|
||||||
{
|
{
|
||||||
foreach (var payload in SendModel[device.DeviceName])
|
if(Client.IsConnected)
|
||||||
{
|
{
|
||||||
if (payload.DeviceStatus != DeviceStatusTypeEnum.Good)
|
foreach (var payload in SendModel[device.DeviceName])
|
||||||
continue;
|
|
||||||
|
|
||||||
IotTsData tsData = new IotTsData()
|
|
||||||
{
|
{
|
||||||
device = device.DeviceName,
|
if (payload.DeviceStatus != DeviceStatusTypeEnum.Good)
|
||||||
timestamp = payload.TS,
|
continue;
|
||||||
measurements = payload.Values.Keys.ToList(),
|
|
||||||
values = payload.Values.Values.ToList()
|
var keylist= payload.Values.Keys.ToList();
|
||||||
};
|
var vallist = payload.Values.Values.ToList();
|
||||||
Client.PublishAsync(device.DeviceName, JsonConvert.SerializeObject(tsData));
|
IotTsData tsData = null;
|
||||||
|
if (keylist.Count==1&& vallist[0].GetType()== typeof(ushort[]))
|
||||||
|
{
|
||||||
|
var tempData = vallist[0] as ushort[];
|
||||||
|
tsData = new IotTsData()
|
||||||
|
{
|
||||||
|
device = device.DeviceName,
|
||||||
|
timestamp = payload.TS, measurements = new List<string>(), values=new List<dynamic>()
|
||||||
|
};
|
||||||
|
for (int i = 0; i < tempData.Length; i++)
|
||||||
|
{
|
||||||
|
tsData.measurements.Add(String.Format("{0}_{1}", keylist[0], i));
|
||||||
|
tsData.values.Add(tempData[i]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
|
||||||
|
tsData = new IotTsData()
|
||||||
|
{
|
||||||
|
device = device.DeviceName,
|
||||||
|
timestamp = payload.TS,
|
||||||
|
measurements = keylist,
|
||||||
|
values = vallist
|
||||||
|
};
|
||||||
|
}
|
||||||
|
if(tsData != null)
|
||||||
|
Client.PublishAsync(device.DeviceName, JsonConvert.SerializeObject(tsData));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
break;
|
break;
|
||||||
|
@ -13,7 +13,7 @@ namespace PluginInterface.IotDB
|
|||||||
{
|
{
|
||||||
public string device { get; set; }
|
public string device { get; set; }
|
||||||
public List<string> measurements { get; set; }
|
public List<string> measurements { get; set; }
|
||||||
public List<object> values { get; set; }
|
public List<dynamic> values { get; set; }
|
||||||
public long timestamp { get; set; }
|
public long timestamp { get; set; }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user