HamBook – Blame information for rev 54

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