HamBook – Rev 1

Subversion Repositories:
Rev:
using HamBook.Radios.Generic;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO.Ports;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using static HamBook.Radios.Yaesu.FT_891.Constants;

namespace HamBook.Radios.Yaesu.FT_891
{
    [Radio("Yaesu FT-891")]
    public class MD : Generic.CAT.MD
    {
        private static readonly Regex readRegex = new Regex(@"^MD0(?<mode>[1,2,3,4,5,6,7,8,9,A,B,C,D])$", RegexOptions.Compiled);

        public MD(SerialPort serialPort) : base(serialPort)
        {
        }

        public override void Set(RadioMode mode)
        {
            switch(mode)
            {
                case RadioMode.SSB_1:
                    SerialPort.Write($"{Name}01{EOT}");
                    break;
                case RadioMode.SSB_2:
                    SerialPort.Write($"{Name}02{EOT}");
                    break;
                case RadioMode.AM:
                    SerialPort.Write($"{Name}05{EOT}");
                    break;
                case RadioMode.FM:
                    SerialPort.Write($"{Name}04{EOT}");
                    break;
                case RadioMode.CW:
                    SerialPort.Write($"{Name}03{EOT}");
                    break;
            }
        }

        public override RadioMode Read()
        {
            SerialPort.Write($"{Name}0{EOT}");
            var result = SerialPort.ReadTo(EOT);
            var match = readRegex.Match(result);
            var value = char.Parse(match.Result("${mode}"));
            switch(value)
            {
                case '1':
                    return RadioMode.SSB_1;
                case '2':
                    return RadioMode.SSB_2;
                case '5':
                    return RadioMode.AM;
                case '4':
                    return RadioMode.FM;
                case '3':
                    return RadioMode.CW;
            }

            return RadioMode.NONE;
        }
    }
}