HamBook – Blame information for rev 46

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