|
|
|
@ -1,34 +1,96 @@ |
|
|
|
using Common.Logging; |
|
|
|
using Common.Logging; |
|
|
|
|
|
|
|
using MultiTerm.Protocols.Model; |
|
|
|
using MultiTerm.Protocols.Types; |
|
|
|
using MultiTerm.Protocols.Types; |
|
|
|
|
|
|
|
using RJCP.IO.Ports; |
|
|
|
|
|
|
|
using System.Text; |
|
|
|
|
|
|
|
|
|
|
|
namespace MultiTerm.Protocols.Serial; |
|
|
|
namespace MultiTerm.Protocols.Serial; |
|
|
|
|
|
|
|
|
|
|
|
public class SerialProtocol : CommunicationProtocol |
|
|
|
public class SerialProtocol : CommunicationProtocol |
|
|
|
{ |
|
|
|
{ |
|
|
|
public override ProtocolType ProtocolType => ProtocolType.Serial; |
|
|
|
public override ProtocolType ProtocolType => ProtocolType.Serial; |
|
|
|
|
|
|
|
private ISerialProtocolSettings? serialSettings; |
|
|
|
|
|
|
|
private SerialPortStream serialPort = new(); |
|
|
|
|
|
|
|
|
|
|
|
public SerialProtocol(ILogger logger) : base(logger) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
public SerialProtocol(ILogger logger) : base(logger) { } |
|
|
|
|
|
|
|
|
|
|
|
protected override bool InternalConnect() |
|
|
|
protected override bool InternalConnect(IProtocolSettings protocolSettings) |
|
|
|
{ |
|
|
|
{ |
|
|
|
throw new NotImplementedException(); |
|
|
|
// 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() |
|
|
|
protected override void InternalDisconnect() |
|
|
|
{ |
|
|
|
{ |
|
|
|
throw new NotImplementedException(); |
|
|
|
serialPort.Close(); |
|
|
|
|
|
|
|
this.serialSettings = null; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
protected override void InternalRead(CancellationToken ct) |
|
|
|
protected override void InternalRead(CancellationToken ct) |
|
|
|
{ |
|
|
|
{ |
|
|
|
throw new NotImplementedException(); |
|
|
|
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) |
|
|
|
protected override void InternalSendBytes(byte[] bytes) |
|
|
|
{ |
|
|
|
{ |
|
|
|
throw new NotImplementedException(); |
|
|
|
foreach (byte b in bytes) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
serialPort.WriteByte(b); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static IEnumerable<string> GetPortNames() |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
return SerialPortStream.GetPortNames(); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|