139 lines
3.8 KiB
C#
139 lines
3.8 KiB
C#
namespace Common.Hardware.Interfaces.Ports.SIRT
|
|
{
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
|
|
public class SIRTPort : SerialPort
|
|
{
|
|
private readonly List<Byte> buffer;
|
|
|
|
public SIRTPort(String serialPortName) : base(new SerialPortSettings
|
|
{
|
|
BaudRate = SerialPortBaudRate.X115200,
|
|
DataBits = SerialPortDataBits.X8,
|
|
Parity = SerialPortParity.None,
|
|
PortName = serialPortName,
|
|
StopBits = SerialPortStopBits.One
|
|
}) => this.buffer = new List<Byte>();
|
|
|
|
public event Action<SIRTMessage> MessageReceived;
|
|
|
|
public override void Open()
|
|
{
|
|
base.Open();
|
|
|
|
Task.Factory
|
|
.StartNew(this.StartParsing, this, CancellationToken.None, TaskCreationOptions.LongRunning, TaskScheduler.Default)
|
|
.ContinueWith(task =>
|
|
{
|
|
if (task.Exception != null)
|
|
{
|
|
this.NotifyInfoMessage(task.Exception.ToString());
|
|
}
|
|
|
|
this.Close();
|
|
});
|
|
|
|
this.Write(new SIRTMessage()
|
|
{
|
|
Direction = 0x84,
|
|
P2 = 0x04,
|
|
Address = new Byte[] { 0x20, 0x00, 0x00, 0x00 },
|
|
Data = new Byte[] { 0x00, 0x80, 0x80, 0x01 },
|
|
});
|
|
}
|
|
|
|
public void Write(SIRTMessage message)
|
|
=> base.Write(message, 0, message.Length);
|
|
|
|
private void StartParsing(object state)
|
|
{
|
|
var SIRTPort = state as SIRTPort;
|
|
|
|
if (SIRTPort is null)
|
|
{
|
|
return;
|
|
}
|
|
|
|
var buffer = new List<Byte>();
|
|
var reading = true;
|
|
var readingTask = Task.Factory
|
|
.StartNew(this.StartReading, state, CancellationToken.None, TaskCreationOptions.LongRunning, TaskScheduler.Default)
|
|
.ContinueWith(task =>
|
|
{
|
|
if (task.Exception != null)
|
|
{
|
|
this.NotifyInfoMessage(task.Exception.ToString());
|
|
}
|
|
|
|
reading = false;
|
|
});
|
|
var messageReceived = SIRTPort.MessageReceived;
|
|
|
|
while (reading)
|
|
{
|
|
var bufferLength = buffer.Count;
|
|
|
|
if (bufferLength < SIRTMessage.MinLength)
|
|
{
|
|
continue;
|
|
}
|
|
|
|
var start = buffer.IndexOf(SIRTMessage.SIRT2PC);
|
|
|
|
if (start < 0)
|
|
{
|
|
buffer.RemoveRange(0, bufferLength);
|
|
|
|
continue;
|
|
}
|
|
|
|
var lengthIndex = start + SIRTMessage.LengthI;
|
|
|
|
if (lengthIndex >= bufferLength)
|
|
{
|
|
continue;
|
|
}
|
|
|
|
var length = buffer[lengthIndex];
|
|
var dataLength = length + SIRTMessage.MinLength;
|
|
|
|
if (start + dataLength > bufferLength)
|
|
{
|
|
continue;
|
|
}
|
|
|
|
var data = new Byte[dataLength];
|
|
|
|
buffer.CopyTo(start, data, 0, dataLength);
|
|
buffer.RemoveRange(0, start + dataLength);
|
|
messageReceived?.Invoke(data);
|
|
}
|
|
}
|
|
|
|
private void StartReading(object state)
|
|
{
|
|
var SIRTPort = state as SIRTPort;
|
|
|
|
if (SIRTPort is null)
|
|
{
|
|
return;
|
|
}
|
|
|
|
var buffer = SIRTPort.buffer;
|
|
|
|
while (true)
|
|
{
|
|
var bytes = SIRTPort.Read();
|
|
|
|
if (bytes.Length > 0)
|
|
{
|
|
buffer.AddRange(bytes);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|