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 Fb : Generic.CAT.Fb
{
private readonly Regex _readRegex;
public Fb(SerialPortStream serialPort) : base(serialPort)
{
_readRegex = new Regex($"^{Name}(?<frequency>[0-9]{{9}}){Eot}$", RegexOptions.Compiled);
}
public override CatLength CatLength => new CatLength { Set = 12, Answer = 12, Read = 3 };
public override bool Set(int frequency)
{
var f = $"{frequency:000000000}";
SerialPort.Write($"{Name}{f}{Eot}");
return Read() == frequency;
}
public override async Task<bool> SetAsync(int frequency, CancellationToken cancellation)
{
var f = $"{frequency:000000000}";
var payload = Encoding.GetBytes($"{Name}{f}{Eot}");
await SerialPort.WriteAsync(payload, 0, payload.Length, cancellation);
return await ReadAsync(cancellation) == frequency;
}
public override int 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("${frequency}"));
}
public override async Task<int> ReadAsync(CancellationToken cancellationToken)
{
var payload = Encoding.GetBytes($"{Name}{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 int.Parse(match.Result("${frequency}"));
}
public override void Write(int frequency)
{
var f = $"{frequency:000000000}";
SerialPort.Write($"{Name}{f}{Eot}");
}
public override async Task WriteAsync(int frequency, CancellationToken cancellationToken)
{
var f = $"{frequency:000000000}";
var payload = Encoding.GetBytes($"{Name}{f}{Eot}");
await SerialPort.WriteAsync(payload, 0, payload.Length, cancellationToken);
}
}
}
Generated by GNU Enscript 1.6.5.90.