Skip to content

Commit

Permalink
Merge pull request #51 from WildernessLabs/feature/stepper-online
Browse files Browse the repository at this point in the history
expose base address as protected
  • Loading branch information
adrianstevens authored Jan 4, 2025
2 parents f228a4a + 9daebba commit 86f7fe8
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 13 deletions.
44 changes: 42 additions & 2 deletions src/Meadow.Modbus.Unit.Tests/V10x.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@

namespace Meadow.Modbus.Voltaic;

/// <summary>
/// Represents a Voltaic Systems V10x solar charge controller and battery
/// </summary>
public class V10x : ModbusPolledDevice
{
private double _rawBatteryVoltage;

Check warning on line 11 in src/Meadow.Modbus.Unit.Tests/V10x.cs

View workflow job for this annotation

GitHub Actions / build

Field 'V10x._rawBatteryVoltage' is never assigned to, and will always have its default value 0
Expand All @@ -13,17 +16,51 @@ public class V10x : ModbusPolledDevice
private double _rawEnvironmentTemp;
private double _rawControllerTemp;

Check warning on line 17 in src/Meadow.Modbus.Unit.Tests/V10x.cs

View workflow job for this annotation

GitHub Actions / build

Field 'V10x._rawControllerTemp' is never assigned to, and will always have its default value 0

private const ushort BatteryOutputSwitchRegister = 0;

/// <summary>
/// The default Modbus address for the V10x device.
/// </summary>
public const int DefaultModbusAddress = 1;
public const int DefaultBaudRate = 9600;

private const ushort BatteryOutputSwitchRegister = 0;
/// <summary>
/// The default baud rate for communication with the V10x device.
/// </summary>
public const int DefaultBaudRate = 9600;

/// <summary>
/// Gets the battery voltage.
/// </summary>
public Voltage BatteryVoltage => new Voltage(_rawBatteryVoltage, Voltage.UnitType.Volts);

/// <summary>
/// Gets the input voltage.
/// </summary>
public Voltage InputVoltage => new Voltage(_rawInputVoltage, Voltage.UnitType.Volts);

/// <summary>
/// Gets the input current.
/// </summary>
public Current InputCurrent => new Current(_rawInputCurrent, Current.UnitType.Amps);

/// <summary>
/// Gets the load voltage.
/// </summary>
public Voltage LoadVoltage => new Voltage(_rawLoadVoltage, Voltage.UnitType.Volts);

/// <summary>
/// Gets the load current.
/// </summary>
public Current LoadCurrent => new Current(_rawLoadCurrent, Current.UnitType.Amps);

/// <summary>
/// Gets the environment temperature.
/// </summary>
public Temperature EnvironmentTemp => new Temperature(_rawEnvironmentTemp, Temperature.UnitType.Celsius);

/// <summary>
/// Gets the controller temperature.
/// </summary>
public Temperature ControllerTemp => new Temperature(_rawControllerTemp, Temperature.UnitType.Celsius);

public V10x(
Expand Down Expand Up @@ -82,6 +119,9 @@ public V10x(
);
}

/// <summary>
/// Sets the battery output switch state.
/// </summary>
public bool BatteryOutput
{
set => _ = WriteCoil(BatteryOutputSwitchRegister, value);
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 @@ -20,7 +20,7 @@
</PropertyGroup>
<ItemGroup>
<None Include="icon.png" Pack="true" PackagePath="" />
<PackageReference Include="System.IO.Ports" Version="9.0.0" />
<PackageReference Include="System.IO.Ports" Version="8.0.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\..\Meadow.Contracts\Source\Meadow.Contracts\Meadow.Contracts.csproj" />
Expand Down
24 changes: 14 additions & 10 deletions src/Meadow.Modbus/ModbusPolledDevice.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,12 @@ private class RegisterMapping
/// </summary>
public static readonly TimeSpan DefaultRefreshPeriod = TimeSpan.FromSeconds(5);

/// <summary>
/// Gets or sets the device's address on the bus
/// </summary>
protected byte BusAddress { get; set; }

private ModbusClientBase _client;
private byte _address;
private Timer _timer;
private int _refreshPeriosMs;

Expand Down Expand Up @@ -109,7 +113,7 @@ public virtual void StopPolling()
public ModbusPolledDevice(ModbusClientBase client, byte modbusAddress, TimeSpan? refreshPeriod = null)
{
_client = client;
_address = modbusAddress;
BusAddress = modbusAddress;
_refreshPeriosMs = (int)(refreshPeriod ?? DefaultRefreshPeriod).TotalMilliseconds;
_timer = new Timer(RefreshTimerProc, null, -1, -1);
}
Expand All @@ -124,23 +128,23 @@ protected async Task WriteHoldingRegister(ushort startRegister, params ushort[]
{
if (data.Length == 1)
{
await _client.WriteHoldingRegister(_address, startRegister, data[0]);
await _client.WriteHoldingRegister(BusAddress, startRegister, data[0]);
}
else
{
await _client.WriteHoldingRegisters(_address, startRegister, data);
await _client.WriteHoldingRegisters(BusAddress, startRegister, data);
}
}

/// <summary>
/// Writes one or more values to the holding registers of the Modbus device.
/// Reads one or more values from the holding registers of the Modbus device.
/// </summary>
/// <param name="startRegister">The starting register address.</param>
/// <param name="count">The number of registers to read.</param>
/// <returns>A task representing the asynchronous write operation.</returns>
protected Task<ushort[]> ReadHoldingRegisters(ushort startRegister, int count)
{
return _client.ReadHoldingRegisters(_address, startRegister, count);
return _client.ReadHoldingRegisters(BusAddress, startRegister, count);
}

/// <summary>
Expand All @@ -150,7 +154,7 @@ protected Task<ushort[]> ReadHoldingRegisters(ushort startRegister, int count)
/// <returns>A task representing the asynchronous write operation.</returns>
protected async Task<bool> ReadCoil(ushort register)
{
var registers = await _client.ReadCoils(_address, register, 1);
var registers = await _client.ReadCoils(BusAddress, register, 1);
return registers[0];
}

Expand All @@ -162,7 +166,7 @@ protected async Task<bool> ReadCoil(ushort register)
/// <returns>A task representing the asynchronous write operation.</returns>
protected async Task WriteCoil(ushort register, bool value)
{
await _client.WriteCoil(_address, register, value);
await _client.WriteCoil(BusAddress, register, value);
}

/// <summary>
Expand Down Expand Up @@ -402,7 +406,7 @@ private async Task MoveHoldingRegistersToProperties()

try
{
data = await _client.ReadHoldingRegisters(_address, r.StartRegister, r.RegisterCount);
data = await _client.ReadHoldingRegisters(BusAddress, r.StartRegister, r.RegisterCount);
}
catch (TimeoutException)
{
Expand Down Expand Up @@ -448,7 +452,7 @@ private async Task MoveInputRegistersToProperties()

try
{
data = await _client.ReadInputRegisters(_address, r.StartRegister, r.RegisterCount);
data = await _client.ReadInputRegisters(BusAddress, r.StartRegister, r.RegisterCount);
}
catch (TimeoutException)
{
Expand Down

0 comments on commit 86f7fe8

Please sign in to comment.