HamBook – Rev 11

Subversion Repositories:
Rev:
using HamBook.Radios.Generic;
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.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 IF : Generic.CAT.IF
    {
        public override CatLength CatLength => new CatLength { Answer = 28, Read = 3 };

        public IF(SerialPortStream serialPort) : base(serialPort)
        {
        }

        public override Information Read() 
        {
            SerialPort.Write($"{Name}{Generic.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 = InformationRegex.Match(answer);
            if (!match.Success)
            {
                throw new UnmatchedRadioResponseException(Name, answer);
            }

            var information = new Information
            {
                RadioBank = int.Parse(match.Result("${radioBank}")),
                Frequency = int.Parse(match.Result("${frequency}")),
                ClarifierDirection = char.Parse(match.Result("${clarifierDirection}")),
                ClarifierOffset = int.Parse(match.Result("${clarifierOffset}")),
                Clar = int.Parse(match.Result("${clar}")),
                RadioMode = char.Parse(match.Result("${mode}")),
                Storage = (RadioStorage)int.Parse(match.Result("${storage}")),
                CtcssMode = (CtcssMode)int.Parse(match.Result("${ctcssMode}")),
                Phase = (RadioPhase)int.Parse(match.Result("${phase}"))
            };

            return information;
        }
    }
}