laatzen/Common/CommonConsole/Program.cs
2025-03-17 16:03:24 +01:00

186 lines
4.7 KiB
C#

namespace CommonConsole
{
using Common.Hardware.SIRT;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.IO.Ports;
using System.Threading;
public static class Program
{
public static void Main()
{
var sirtPort = new SIRTPort("COM8", 112500, Parity.None, 8, StopBits.One);
var count = 100;
sirtPort.Open();
sirtPort.Write(SIRTTask.ActivateSIRT.ToByteArray());
foreach (var bytes in sirtPort.ReadBytes())
{
Console.WriteLine(BitConverter.ToString(bytes));
if (--count <= 0)
{
break;
}
}
sirtPort.Close();
Console.WriteLine("Done!");
}
}
public sealed class SIRTPort
{
private static readonly ConcurrentDictionary<string, SerialPort> serialPorts
= new ConcurrentDictionary<string, SerialPort>();
private readonly int baudRate;
private readonly Parity parity;
private readonly int dataBits;
private readonly StopBits stopBits;
public SIRTPort(string portName, int baudRate, Parity parity, int dataBits, StopBits stopBits)
{
this.PortName = portName;
this.baudRate = baudRate;
this.parity = parity;
this.dataBits = dataBits;
this.stopBits = stopBits;
}
public event Action Connected;
public event Action Disconnected;
public event Action<string> Logging;
public string PortName { get; }
public void Close()
{
try
{
var awaiter = new ManualResetEventSlim();
var sp = this.GetOrAddSerialPort();
void disposed(object _, EventArgs _1)
=> awaiter.Set();
sp.Disposed += disposed;
sp.Close();
awaiter.Wait(5000);
sp.Disposed -= disposed;
this.Disconnected?.Invoke();
}
catch (Exception e)
{
this.Logging?.Invoke(e.ToString());
}
serialPorts.TryRemove(this.PortName, out var _);
}
public void Open()
{
var sp = this.GetOrAddSerialPort();
if (sp?.IsOpen == true)
{
this.Close();
}
try
{
sp.Open();
this.Connected?.Invoke();
}
catch (Exception e)
{
Console.WriteLine(e);
this.Logging?.Invoke(e.ToString());
}
}
public IEnumerable<byte[]> ReadBytes()
{
var awaiter = new ManualResetEventSlim();
void dataReceived(object _, SerialDataReceivedEventArgs args)
{
Console.WriteLine($"{args.EventType}");
if (args.EventType == SerialData.Chars)
{
awaiter.Set();
Console.WriteLine($"awaiter.Set()");
}
}
var sp = this.GetOrAddSerialPort();
sp.DataReceived += dataReceived;
var size = sp.ReadBufferSize;
var buffer = new byte[size];
var length = 0;
while (sp?.IsOpen == true)
{
if (!awaiter.IsSet)
{
Console.WriteLine($"awaiter.Wait()");
awaiter.Wait();
awaiter.Reset();
}
try
{
buffer = new byte[size];
length = sp.Read(buffer, 0, size);
Array.Resize(ref buffer, length);
}
catch (Exception e)
{
Console.WriteLine(e);
this.Logging?.Invoke(e.ToString());
}
awaiter.Reset();
yield return buffer;
}
sp.DataReceived -= dataReceived;
}
public void Write(byte[] bytes)
{
try
{
this.GetOrAddSerialPort().Write(bytes, 0, bytes.Length);
}
catch (Exception e)
{
this.Logging?.Invoke(e.ToString());
}
}
private SerialPort GetOrAddSerialPort()
=> serialPorts.GetOrAdd(this.PortName, new SerialPort(this.PortName, this.baudRate, this.parity, this.dataBits, this.stopBits));
public static string[] GetPortNames()
=> SerialPort.GetPortNames();
}
}