HamBook – Blame information for rev 11

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