HamBook – Blame information for rev 46

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