From 5d3fe794f92abb468973c37f7d0164b9f6949bdc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=BD=97=E5=9C=A3?= Date: Tue, 23 Aug 2016 12:27:47 +0800 Subject: [PATCH] Add "nodata" process code. --- Modbus.Net/Modbus.Net/BaseMachine.cs | 65 ++++++++++++++++++++-------- 1 file changed, 46 insertions(+), 19 deletions(-) diff --git a/Modbus.Net/Modbus.Net/BaseMachine.cs b/Modbus.Net/Modbus.Net/BaseMachine.cs index 3970885..e972ddc 100644 --- a/Modbus.Net/Modbus.Net/BaseMachine.cs +++ b/Modbus.Net/Modbus.Net/BaseMachine.cs @@ -163,14 +163,26 @@ namespace Modbus.Net Math.Ceiling(communicateAddress.GetCount* BigEndianValueHelper.Instance.ByteLength[ communicateAddress.DataType.FullName])); - var datas = datasReturn.ReturnValue; - //如果没有数据,终止 - if (datas == null || datas.Length == 0 || datas.Length != - (int) - Math.Ceiling(communicateAddress.GetCount * - BigEndianValueHelper.Instance.ByteLength[ - communicateAddress.DataType.FullName])) return null; + byte[] datas; + + //如果设备本身能获取到数据但是没有数据 + if (datasReturn == null) + { + datas = null; + } + else + { + datas = datasReturn.ReturnValue; + + //如果没有数据,终止 + if (datas == null || datas.Length == 0 || datas.Length != + (int) + Math.Ceiling(communicateAddress.GetCount * + BigEndianValueHelper.Instance.ByteLength[ + communicateAddress.DataType.FullName])) return null; + } + int pos = 0; //解码数据 while (pos < communicateAddress.GetCount) @@ -200,18 +212,33 @@ namespace Modbus.Net break; } } - - //将获取的数据和对应的通讯标识对应 - ans.Add(key, - new ReturnUnit + + if (datas == null) + { + ans.Add(key, new ReturnUnit { - PlcValue = - Double.Parse( - datasReturn.IsLittleEndian ? ValueHelper.Instance.GetValue(datas, ref pos, address.DataType) - .ToString() : BigEndianValueHelper.Instance.GetValue(datas, ref pos, address.DataType) - .ToString()) *address.Zoom, + PlcValue = null, UnitExtend = address.UnitExtend }); + pos += (int)ValueHelper.Instance.ByteLength[address.DataType.ToString()]; + } + else + { + //将获取的数据和对应的通讯标识对应 + ans.Add(key, + new ReturnUnit + { + PlcValue = + Double.Parse( + datasReturn.IsLittleEndian + ? ValueHelper.Instance.GetValue(datas, ref pos, address.DataType) + .ToString() + : BigEndianValueHelper.Instance.GetValue(datas, ref pos, + address.DataType) + .ToString())*address.Zoom, + UnitExtend = address.UnitExtend + }); + } } else { @@ -398,8 +425,8 @@ namespace Modbus.Net public static Dictionary MapGetValuesToSetValues(Dictionary getValues) { if (getValues == null) return null; - return (from getValue in getValues - select new KeyValuePair(getValue.Key, getValue.Value.PlcValue)).ToDictionary(p=>p.Key,p=>p.Value); + return (from getValue in getValues where getValue.Value.PlcValue != null + select new KeyValuePair(getValue.Key, getValue.Value.PlcValue.Value)).ToDictionary(p=>p.Key,p=>p.Value); } } @@ -455,7 +482,7 @@ namespace Modbus.Net /// /// 返回的数据 /// - public double PlcValue { get; set; } + public double? PlcValue { get; set; } /// /// 数据的扩展 ///