Add "nodata" process code.

This commit is contained in:
罗圣
2016-08-23 12:27:47 +08:00
parent 8db2dce585
commit 5d3fe794f9

View File

@@ -163,7 +163,17 @@ namespace Modbus.Net
Math.Ceiling(communicateAddress.GetCount* Math.Ceiling(communicateAddress.GetCount*
BigEndianValueHelper.Instance.ByteLength[ BigEndianValueHelper.Instance.ByteLength[
communicateAddress.DataType.FullName])); communicateAddress.DataType.FullName]));
var datas = datasReturn.ReturnValue;
byte[] datas;
//如果设备本身能获取到数据但是没有数据
if (datasReturn == null)
{
datas = null;
}
else
{
datas = datasReturn.ReturnValue;
//如果没有数据,终止 //如果没有数据,终止
if (datas == null || datas.Length == 0 || datas.Length != if (datas == null || datas.Length == 0 || datas.Length !=
@@ -171,6 +181,8 @@ namespace Modbus.Net
Math.Ceiling(communicateAddress.GetCount * Math.Ceiling(communicateAddress.GetCount *
BigEndianValueHelper.Instance.ByteLength[ BigEndianValueHelper.Instance.ByteLength[
communicateAddress.DataType.FullName])) return null; communicateAddress.DataType.FullName])) return null;
}
int pos = 0; int pos = 0;
//解码数据 //解码数据
while (pos < communicateAddress.GetCount) while (pos < communicateAddress.GetCount)
@@ -201,18 +213,33 @@ namespace Modbus.Net
} }
} }
if (datas == null)
{
ans.Add(key, new ReturnUnit
{
PlcValue = null,
UnitExtend = address.UnitExtend
});
pos += (int)ValueHelper.Instance.ByteLength[address.DataType.ToString()];
}
else
{
//将获取的数据和对应的通讯标识对应 //将获取的数据和对应的通讯标识对应
ans.Add(key, ans.Add(key,
new ReturnUnit new ReturnUnit
{ {
PlcValue = PlcValue =
Double.Parse( Double.Parse(
datasReturn.IsLittleEndian ? ValueHelper.Instance.GetValue(datas, ref pos, address.DataType) datasReturn.IsLittleEndian
.ToString() : BigEndianValueHelper.Instance.GetValue(datas, ref pos, address.DataType) ? ValueHelper.Instance.GetValue(datas, ref pos, address.DataType)
.ToString()) *address.Zoom, .ToString()
: BigEndianValueHelper.Instance.GetValue(datas, ref pos,
address.DataType)
.ToString())*address.Zoom,
UnitExtend = address.UnitExtend UnitExtend = address.UnitExtend
}); });
} }
}
else else
{ {
pos++; pos++;
@@ -398,8 +425,8 @@ namespace Modbus.Net
public static Dictionary<string, double> MapGetValuesToSetValues(Dictionary<string, ReturnUnit> getValues) public static Dictionary<string, double> MapGetValuesToSetValues(Dictionary<string, ReturnUnit> getValues)
{ {
if (getValues == null) return null; if (getValues == null) return null;
return (from getValue in getValues return (from getValue in getValues where getValue.Value.PlcValue != null
select new KeyValuePair<string, double>(getValue.Key, getValue.Value.PlcValue)).ToDictionary(p=>p.Key,p=>p.Value); select new KeyValuePair<string, double>(getValue.Key, getValue.Value.PlcValue.Value)).ToDictionary(p=>p.Key,p=>p.Value);
} }
} }
@@ -455,7 +482,7 @@ namespace Modbus.Net
/// <summary> /// <summary>
/// 返回的数据 /// 返回的数据
/// </summary> /// </summary>
public double PlcValue { get; set; } public double? PlcValue { get; set; }
/// <summary> /// <summary>
/// 数据的扩展 /// 数据的扩展
/// </summary> /// </summary>