HamBook – Rev 46
?pathlinks?
using HamBook.Radios.Generic.CAT;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using static HamBook.Radios.Yaesu.FT_891.Constants;
using RJCP.IO.Ports;
namespace HamBook.Radios.Yaesu.FT_891.CAT
{
[Radio("Yaesu FT-891")]
public class FA : Generic.CAT.FA
{
private readonly Regex readRegex;
public override CatLength CatLength => new CatLength { Set = 12, Answer = 12, Read = 3 };
public FA(SerialPortStream serialPort) : base(serialPort)
{
readRegex = new Regex($"^{Name}(?<frequency>[0-9]{{9}}){Constants.EOT}$", RegexOptions.Compiled);
}
public override bool Set(int frequency)
{
var f = $"{frequency:000000000}";
SerialPort.Write($"{Name}{f}{Constants.EOT}");
return Read() == frequency;
}
public override async Task<bool> SetAsync(int frequency, CancellationToken cancellation)
{
var f = $"{frequency:000000000}";
var payload = Constants.Encoding.GetBytes($"{Name}{f}{Constants.EOT}");
await SerialPort.WriteAsync(payload, 0, payload.Length, cancellation);
return await ReadAsync(cancellation) == frequency;
}
public override int Read()
{
SerialPort.Write($"{Name}{Constants.EOT}");
var buffer = new byte[CatLength.Answer];
if (SerialPort.Read(buffer, 0, CatLength.Answer) != CatLength.Answer)
{
throw new UnexpectedRadioResponseException(Name, buffer);
}
var answer = Constants.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 = Constants.Encoding.GetBytes($"{Name}{Constants.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 = Constants.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}{Constants.EOT}");
}
public override async Task WriteAsync(int frequency, CancellationToken cancellationToken)
{
var f = $"{frequency:000000000}";
var payload = Constants.Encoding.GetBytes($"{Name}{f}{Constants.EOT}");
await SerialPort.WriteAsync(payload, 0, payload.Length, cancellationToken);
}
}
}