Updated serial implementations
This commit is contained in:
@@ -16,7 +16,7 @@ namespace AMWD.Protocols.Modbus.Serial
|
||||
/// </summary>
|
||||
/// <param name="portName">The name of the serial port to use.</param>
|
||||
public ModbusSerialClient(string portName)
|
||||
: this(new ModbusSerialConnection { PortName = portName })
|
||||
: this(new ModbusSerialConnection(portName))
|
||||
{ }
|
||||
|
||||
/// <summary>
|
||||
@@ -41,8 +41,8 @@ namespace AMWD.Protocols.Modbus.Serial
|
||||
Protocol = new RtuProtocol();
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="SerialPort.GetPortNames" />
|
||||
public static string[] AvailablePortNames => SerialPort.GetPortNames();
|
||||
/// <inheritdoc cref="ModbusSerialConnection.AvailablePortNames" />
|
||||
public static string[] AvailablePortNames => ModbusSerialConnection.AvailablePortNames;
|
||||
|
||||
/// <inheritdoc cref="IModbusConnection.IdleTimeout"/>
|
||||
public TimeSpan IdleTimeout
|
||||
|
||||
@@ -39,10 +39,15 @@ namespace AMWD.Protocols.Modbus.Serial
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="ModbusSerialConnection"/> class.
|
||||
/// </summary>
|
||||
public ModbusSerialConnection()
|
||||
public ModbusSerialConnection(string portName)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(portName))
|
||||
throw new ArgumentNullException(nameof(portName));
|
||||
|
||||
_serialPort = new SerialPortWrapper
|
||||
{
|
||||
PortName = portName,
|
||||
|
||||
BaudRate = (int)BaudRate.Baud19200,
|
||||
DataBits = 8,
|
||||
Handshake = Handshake.None,
|
||||
@@ -59,6 +64,9 @@ namespace AMWD.Protocols.Modbus.Serial
|
||||
|
||||
#region Properties
|
||||
|
||||
/// <inheritdoc cref="SerialPort.GetPortNames" />
|
||||
public static string[] AvailablePortNames => SerialPort.GetPortNames();
|
||||
|
||||
/// <inheritdoc/>
|
||||
public string Name => "Serial";
|
||||
|
||||
@@ -68,20 +76,6 @@ namespace AMWD.Protocols.Modbus.Serial
|
||||
/// <inheritdoc/>
|
||||
public virtual TimeSpan ConnectTimeout { get; set; } = TimeSpan.MaxValue;
|
||||
|
||||
/// <inheritdoc/>
|
||||
public virtual TimeSpan ReadTimeout
|
||||
{
|
||||
get => TimeSpan.FromMilliseconds(_serialPort.ReadTimeout);
|
||||
set => _serialPort.ReadTimeout = (int)value.TotalMilliseconds;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public virtual TimeSpan WriteTimeout
|
||||
{
|
||||
get => TimeSpan.FromMilliseconds(_serialPort.WriteTimeout);
|
||||
set => _serialPort.WriteTimeout = (int)value.TotalMilliseconds;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets a value indicating whether the RS485 driver has to be enabled via software switch.
|
||||
/// </summary>
|
||||
@@ -171,6 +165,20 @@ namespace AMWD.Protocols.Modbus.Serial
|
||||
set => _serialPort.StopBits = value;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public virtual TimeSpan ReadTimeout
|
||||
{
|
||||
get => TimeSpan.FromMilliseconds(_serialPort.ReadTimeout);
|
||||
set => _serialPort.ReadTimeout = (int)value.TotalMilliseconds;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public virtual TimeSpan WriteTimeout
|
||||
{
|
||||
get => TimeSpan.FromMilliseconds(_serialPort.WriteTimeout);
|
||||
set => _serialPort.WriteTimeout = (int)value.TotalMilliseconds;
|
||||
}
|
||||
|
||||
#endregion SerialPort Properties
|
||||
|
||||
#endregion Properties
|
||||
|
||||
@@ -20,6 +20,39 @@ namespace AMWD.Protocols.Modbus.Serial.Utils
|
||||
|
||||
#endregion Fields
|
||||
|
||||
#region Constructor
|
||||
|
||||
public SerialPortWrapper()
|
||||
{
|
||||
_serialPort.DataReceived += OnDataReceived;
|
||||
_serialPort.PinChanged += OnPinChanged;
|
||||
_serialPort.ErrorReceived += OnErrorReceived;
|
||||
}
|
||||
|
||||
#endregion Constructor
|
||||
|
||||
#region Events
|
||||
|
||||
/// <inheritdoc cref="SerialPort.DataReceived"/>
|
||||
public virtual event SerialDataReceivedEventHandler DataReceived;
|
||||
|
||||
/// <inheritdoc cref="SerialPort.PinChanged"/>
|
||||
public virtual event SerialPinChangedEventHandler PinChanged;
|
||||
|
||||
/// <inheritdoc cref="SerialPort.ErrorReceived"/>
|
||||
public virtual event SerialErrorReceivedEventHandler ErrorReceived;
|
||||
|
||||
private void OnDataReceived(object sender, SerialDataReceivedEventArgs e)
|
||||
=> DataReceived?.Invoke(sender, e);
|
||||
|
||||
private void OnPinChanged(object sender, SerialPinChangedEventArgs e)
|
||||
=> PinChanged?.Invoke(sender, e);
|
||||
|
||||
private void OnErrorReceived(object sender, SerialErrorReceivedEventArgs e)
|
||||
=> ErrorReceived?.Invoke(sender, e);
|
||||
|
||||
#endregion Events
|
||||
|
||||
#region Properties
|
||||
|
||||
/// <inheritdoc cref="SerialPort.Handshake"/>
|
||||
@@ -82,6 +115,10 @@ namespace AMWD.Protocols.Modbus.Serial.Utils
|
||||
set => _serialPort.Parity = value;
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="SerialPort.BytesToWrite"/>
|
||||
public virtual int BytesToWrite
|
||||
=> _serialPort.BytesToWrite;
|
||||
|
||||
/// <inheritdoc cref="SerialPort.BaudRate"/>
|
||||
public virtual int BaudRate
|
||||
{
|
||||
@@ -89,6 +126,10 @@ namespace AMWD.Protocols.Modbus.Serial.Utils
|
||||
set => _serialPort.BaudRate = value;
|
||||
}
|
||||
|
||||
/// <inheritdoc cref="SerialPort.BytesToRead"/>
|
||||
public virtual int BytesToRead
|
||||
=> _serialPort.BytesToRead;
|
||||
|
||||
#endregion Properties
|
||||
|
||||
#region Methods
|
||||
@@ -101,6 +142,14 @@ namespace AMWD.Protocols.Modbus.Serial.Utils
|
||||
public virtual void Open()
|
||||
=> _serialPort.Open();
|
||||
|
||||
/// <inheritdoc cref="SerialPort.Read(byte[], int, int)"/>
|
||||
public virtual int Read(byte[] buffer, int offset, int count)
|
||||
=> _serialPort.Read(buffer, offset, count);
|
||||
|
||||
/// <inheritdoc cref="SerialPort.Write(byte[], int, int)"/>
|
||||
public virtual void Write(byte[] buffer, int offset, int count)
|
||||
=> _serialPort.Write(buffer, offset, count);
|
||||
|
||||
/// <inheritdoc cref="SerialPort.Dispose"/>
|
||||
public virtual void Dispose()
|
||||
=> _serialPort.Dispose();
|
||||
@@ -117,7 +166,7 @@ namespace AMWD.Protocols.Modbus.Serial.Utils
|
||||
/// <remarks>
|
||||
/// There seems to be a bug with the async stream implementation on Windows.
|
||||
/// <br/>
|
||||
/// See this StackOverflow answer: <see href="https://stackoverflow.com/a/54610437/11906695" />
|
||||
/// See this StackOverflow answer: <see href="https://stackoverflow.com/a/54610437/11906695" />.
|
||||
/// </remarks>
|
||||
/// <param name="buffer">The buffer to write the data into.</param>
|
||||
/// <param name="offset">The byte offset in buffer at which to begin writing data from the serial port.</param>
|
||||
|
||||
Reference in New Issue
Block a user