HamBook – Rev 54

Subversion Repositories:
Rev:
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 Tx : Generic.CAT.Tx
    {
        private readonly Regex _readRegex;

        public Tx(SerialPortStream serialPort) : base(serialPort)
        {
            _readRegex = new Regex($"^{Name}(?<state>[0-9]){Eot}$", RegexOptions.Compiled);
        }

        public override CatLength CatLength => new CatLength { Set = 4, Read = 3, Answer = 4 };

        public override TxState 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 (TxState)int.Parse(match.Result("${state}"));
        }

        public override async Task<TxState> 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 (TxState)int.Parse(match.Result("${state}"));
        }

        public override bool Set(TxState state)
        {
            var payload = Encoding.GetBytes($"{Name}{(int)state}{Eot}");

            SerialPort.Write(payload, 0, payload.Length);

            return Read() == state;
        }

        public override async Task<bool> SetAsync(TxState state, CancellationToken cancellationToken)
        {
            var payload = Encoding.GetBytes($"{Name}{(int)state}{Eot}");

            await SerialPort.WriteAsync(payload, 0, payload.Length, cancellationToken);

            return await ReadAsync(cancellationToken) == state;
        }

        public override void Write(TxState state)
        {
            SerialPort.Write($"{Name}{(int)state}{Eot}");
        }

        public override async Task WriteAsync(TxState state, CancellationToken cancellationToken)
        {
            var payload = Encoding.GetBytes($"{Name}{(int)state}{Eot}");

            await SerialPort.WriteAsync(payload, 0, payload.Length, cancellationToken);
        }
    }
}

Generated by GNU Enscript 1.6.5.90.