HamBook – Rev 54
?pathlinks?
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
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 Id : Generic.CAT.Id
{
private readonly Regex _readRegex;
public Id(SerialPortStream serialPort) : base(serialPort)
{
_readRegex = new Regex($"^{Name}(?<id>[0-9]{{4}}){Eot}$", RegexOptions.Compiled);
}
public override CatLength CatLength => new CatLength { Answer = 7, Read = 3 };
public override bool Read()
{
SerialPort.Write($"{Name}{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 int.Parse(match.Result("${id}")) == int.Parse(Constants.Id);
}
public override async Task<bool> ReadAsync(CancellationToken cancellationToken)
{
var payload = Encoding.GetBytes($"{Name}{Eot}");
await SerialPort.WriteAsync(payload, 0, payload.Length);
var buffer = new byte[CatLength.Answer];
if (await SerialPort.ReadAsync(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 int.Parse(match.Result("${id}")) == int.Parse(Constants.Id);
}
}
}
Generated by GNU Enscript 1.6.5.90.