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;
8 using System.Threading; 9 using System.Threading;
9 using System.Threading.Tasks; 10 using System.Threading.Tasks;
10 using static HamBook.Radios.Yaesu.FT_891.Constants; 11 using static HamBook.Radios.Yaesu.FT_891.Constants;
-   12 using RJCP.IO.Ports;
Line 11... Line 13...
11   13  
12 namespace HamBook.Radios.Yaesu.FT_891 14 namespace HamBook.Radios.Yaesu.FT_891.CAT
13 { 15 {
14 [Radio("Yaesu FT-891")] 16 [Radio("Yaesu FT-891")]
15 public class FA : Generic.CAT.FA 17 public class FA : Generic.CAT.FA
16 { 18 {
Line -... Line 19...
-   19 private readonly Regex readRegex;
-   20  
17 private static readonly Regex readRegex = new Regex(@"^FA(?<frequency>[0-9]{9})$", RegexOptions.Compiled); 21 public override CatLength CatLength => new CatLength { Set = 12, Answer = 12, Read = 3 };
18   22  
-   23 public FA(SerialPortStream serialPort) : base(serialPort)
19 public FA(SerialPort serialPort) : base(serialPort) 24 {
Line 20... Line 25...
20 { 25 readRegex = new Regex($"^{Name}(?<frequency>[0-9]{{9}}){HamBook.Radios.Generic.Constants.EOT}$", RegexOptions.Compiled);
21 } 26 }
22   27  
Line 23... Line 28...
23 public override bool Set(int frequency) 28 public override bool Set(int frequency)
24 { 29 {
25 var fa = 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  
26   42 await SerialPort.WriteAsync(payload, 0, payload.Length, cancellation);
27 SerialPort.Write($"{Name}{fa}{Generic.Constants.EOT}"); 43  
28 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];
29 } 51 if (SerialPort.Read(buffer, 0, CatLength.Answer) != CatLength.Answer)
30   52 {
-   53 throw new ArgumentException();
-   54 }
-   55  
-   56 var answer = Encoding.ASCII.GetString(buffer);
31 public override int Read() 57 var match = readRegex.Match(answer);
32 { 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}";
33 SerialPort.Write($"{Name}{Generic.Constants.EOT}"); 95  
34 var result = SerialPort.ReadTo(Generic.Constants.EOT); 96 var payload = Encoding.ASCII.GetBytes($"{Name}{f}{Generic.Constants.EOT}");