Skip to content
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

Release 1.5.0 #36

Merged
merged 1 commit into from
Nov 29, 2023
Merged
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
42 changes: 28 additions & 14 deletions src/Meadow.Modbus/Clients/ModbusClientBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ namespace Meadow.Modbus;
/// </summary>
public abstract class ModbusClientBase : IModbusBusClient, IDisposable
{
private const int MaxRegisterReadCount = 125;
private const int MaxRegisterReadCount = 0x7d;
private const int MaxCoilReadCount = 0x7d0;
private const int MaxRegisterWriteCount = 0x7b;
private const int MaxCoilWriteCount = 0x7b0;

/// <summary>
/// Event triggered when the client is disconnected.
Expand Down Expand Up @@ -161,6 +164,11 @@ public async Task WriteHoldingRegister(byte modbusAddress, ushort register, usho
/// <param name="values">The values to write to the registers.</param>
public async Task WriteHoldingRegisters(byte modbusAddress, ushort startRegister, IEnumerable<ushort> values)
{
if (values.Count() > MaxRegisterWriteCount)
{
throw new ArgumentException($"A maximum of {MaxRegisterWriteCount} registers can be written at one time");
}

if (startRegister > 40000)
{
// holding registers are defined as starting at 40001, but the actual bus read doesn't use the address, but instead the offset
Expand Down Expand Up @@ -205,7 +213,7 @@ public async Task<float[]> ReadHoldingRegistersFloat(byte modbusAddress, ushort

for (int i = 0; i < values.Length; i++)
{
values[i] = ConvertUShortsToFloat(data[i * 2 + 1], data[i * 2]);
values[i] = ConvertUShortsToFloat(data[(i * 2) + 1], data[i * 2]);
}
return values;
}
Expand All @@ -219,15 +227,15 @@ public async Task<float[]> ReadHoldingRegistersFloat(byte modbusAddress, ushort
/// <returns>An array of ushort values representing the registers.</returns>
public async Task<ushort[]> ReadHoldingRegisters(byte modbusAddress, ushort startRegister, int registerCount)
{
if (registerCount > MaxRegisterReadCount) throw new ArgumentException($"A maximum of {MaxRegisterReadCount} registers can be retrieved at one time");

if (startRegister > 40000)
{
// holding registers are defined as starting at 40001, but the actual bus read doesn't use the address, but instead the offset
// we'll support th user passing in the definition either way
startRegister -= 40001;
}

if (registerCount > MaxRegisterReadCount) throw new ArgumentException($"A maximum of {MaxRegisterReadCount} registers can be retrieved at one time");

var message = GenerateReadMessage(modbusAddress, ModbusFunction.ReadHoldingRegister, startRegister, registerCount);
await _syncRoot.WaitAsync();

Expand All @@ -237,6 +245,7 @@ public async Task<ushort[]> ReadHoldingRegisters(byte modbusAddress, ushort star
{
await DeliverMessage(message);
result = await ReadResult(ModbusFunction.ReadHoldingRegister);
if (result.Length == 0) return Array.Empty<ushort>();
}
finally
{
Expand All @@ -246,7 +255,7 @@ public async Task<ushort[]> ReadHoldingRegisters(byte modbusAddress, ushort star
var registers = new ushort[registerCount];
for (var i = 0; i < registerCount; i++)
{
registers[i] = (ushort)((result[i * 2] << 8) | (result[i * 2 + 1]));
registers[i] = (ushort)((result[i * 2] << 8) | (result[(i * 2) + 1]));
}
return registers;
}
Expand Down Expand Up @@ -277,7 +286,7 @@ public async Task<ushort[]> ReadInputRegisters(byte modbusAddress, ushort startR
try
{
await DeliverMessage(message);
result = await ReadResult(ModbusFunction.ReadHoldingRegister);
result = await ReadResult(ModbusFunction.ReadInputRegister);
}
finally
{
Expand All @@ -287,7 +296,7 @@ public async Task<ushort[]> ReadInputRegisters(byte modbusAddress, ushort startR
var registers = new ushort[result.Length / 2];
for (var i = 0; i < registers.Length; i++)
{
registers[i] = (ushort)((result[i * 2] << 8) | (result[i * 2 + 1]));
registers[i] = (ushort)((result[i * 2] << 8) | (result[(i * 2) + 1]));
}
return registers;
}
Expand All @@ -314,6 +323,11 @@ public async Task WriteCoil(byte modbusAddress, ushort register, bool value)
/// <inheritdoc/>
public async Task WriteMultipleCoils(byte modbusAddress, ushort startRegister, IEnumerable<bool> values)
{
if (values.Count() > MaxCoilWriteCount)
{
throw new ArgumentException($"A maximum of {MaxCoilWriteCount} coils can be written at one time");
}

// Reduce bool value list to 8 bit byte array
ushort byteArrayLength = (ushort)((values.Count() / 8) + (ushort)((values.Count() % 8) > 0 ? 1 : 0)); // Calc # 8 bit bytes needed to TX
byte[] msgSegment = new byte[2 + 1 + byteArrayLength]; // StartAddr + coils + value bytes
Expand Down Expand Up @@ -345,7 +359,7 @@ public async Task WriteMultipleCoils(byte modbusAddress, ushort startRegister, I
/// <inheritdoc/>
public async Task<bool[]> ReadCoils(byte modbusAddress, ushort startCoil, int coilCount)
{
if (coilCount > MaxRegisterReadCount) throw new ArgumentException($"A maximum of {MaxRegisterReadCount} coils can be retrieved at one time");
if (coilCount > MaxCoilReadCount) throw new ArgumentException($"A maximum of {MaxCoilReadCount} coils can be retrieved at one time");

var message = GenerateReadMessage(modbusAddress, ModbusFunction.ReadCoil, startCoil, coilCount);
await _syncRoot.WaitAsync();
Expand All @@ -355,7 +369,7 @@ public async Task<bool[]> ReadCoils(byte modbusAddress, ushort startCoil, int co
try
{
await DeliverMessage(message);
result = await ReadResult(ModbusFunction.ReadHoldingRegister);
result = await ReadResult(ModbusFunction.ReadCoil);
}
finally
{
Expand Down Expand Up @@ -384,18 +398,18 @@ private float ConvertUShortsToFloat(ushort high, ushort low)
// Combine the high and low values into a single uint
uint input = (uint)(((high & 0x00FF) << 24) |
((high & 0xFF00) << 8) |
(low & 0x00FF) << 8 |
low >> 8);
((low & 0x00FF) << 8) |
(low >> 8));

// Get the sign bit
uint signBit = (input >> 31) & 1;
int sign = 1 - (int)(2 * signBit);
// Get the exponent bits
var exponentBits = ((input >> 23) & 0xFF);
var exponentBits = (input >> 23) & 0xFF;
var exponent = exponentBits - 127;
// Get the fraction
var fractionBits = (input & 0x7FFFFF);
var fraction = 1.0 + fractionBits / Math.Pow(2, 23);
var fractionBits = input & 0x7FFFFF;
var fraction = 1.0 + (fractionBits / Math.Pow(2, 23));

// get the value
return (float)(sign * fraction * Math.Pow(2, exponent));
Expand Down
9 changes: 9 additions & 0 deletions src/Meadow.Modbus/Clients/ModbusTcpClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,15 @@ public class ModbusTcpClient : ModbusClientBase, IDisposable
private ushort _transaction = 0;
private byte[] _responseBuffer = new byte[300]; // I think the max is 9 + 255, but this gives a little room

/// <summary>
/// Initializes a new instance of the <see cref="ModbusTcpClient"/> class using the specified destination address and port.
/// </summary>
/// <param name="destination">The destination address.</param>
public ModbusTcpClient(IPEndPoint destination)
: this(destination.Address, (short)destination.Port)
{
}

/// <summary>
/// Initializes a new instance of the <see cref="ModbusTcpClient"/> class using the specified destination address and port.
/// </summary>
Expand Down
2 changes: 1 addition & 1 deletion src/Meadow.Modbus/Meadow.Modbus.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,6 @@
<PackageReference Include="System.IO.Ports" Version="6.0.0" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Meadow.Contracts" Version="1.4.0.3" />
<PackageReference Include="Meadow.Contracts" Version="1.5.0" />
</ItemGroup>
</Project>
Loading