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

zero warnings! #28

Merged
merged 1 commit into from
Oct 6, 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
27 changes: 27 additions & 0 deletions src/Meadow.Modbus/Clients/Extensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
namespace Meadow.Modbus;

/// <summary>
/// Extension methods for Modbus functions
/// </summary>
public static class Extensions
{
/// <summary>
/// Converts a set of Modbus registers (ushort[]) to integers (int[]) assuming little-endina ordering
/// </summary>
/// <param name="registers"></param>
/// <returns></returns>
public static int[] ConvertRegistersToInt32(this ushort[] registers)
{
var values = new int[registers.Length / 2];

var index = 0;
var i = 0;
// Need to byte swap
for (; i < registers.Length; i += 2, index++)
{
values[index] = ((registers[i] << 8) | (registers[i] >> 8) | (registers[i + 1] << 8) | (registers[i + 1] >> 8));
}

return values;
}
}
163 changes: 81 additions & 82 deletions src/Meadow.Modbus/Clients/IModbusBusClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,100 +2,99 @@
using System.Collections.Generic;
using System.Threading.Tasks;

namespace Meadow.Modbus
namespace Meadow.Modbus;

/// <summary>
/// Interface for a Modbus bus client.
/// </summary>
public interface IModbusBusClient
{
/// <summary>
/// Interface for a Modbus bus client.
/// Event that is raised when the client is disconnected.
/// </summary>
public interface IModbusBusClient
{
/// <summary>
/// Event that is raised when the client is disconnected.
/// </summary>
event EventHandler Disconnected;
event EventHandler Disconnected;

/// <summary>
/// Event that is raised when the client is connected.
/// </summary>
event EventHandler Connected;
/// <summary>
/// Event that is raised when the client is connected.
/// </summary>
event EventHandler Connected;

/// <summary>
/// Gets a value indicating whether the client is connected.
/// </summary>
public bool IsConnected { get; }
/// <summary>
/// Gets a value indicating whether the client is connected.
/// </summary>
public bool IsConnected { get; }

/// <summary>
/// Asynchronously connects the client.
/// </summary>
/// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
Task Connect();
/// <summary>
/// Asynchronously connects the client.
/// </summary>
/// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
Task Connect();

/// <summary>
/// Disconnects the client.
/// </summary>
void Disconnect();
/// <summary>
/// Disconnects the client.
/// </summary>
void Disconnect();

/// <summary>
/// Writes a single value to the given register on a device
/// </summary>
/// <param name="modbusAddress"></param>
/// <param name="register"></param>
/// <param name="value"></param>
Task WriteHoldingRegister(byte modbusAddress, ushort register, ushort value);
/// <summary>
/// Writes a single value to the given register on a device
/// </summary>
/// <param name="modbusAddress"></param>
/// <param name="register"></param>
/// <param name="value"></param>
Task WriteHoldingRegister(byte modbusAddress, ushort register, ushort value);

/// <summary>
/// Writes multiple values to holding registers (modbus function 16)
/// </summary>
/// <param name="modbusAddress">The target device modbus address</param>
/// <param name="startRegister">The first register to begin writing</param>
/// <param name="values">The registers (16-bit values) to write</param>
/// <returns></returns>
Task WriteHoldingRegisters(byte modbusAddress, ushort startRegister, IEnumerable<ushort> values);
/// <summary>
/// Writes multiple values to holding registers (modbus function 16)
/// </summary>
/// <param name="modbusAddress">The target device modbus address</param>
/// <param name="startRegister">The first register to begin writing</param>
/// <param name="values">The registers (16-bit values) to write</param>
/// <returns></returns>
Task WriteHoldingRegisters(byte modbusAddress, ushort startRegister, IEnumerable<ushort> values);

/// <summary>
/// Reads the requested number of holding registers from a device
/// </summary>
/// <param name="modbusAddress"></param>
/// <param name="startRegister"></param>
/// <param name="registerCount"></param>
/// <returns></returns>
Task<ushort[]> ReadHoldingRegisters(byte modbusAddress, ushort startRegister, int registerCount);
/// <summary>
/// Reads the requested number of holding registers from a device
/// </summary>
/// <param name="modbusAddress"></param>
/// <param name="startRegister"></param>
/// <param name="registerCount"></param>
/// <returns></returns>
Task<ushort[]> ReadHoldingRegisters(byte modbusAddress, ushort startRegister, int registerCount);

/// <summary>
/// Reads the requested number of floats from the holding registers
/// Each float is two sequential registers
/// </summary>
/// <param name="modbusAddress"></param>
/// <param name="startRegister"></param>
/// <param name="floatCount"></param>
/// <returns></returns>
Task<float[]> ReadHoldingRegistersFloat(byte modbusAddress, ushort startRegister, int floatCount);
/// <summary>
/// Reads the requested number of floats from the holding registers
/// Each float is two sequential registers
/// </summary>
/// <param name="modbusAddress"></param>
/// <param name="startRegister"></param>
/// <param name="floatCount"></param>
/// <returns></returns>
Task<float[]> ReadHoldingRegistersFloat(byte modbusAddress, ushort startRegister, int floatCount);

/// <summary>
/// Writes a coil value to the given register on a device.
/// </summary>
/// <param name="modbusAddress">The Modbus address of the device.</param>
/// <param name="register">The register to write to.</param>
/// <param name="value">The value to write.</param>
/// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
Task WriteCoil(byte modbusAddress, ushort register, bool value);
/// <summary>
/// Writes a coil value to the given register on a device.
/// </summary>
/// <param name="modbusAddress">The Modbus address of the device.</param>
/// <param name="register">The register to write to.</param>
/// <param name="value">The value to write.</param>
/// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
Task WriteCoil(byte modbusAddress, ushort register, bool value);

/// <summary>
/// Writes multiple coil values to the given registers on a device.
/// </summary>
/// <param name="modbusAddress">The Modbus address of the device.</param>
/// <param name="startRegister">The first register to begin writing.</param>
/// <param name="values">The coil values to write.</param>
/// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
Task WriteMultipleCoils(byte modbusAddress, ushort startRegister, IEnumerable<bool> values);
/// <summary>
/// Writes multiple coil values to the given registers on a device.
/// </summary>
/// <param name="modbusAddress">The Modbus address of the device.</param>
/// <param name="startRegister">The first register to begin writing.</param>
/// <param name="values">The coil values to write.</param>
/// <returns>A <see cref="Task"/> representing the asynchronous operation.</returns>
Task WriteMultipleCoils(byte modbusAddress, ushort startRegister, IEnumerable<bool> values);

/// <summary>
/// Reads the requested number of coils from a device.
/// </summary>
/// <param name="modbusAddress">The Modbus address of the device.</param>
/// <param name="startCoil">The first coil to begin reading.</param>
/// <param name="coilCount">The number of coils to read.</param>
/// <returns>An array of coil values read from the device.</returns>
Task<bool[]> ReadCoils(byte modbusAddress, ushort startCoil, int coilCount);
}
/// <summary>
/// Reads the requested number of coils from a device.
/// </summary>
/// <param name="modbusAddress">The Modbus address of the device.</param>
/// <param name="startCoil">The first coil to begin reading.</param>
/// <param name="coilCount">The number of coils to read.</param>
/// <returns>An array of coil values read from the device.</returns>
Task<bool[]> ReadCoils(byte modbusAddress, ushort startCoil, int coilCount);
}
Loading
Loading