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;
1 office 3 using System.Threading.Tasks;
54 office 4 using RJCP.IO.Ports;
1 office 5 using static HamBook.Radios.Yaesu.FT_891.Constants;
6  
9 office 7 namespace HamBook.Radios.Yaesu.FT_891.CAT
1 office 8 {
9 [Radio("Yaesu FT-891")]
54 office 10 public class Fa : Generic.CAT.Fa
1 office 11 {
54 office 12 private readonly Regex _readRegex;
1 office 13  
54 office 14 public Fa(SerialPortStream serialPort) : base(serialPort)
1 office 15 {
54 office 16 _readRegex = new Regex($"^{Name}(?<frequency>[0-9]{{9}}){Eot}$", RegexOptions.Compiled);
1 office 17 }
18  
54 office 19 public override CatLength CatLength => new CatLength { Set = 12, Answer = 12, Read = 3 };
20  
7 office 21 public override bool Set(int frequency)
1 office 22 {
9 office 23 var f = $"{frequency:000000000}";
1 office 24  
54 office 25 SerialPort.Write($"{Name}{f}{Eot}");
7 office 26 return Read() == frequency;
1 office 27 }
28  
9 office 29 public override async Task<bool> SetAsync(int frequency, CancellationToken cancellation)
30 {
31 var f = $"{frequency:000000000}";
32  
54 office 33 var payload = Encoding.GetBytes($"{Name}{f}{Eot}");
9 office 34  
35 await SerialPort.WriteAsync(payload, 0, payload.Length, cancellation);
36  
37 return await ReadAsync(cancellation) == frequency;
38 }
39  
1 office 40 public override int Read()
41 {
54 office 42 SerialPort.Write($"{Name}{Eot}");
9 office 43 var buffer = new byte[CatLength.Answer];
44 if (SerialPort.Read(buffer, 0, CatLength.Answer) != CatLength.Answer)
11 office 45 throw new UnexpectedRadioResponseException(Name, buffer);
9 office 46  
54 office 47 var answer = Encoding.GetString(buffer);
48 var match = _readRegex.Match(answer);
49 if (!match.Success) throw new UnmatchedRadioResponseException(Name, answer);
1 office 50 return int.Parse(match.Result("${frequency}"));
51 }
9 office 52  
53 public override async Task<int> ReadAsync(CancellationToken cancellationToken)
54 {
54 office 55 var payload = Encoding.GetBytes($"{Name}{Eot}");
9 office 56 await SerialPort.WriteAsync(payload, 0, payload.Length, cancellationToken);
57 var buffer = new byte[CatLength.Answer];
54 office 58 if (await SerialPort.ReadAsync(buffer, 0, CatLength.Answer, cancellationToken) != CatLength.Answer)
11 office 59 throw new UnexpectedRadioResponseException(Name, buffer);
9 office 60  
54 office 61 var answer = Encoding.GetString(buffer);
62 var match = _readRegex.Match(answer);
63 if (!match.Success) throw new UnmatchedRadioResponseException(Name, answer);
9 office 64  
65 return int.Parse(match.Result("${frequency}"));
66 }
67  
68 public override void Write(int frequency)
69 {
70 var f = $"{frequency:000000000}";
71  
54 office 72 SerialPort.Write($"{Name}{f}{Eot}");
9 office 73 }
74  
75 public override async Task WriteAsync(int frequency, CancellationToken cancellationToken)
76 {
77 var f = $"{frequency:000000000}";
78  
54 office 79 var payload = Encoding.GetBytes($"{Name}{f}{Eot}");
9 office 80  
81 await SerialPort.WriteAsync(payload, 0, payload.Length, cancellationToken);
82 }
1 office 83 }
54 office 84 }