You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
96 lines
3.1 KiB
96 lines
3.1 KiB
using Common.Logging;
|
|
using MultiTerm.Protocols.Model;
|
|
using MultiTerm.Protocols.Types;
|
|
using RJCP.IO.Ports;
|
|
using System.Text;
|
|
|
|
namespace MultiTerm.Protocols.Serial;
|
|
|
|
public class SerialProtocol : CommunicationProtocol
|
|
{
|
|
public override ProtocolType ProtocolType => ProtocolType.Serial;
|
|
private ISerialProtocolSettings? serialSettings;
|
|
private SerialPortStream serialPort = new();
|
|
|
|
|
|
public SerialProtocol(ILogger logger) : base(logger) { }
|
|
|
|
protected override bool InternalConnect(IProtocolSettings protocolSettings)
|
|
{
|
|
// check if settings are of correct type
|
|
if (protocolSettings is not ISerialProtocolSettings serialProtocolSettings)
|
|
{
|
|
this.serialSettings = null;
|
|
throw new ArgumentException($"Cannot connect due to wrong type of Protocol Settings. " +
|
|
$"Check parameter {nameof(protocolSettings)}' of '{nameof(InternalConnect)}()' in {nameof(SerialProtocol)}.");
|
|
}
|
|
|
|
// store locally
|
|
this.serialSettings = serialProtocolSettings;
|
|
|
|
// create new serial port
|
|
this.serialPort = new()
|
|
{
|
|
// apply user settings, where needed converters are used
|
|
PortName = this.serialSettings.PortName,
|
|
BaudRate = this.serialSettings.BaudRate,
|
|
DataBits = this.serialSettings.DataBits,
|
|
Parity = new ParityLibraryEquivalentConverter().ConvertToLibraryType(this.serialSettings.Parity),
|
|
StopBits = new StopBitsLibraryEquivalentConverter().ConvertToLibraryType(this.serialSettings.StopBits),
|
|
Handshake = new HandshakeLibraryEquivalentConverter().ConvertToLibraryType(this.serialSettings.Handshake),
|
|
|
|
// define static settings
|
|
Encoding = Encoding.Unicode,
|
|
ReadTimeout = 500,
|
|
WriteTimeout = 500
|
|
};
|
|
|
|
// try opening serial port
|
|
try
|
|
{
|
|
serialPort.Open();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
this.logger.LogException(ex, $"'{nameof(InternalConnect)}()'Opening serial port failed:", nameof(SerialProtocol));
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
protected override void InternalDisconnect()
|
|
{
|
|
serialPort.Close();
|
|
this.serialSettings = null;
|
|
}
|
|
|
|
protected override void InternalRead(CancellationToken ct)
|
|
{
|
|
while(ct.IsCancellationRequested == false)
|
|
{
|
|
// reads character based on configured encoding (here Unicode)
|
|
int readCharacter = serialPort.ReadChar();
|
|
if (readCharacter != -1) // -1 = timeout
|
|
{
|
|
// create extended char type
|
|
var character = new ExtendedChar((char)readCharacter);
|
|
|
|
// report new data with event
|
|
this.OnReceivedData(new ReceivedDataEventArgs(new ExtendedChar[] { character }));
|
|
}
|
|
}
|
|
}
|
|
|
|
protected override void InternalSendBytes(byte[] bytes)
|
|
{
|
|
foreach (byte b in bytes)
|
|
{
|
|
serialPort.WriteByte(b);
|
|
}
|
|
}
|
|
|
|
public static IEnumerable<string> GetPortNames()
|
|
{
|
|
return SerialPortStream.GetPortNames();
|
|
}
|
|
}
|
|
|