Implementation of the basic functionallity

This commit is contained in:
2024-02-06 19:47:06 +01:00
parent a6c7828fbe
commit f31f6f94ff
42 changed files with 6875 additions and 11 deletions

View File

@@ -0,0 +1,28 @@
namespace AMWD.Protocols.Modbus.Common
{
/// <summary>
/// Represents a coil.
/// </summary>
public class Coil : ModbusObject
{
/// <inheritdoc/>
public override ModbusObjectType Type => ModbusObjectType.Coil;
/// <summary>
/// Gets or sets a value indicating whether the coil is on or off.
/// </summary>
public bool Value
{
get => HighByte == 0xFF;
set
{
HighByte = (byte)(value ? 0xFF : 0x00);
LowByte = 0x00;
}
}
/// <inheritdoc/>
public override string ToString()
=> $"Coil #{Address} | {(Value ? "ON" : "OFF")}";
}
}

View File

@@ -0,0 +1,20 @@
namespace AMWD.Protocols.Modbus.Common
{
/// <summary>
/// Represents a discrete input.
/// </summary>
public class DiscreteInput : ModbusObject
{
/// <inheritdoc/>
public override ModbusObjectType Type => ModbusObjectType.DiscreteInput;
/// <summary>
/// Gets or sets a value indicating whether the discrete input is on or off.
/// </summary>
public bool Value => HighByte == 0xFF;
/// <inheritdoc/>
public override string ToString()
=> $"Discrete Input #{Address} | {(Value ? "ON" : "OFF")}";
}
}

View File

@@ -0,0 +1,41 @@
using System;
namespace AMWD.Protocols.Modbus.Common
{
/// <summary>
/// Represents a holding register.
/// </summary>
public class HoldingRegister : ModbusObject
{
/// <inheritdoc/>
public override ModbusObjectType Type => ModbusObjectType.HoldingRegister;
/// <summary>
/// Gets or sets the value of the holding register.
/// </summary>
public ushort Value
{
get
{
byte[] blob = [HighByte, LowByte];
if (BitConverter.IsLittleEndian)
Array.Reverse(blob);
return BitConverter.ToUInt16(blob, 0);
}
set
{
byte[] blob = BitConverter.GetBytes(value);
if (BitConverter.IsLittleEndian)
Array.Reverse(blob);
HighByte = blob[0];
LowByte = blob[1];
}
}
/// <inheritdoc/>
public override string ToString()
=> $"Holding Register #{Address} | {Value} | HI: {HighByte:X2}, LO: {LowByte:X2}";
}
}

View File

@@ -0,0 +1,32 @@
using System;
namespace AMWD.Protocols.Modbus.Common
{
/// <summary>
/// Represents a input register.
/// </summary>
public class InputRegister : ModbusObject
{
/// <inheritdoc/>
public override ModbusObjectType Type => ModbusObjectType.InputRegister;
/// <summary>
/// Gets or sets the value of the input register.
/// </summary>
public ushort Value
{
get
{
byte[] blob = [HighByte, LowByte];
if (BitConverter.IsLittleEndian)
Array.Reverse(blob);
return BitConverter.ToUInt16(blob, 0);
}
}
/// <inheritdoc/>
public override string ToString()
=> $"Input Register #{Address} | {Value} | HI: {HighByte:X2}, LO: {LowByte:X2}";
}
}

View File

@@ -0,0 +1,56 @@
using System;
namespace AMWD.Protocols.Modbus.Common
{
/// <summary>
/// Represents the base of all Modbus specific objects.
/// </summary>
public abstract class ModbusObject
{
/// <summary>
/// Gets the type of the object.
/// </summary>
public abstract ModbusObjectType Type { get; }
/// <summary>
/// Gets or sets the address of the object.
/// </summary>
public ushort Address { get; set; }
/// <summary>
/// Gets or sets the high byte of the value.
/// </summary>
public byte HighByte { get; set; }
/// <summary>
/// Gets or sets the low byte of the value.
/// </summary>
public byte LowByte { get; set; }
/// <inheritdoc/>
public override bool Equals(object obj)
{
if (obj is not ModbusObject mo)
return false;
return Type == mo.Type
&& Address == mo.Address
&& HighByte == mo.HighByte
&& LowByte == mo.LowByte;
}
/// <inheritdoc/>
[System.Diagnostics.CodeAnalysis.ExcludeFromCodeCoverage]
public override int GetHashCode()
{
#if NET6_0_OR_GREATER
return HashCode.Combine(Type, Address, HighByte, LowByte);
#else
return Type.GetHashCode()
^ Address.GetHashCode()
^ HighByte.GetHashCode()
^ LowByte.GetHashCode();
#endif
}
}
}