HamBook – Rev 54

Subversion Repositories:
Rev:
using System.Text.RegularExpressions;
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 By : Generic.CAT.By
    {
        private readonly Regex _readRegex;

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

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

        public override BusyState Parse(string input)
        {
            var match = _readRegex.Match(input);
            if (!match.Success) throw new UnmatchedRadioResponseException(Name, input);

            return (BusyState)int.Parse(match.Result("${state}"));
        }

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

Generated by GNU Enscript 1.6.5.90.