Skip to content

ModbusTCP 批量读取可以按位读取(例如 “10.0....10.15”) #45

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion IoTClient/Clients/Modbus/Base/ModbusSerialBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -726,7 +726,7 @@ private Result<List<ModbusOutput>> BatchRead(List<ModbusInput> addresses)
{
result.Value.Add(new ModbusOutput()
{
Address = item.Key,
Address = item.Key.ToString(),
FunctionCode = functionCode,
StationNumber = stationNumber,
Value = item.Value
Expand Down
2 changes: 1 addition & 1 deletion IoTClient/Clients/Modbus/ModbusRtuOverTcpClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -827,7 +827,7 @@ private Result<List<ModbusOutput>> BatchRead(List<ModbusInput> addresses)
{
result.Value.Add(new ModbusOutput()
{
Address = item.Key,
Address = item.Key.ToString(),
FunctionCode = functionCode,
StationNumber = stationNumber,
Value = item.Value
Expand Down
69 changes: 62 additions & 7 deletions IoTClient/Clients/Modbus/ModbusTcpClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1071,12 +1071,11 @@ public Result<List<ModbusOutput>> BatchRead(List<ModbusInput> addresses, uint re
return result;
}

private Result<Dictionary<int, object>> BatchRead(Dictionary<string, DataTypeEnum> addressList, byte stationNumber, byte functionCode)
private Result<Dictionary<string, object>> BatchRead(Dictionary<string, DataTypeEnum> addressList, byte stationNumber, byte functionCode)
{
var result = new Result<Dictionary<int, object>>();
result.Value = new Dictionary<int, object>();

var addresses = addressList.Select(t => new KeyValuePair<int, DataTypeEnum>(int.Parse(t.Key), t.Value)).ToList();
var result = new Result<Dictionary<string, object>>();
result.Value = new Dictionary<string, object>();
var addresses = addressList.Select(m => new { Key = m.Key.Contains('.') ? m.Key.Split('.')[0] : m.Key, Value = m.Key.Contains('.') ? DataTypeEnum.Int16 : m.Value }).GroupBy(m => (m.Key, m.Value)).Select(t => new KeyValuePair<int, DataTypeEnum>(int.Parse(t.Key.Key), t.Key.Value)).ToList();

var minAddress = addresses.Select(t => t.Key).Min();
var maxAddress = addresses.Select(t => t.Key).Max();
Expand Down Expand Up @@ -1167,14 +1166,70 @@ private Result<Dictionary<int, object>> BatchRead(Dictionary<string, DataTypeEnu
throw new Exception("Err BatchRead 未定义类型 -3");
}

result.Value.Add(item.Key, tempVaue);
result.Value.Add(item.Key.ToString(), tempVaue);
}
minAddress = minAddress + readLength;

if (addresses.Any(t => t.Key >= minAddress))
minAddress = addresses.Where(t => t.Key >= minAddress).OrderBy(t => t.Key).FirstOrDefault().Key;
else
return result.EndTime();
{
var xx = result.Value;
var newResult = new Result<Dictionary<string, object>>();
newResult.Value = new Dictionary<string, object>();
foreach (var c in addressList)
{
if (c.Key.Contains('.'))
{
int bit = int.Parse(c.Key.Split('.')[1]);
int address = int.Parse(c.Key.Split('.')[0]);
Int16 value = (Int16)result.Value[address.ToString()];
var binaryArray = DataConvert.IntToBinaryArray(value, 16);
var realvalue = int.Parse(binaryArray[15 - bit].ToString());
switch (c.Value)
{
case DataTypeEnum.Bool:
newResult.Value.Add(c.Key, Convert.ToBoolean(realvalue));
break;
case DataTypeEnum.Byte:
throw new Exception("Err BatchRead 未定义类型 -2");
case DataTypeEnum.Int16:
newResult.Value.Add(c.Key, Convert.ToInt16(realvalue));
break;
case DataTypeEnum.UInt16:
newResult.Value.Add(c.Key, Convert.ToUInt16(realvalue));
break;
case DataTypeEnum.Int32:
newResult.Value.Add(c.Key, Convert.ToInt32(realvalue));
break;
case DataTypeEnum.UInt32:
newResult.Value.Add(c.Key, Convert.ToUInt32(realvalue));
break;
case DataTypeEnum.Int64:
newResult.Value.Add(c.Key, Convert.ToInt64(realvalue));
break;
case DataTypeEnum.UInt64:
newResult.Value.Add(c.Key, Convert.ToUInt64(realvalue));
break;
case DataTypeEnum.Float:
newResult.Value.Add(c.Key, Convert.ToSingle(realvalue));
break;
case DataTypeEnum.Double:
newResult.Value.Add(c.Key, Convert.ToDouble(realvalue));
break;
default:
throw new Exception("Err BatchRead 未定义类型 -3");
}

}
else
{
newResult.Value.Add(c.Key, result.Value[c.Key]);
}
}
return newResult.EndTime();
}
return result.EndTime();
}
return result.EndTime();
}
Expand Down
2 changes: 1 addition & 1 deletion IoTClient/Clients/Modbus/Models/ModBusOutput.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public class ModbusOutput
/// <summary>
/// 地址
/// </summary>
public int Address { get; set; }
public string Address { get; set; }
/// <summary>
/// 站号
/// </summary>
Expand Down