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