HamBook – Blame information for rev 54

Subversion Repositories:
Rev:
Rev Author Line No. Line
54 office 1 using System.Text.RegularExpressions;
2 using HamBook.Radios.Generic;
3 using RJCP.IO.Ports;
9 office 4 using static HamBook.Radios.Yaesu.FT_891.Constants;
5  
6 namespace HamBook.Radios.Yaesu.FT_891.CAT
7 {
8 [Radio("Yaesu FT-891")]
54 office 9 public class By : Generic.CAT.By
9 office 10 {
54 office 11 private readonly Regex _readRegex;
9 office 12  
54 office 13 public By(SerialPortStream serialPort) : base(serialPort)
9 office 14 {
54 office 15 _readRegex = new Regex($"^{Name}(?<state>[01])0{Eot}$", RegexOptions.Compiled);
9 office 16 }
17  
54 office 18 public override CatLength CatLength => new CatLength { Read = 3, Answer = 5 };
19  
9 office 20 public override BusyState Parse(string input)
21 {
54 office 22 var match = _readRegex.Match(input);
23 if (!match.Success) throw new UnmatchedRadioResponseException(Name, input);
9 office 24  
25 return (BusyState)int.Parse(match.Result("${state}"));
26 }
27  
28 public override BusyState Read()
29 {
54 office 30 SerialPort.Write($"{Name}{Eot}");
9 office 31 var buffer = new byte[CatLength.Answer];
54 office 32 if (SerialPort.Read(buffer, 0, CatLength.Answer) != CatLength.Answer)
11 office 33 throw new UnexpectedRadioResponseException(Name, buffer);
9 office 34  
54 office 35 var answer = Encoding.GetString(buffer);
36 var match = _readRegex.Match(answer);
37 if (!match.Success) throw new UnmatchedRadioResponseException(Name, answer);
9 office 38  
39 return (BusyState)int.Parse(match.Result("${state}"));
40 }
41 }
54 office 42 }