Receiver Fix
This commit is contained in:
@@ -3,6 +3,7 @@ using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using AddressUnit = Modbus.Net.AddressUnit<string, int, int>;
|
||||
using DataReturnDef = Modbus.Net.DataReturnDef<string, double>;
|
||||
|
||||
@@ -73,40 +74,6 @@ namespace Modbus.Net.Modbus
|
||||
List<AddressUnit> addressMap = AddressReader<string, int, int>.ReadAddresses(addressMapName).ToList();
|
||||
if (values != null)
|
||||
{
|
||||
try
|
||||
{
|
||||
for (int i = 0; i < addressMap.Count; i++)
|
||||
{
|
||||
var pos = (addressMap[i].Address - 1) * 2;
|
||||
var subpos = addressMap[i].SubAddress;
|
||||
string valueString = null;
|
||||
if (addressMap[i].Area == "4X")
|
||||
{
|
||||
valueString = endian.GetValue(threex, ref pos, ref subpos, addressMap[i].DataType).ToString();
|
||||
}
|
||||
else if (addressMap[i].Area == "0X")
|
||||
{
|
||||
valueString = zerox[addressMap[i].Address - 1].ToString();
|
||||
}
|
||||
if (valueString == "True") valueString = "1";
|
||||
if (valueString == "False") valueString = "0";
|
||||
var value = double.Parse(valueString);
|
||||
value = value * addressMap[i].Zoom;
|
||||
value = Math.Round(value, addressMap[i].DecimalPos);
|
||||
AddValueToValueDic(valueDic, returnDic, addressMap[i], value, dataType);
|
||||
if (ReturnValueDictionary != null)
|
||||
{
|
||||
var dataReturn = new DataReturnDef();
|
||||
dataReturn.MachineId = machineName;
|
||||
dataReturn.ReturnValues = new ReturnStruct<Dictionary<string, ReturnUnit<double>>>() { IsSuccess = true, Datas = returnDic };
|
||||
ReturnValueDictionary(dataReturn);
|
||||
}
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
//_logger.LogError(ex, "Error");
|
||||
}
|
||||
switch (receiveContent.FunctionCode)
|
||||
{
|
||||
case (byte)ModbusProtocolFunctionCode.WriteMultiRegister:
|
||||
@@ -134,7 +101,7 @@ namespace Modbus.Net.Modbus
|
||||
List<bool> bitList = new List<bool>();
|
||||
for (int i = 0; i < receiveContent.WriteByteCount; i++)
|
||||
{
|
||||
var bitArray = BigEndianLsbValueHelper.Instance.GetBits(receiveContent.WriteContent, ref pos);
|
||||
var bitArray = endian.GetBits(receiveContent.WriteContent, ref pos);
|
||||
bitList.AddRange(bitArray.ToList());
|
||||
}
|
||||
Array.Copy(bitList.ToArray(), 0, zerox, receiveContent.StartAddress, bitList.Count);
|
||||
@@ -148,6 +115,40 @@ namespace Modbus.Net.Modbus
|
||||
break;
|
||||
}
|
||||
}
|
||||
try
|
||||
{
|
||||
for (int i = 0; i < addressMap.Count; i++)
|
||||
{
|
||||
var pos = (addressMap[i].Address - 1) * 2;
|
||||
var subpos = addressMap[i].SubAddress;
|
||||
string valueString = null;
|
||||
if (addressMap[i].Area == "4X")
|
||||
{
|
||||
valueString = endian.GetValue(threex, ref pos, ref subpos, addressMap[i].DataType).ToString();
|
||||
}
|
||||
else if (addressMap[i].Area == "0X")
|
||||
{
|
||||
valueString = zerox[addressMap[i].Address - 1].ToString();
|
||||
}
|
||||
if (valueString == "True") valueString = "1";
|
||||
if (valueString == "False") valueString = "0";
|
||||
var value = double.Parse(valueString);
|
||||
value = value * addressMap[i].Zoom;
|
||||
value = Math.Round(value, addressMap[i].DecimalPos);
|
||||
AddValueToValueDic(valueDic, returnDic, addressMap[i], value, dataType);
|
||||
}
|
||||
if (ReturnValueDictionary != null)
|
||||
{
|
||||
var dataReturn = new DataReturnDef();
|
||||
dataReturn.MachineId = machineName;
|
||||
dataReturn.ReturnValues = new ReturnStruct<Dictionary<string, ReturnUnit<double>>>() { IsSuccess = true, Datas = returnDic };
|
||||
ReturnValueDictionary(dataReturn);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
//_logger.LogError(ex, "Error");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -190,5 +191,10 @@ namespace Modbus.Net.Modbus
|
||||
else return null;
|
||||
};
|
||||
}
|
||||
|
||||
public async Task<bool> ConnectAsync()
|
||||
{
|
||||
return await _receiver.ConnectAsync();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -30,8 +30,8 @@ namespace Modbus.Net
|
||||
var root = configuration.GetSection("Modbus.Net").GetSection(machineSection).GetChildren();
|
||||
foreach (var machine in root)
|
||||
{
|
||||
List<KeyValuePair<string, string>> kv = new List<KeyValuePair<string, string>>();
|
||||
Dictionary<string, string> dic = new Dictionary<string, string>();
|
||||
var kv = new List<KeyValuePair<string, string>>();
|
||||
var dic = new Dictionary<string, string>();
|
||||
foreach (var paramO in machine.GetChildren())
|
||||
{
|
||||
foreach (var param in paramO.GetChildren())
|
||||
@@ -47,8 +47,11 @@ namespace Modbus.Net
|
||||
switch (param.Key)
|
||||
{
|
||||
case "protocol":
|
||||
{
|
||||
if (dic["type"] != null && dic["type"] != "")
|
||||
{
|
||||
paramsSet.Add(Enum.Parse(Assembly.Load("Modbus.Net." + dic["protocol"]).GetType("Modbus.Net." + dic["protocol"] + "." + dic["protocol"] + "Type"), dic["type"]));
|
||||
}
|
||||
break;
|
||||
}
|
||||
case "type":
|
||||
@@ -84,23 +87,7 @@ namespace Modbus.Net
|
||||
default:
|
||||
{
|
||||
string value = param.Value;
|
||||
bool boolValue;
|
||||
byte byteValue;
|
||||
if (!bool.TryParse(value, out boolValue))
|
||||
{
|
||||
if (!byte.TryParse(value, out byteValue))
|
||||
{
|
||||
paramsSet.Add(value);
|
||||
}
|
||||
else
|
||||
{
|
||||
paramsSet.Add(byteValue);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
paramsSet.Add(boolValue);
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -109,7 +96,23 @@ namespace Modbus.Net
|
||||
Type machineType = Assembly.Load("Modbus.Net." + dic["protocol"]).GetType("Modbus.Net." + dic["protocol"] + "." + dic["protocol"] + "Machine`2");
|
||||
Type[] typeParams = new Type[] { typeof(string), typeof(string) };
|
||||
Type constructedType = machineType.MakeGenericType(typeParams);
|
||||
IMachine<string> machineInstance = Activator.CreateInstance(constructedType, paramsSet.ToArray()) as IMachine<string>;
|
||||
var constructorParams = constructedType.GetConstructors().First(p => p.GetParameters().Count() == paramsSet.Count()).GetParameters();
|
||||
var constructorParamsEnumerator = constructorParams.GetEnumerator();
|
||||
var paramsSetFinal = new List<object>();
|
||||
foreach(object paramSet in paramsSet)
|
||||
{
|
||||
var moveNext = constructorParamsEnumerator.MoveNext();
|
||||
var constructor = constructorParamsEnumerator.Current as ParameterInfo;
|
||||
if (constructor.ParameterType != typeof(string) && paramSet.GetType() == typeof(string))
|
||||
{
|
||||
paramsSetFinal.Add(Convert.ChangeType(paramSet, constructor.ParameterType));
|
||||
}
|
||||
else
|
||||
{
|
||||
paramsSetFinal.Add(paramSet);
|
||||
}
|
||||
}
|
||||
IMachine<string> machineInstance = Activator.CreateInstance(constructedType, paramsSetFinal.ToArray()) as IMachine<string>;
|
||||
ans.Add(machineInstance);
|
||||
}
|
||||
return ans;
|
||||
|
||||
Reference in New Issue
Block a user