HamBook – Diff between revs 7 and 9

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