HamBook – Rev 54
?pathlinks?
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using HamBook.Radios.Generic;
using RJCP.IO.Ports;
using static HamBook.Radios.Yaesu.FT_891.Constants;
namespace HamBook.Radios.Yaesu.FT_891.CAT
{
[Radio("Yaesu FT-891")]
public class Pa : Generic.CAT.Pa
{
private readonly Regex _readRegex;
public Pa(SerialPortStream serialPort) : base(serialPort)
{
_readRegex = new Regex($"^{Name}0(?<state>[01]){Eot}$", RegexOptions.Compiled);
}
public override CatLength CatLength => new CatLength { Set = 5, Read = 4, Answer = 5 };
public override IpoState Read()
{
SerialPort.Write($"{Name}0{Eot}");
var buffer = new byte[CatLength.Answer];
if (SerialPort.Read(buffer, 0, CatLength.Answer) != CatLength.Answer)
throw new UnexpectedRadioResponseException(Name, buffer);
var answer = Encoding.GetString(buffer);
var match = _readRegex.Match(answer);
if (!match.Success) throw new UnmatchedRadioResponseException(Name, answer);
return (IpoState)int.Parse(match.Result("${state}"));
}
public override async Task<IpoState> ReadAsync(CancellationToken cancellationToken)
{
var payload = Encoding.GetBytes($"{Name}0{Eot}");
await SerialPort.WriteAsync(payload, 0, payload.Length, cancellationToken);
var buffer = new byte[CatLength.Answer];
if (await SerialPort.ReadAsync(buffer, 0, CatLength.Answer, cancellationToken) != CatLength.Answer)
throw new UnexpectedRadioResponseException(Name, buffer);
var answer = Encoding.GetString(buffer);
var match = _readRegex.Match(answer);
if (!match.Success) throw new UnmatchedRadioResponseException(Name, answer);
return (IpoState)int.Parse(match.Result("${state}"));
}
public override bool Set(IpoState ipoState)
{
var payload = Encoding.GetBytes($"{Name}0{(int)ipoState}{Eot}");
SerialPort.Write(payload, 0, payload.Length);
return Read() == ipoState;
}
public override async Task<bool> SetAsync(IpoState ipoState, CancellationToken cancellationToken)
{
var payload = Encoding.GetBytes($"{Name}0{(int)ipoState}{Eot}");
await SerialPort.WriteAsync(payload, 0, payload.Length, cancellationToken);
var result = await ReadAsync(cancellationToken);
return result == ipoState;
}
public override void Write(IpoState ipoState)
{
SerialPort.Write($"{Name}0{(int)ipoState}{Eot}");
}
public override async Task WriteAsync(IpoState ipoState, CancellationToken cancellationToken)
{
var payload = Encoding.GetBytes($"{Name}0{(int)ipoState}{Eot}");
await SerialPort.WriteAsync(payload, 0, payload.Length, cancellationToken);
}
}
}
Generated by GNU Enscript 1.6.5.90.