HamBook – Diff between revs 46 and 54

Subversion Repositories:
Rev:
Show entire fileIgnore whitespace
Rev 46 Rev 54
Line 1... Line -...
1 using HamBook.Radios.Generic; -  
2 using Newtonsoft.Json.Linq; -  
3 using System; -  
4 using System.Collections.Concurrent; -  
5 using System.Collections.Generic; -  
6 using System.ComponentModel; -  
7 using System.IO.Ports; -  
8 using System.Linq; -  
9 using System.Reflection; 1 using System.Text.RegularExpressions;
10 using System.Text; 2 using System.Threading;
11 using System.Text.RegularExpressions; -  
12 using System.Threading.Tasks; 3 using System.Threading.Tasks;
13 using static HamBook.Radios.Yaesu.FT_891.Constants; -  
14 using RJCP.IO.Ports; 4 using RJCP.IO.Ports;
15 using System.Threading; 5 using static HamBook.Radios.Yaesu.FT_891.Constants;
Line 16... Line 6...
16   6  
17 namespace HamBook.Radios.Yaesu.FT_891.CAT 7 namespace HamBook.Radios.Yaesu.FT_891.CAT
18 { 8 {
19 [Radio("Yaesu FT-891")] 9 [Radio("Yaesu FT-891")]
20 public class MD : Generic.CAT.MD 10 public class Md : Generic.CAT.Md
21 { 11 {
Line 22... Line -...
22 private readonly Regex readRegex; -  
23   -  
24 public override CatLength CatLength => new CatLength { Set = 5, Answer = 5, Read = 4 }; 12 private readonly Regex _readRegex;
25   13  
26 public MD(SerialPortStream serialPort) : base(serialPort) 14 public Md(SerialPortStream serialPort) : base(serialPort)
27 { 15 {
Line -... Line 16...
-   16 _readRegex = new Regex($"^MD0(?<mode>[123456789ABCD]){Eot}$", RegexOptions.Compiled);
-   17 }
28 readRegex = new Regex($"^MD0(?<mode>[123456789ABCD]){Constants.EOT}$", RegexOptions.Compiled); 18  
29 } 19 public override CatLength CatLength => new CatLength { Set = 5, Answer = 5, Read = 4 };
30   20  
Line 31... Line 21...
31 public override bool Set(Generic.RadioMode radioMode) 21 public override bool Set(Generic.RadioMode radioMode)
32 { 22 {
Line 33... Line 23...
33 SerialPort.Write($"{Name}0{radioMode.Code}{Constants.EOT}"); 23 SerialPort.Write($"{Name}0{radioMode.Code}{Eot}");
34   24  
35 return Read() == radioMode; 25 return Read() == radioMode;
Line 36... Line 26...
36 } 26 }
Line 37... Line 27...
37   27  
38 public override async Task<bool> SetAsync(Generic.RadioMode radioMode, CancellationToken cancellationToken) 28 public override async Task<bool> SetAsync(Generic.RadioMode radioMode, CancellationToken cancellationToken)
Line 39... Line 29...
39 { 29 {
40 var payload = Constants.Encoding.GetBytes($"{Name}0{radioMode.Code}{Constants.EOT}"); 30 var payload = Encoding.GetBytes($"{Name}0{radioMode.Code}{Eot}");
41   31  
42 await SerialPort.WriteAsync(payload, 0, payload.Length, cancellationToken); 32 await SerialPort.WriteAsync(payload, 0, payload.Length, cancellationToken);
43   33  
44 return (await ReadAsync(cancellationToken)).Code == radioMode.Code; -  
45 } 34 return (await ReadAsync(cancellationToken)).Code == radioMode.Code;
46   -  
Line 47... Line 35...
47 public override Generic.RadioMode Read() 35 }
48 { 36  
49 SerialPort.Write($"{Name}0{Constants.EOT}"); -  
50 var buffer = new byte[CatLength.Answer]; -  
51 if (SerialPort.Read(buffer, 0, CatLength.Answer) != CatLength.Answer) 37 public override Generic.RadioMode Read()
52 { -  
53 throw new UnexpectedRadioResponseException(Name, buffer); 38 {
54 } 39 SerialPort.Write($"{Name}0{Eot}");
55   40 var buffer = new byte[CatLength.Answer];
-   41 if (SerialPort.Read(buffer, 0, CatLength.Answer) != CatLength.Answer)
56 var answer = Constants.Encoding.GetString(buffer); 42 throw new UnexpectedRadioResponseException(Name, buffer);
57 var match = readRegex.Match(answer); 43  
58 if (!match.Success) 44 var answer = Encoding.GetString(buffer);
59 { 45 var match = _readRegex.Match(answer);
60 throw new UnmatchedRadioResponseException(Name, answer); 46 if (!match.Success) throw new UnmatchedRadioResponseException(Name, answer);
61 } 47 var radioMode = char.Parse(match.Result("${mode}"));
62 var radioMode = char.Parse(match.Result("${mode}")); -  
63 return new RadioMode(radioMode); 48 return new RadioMode(radioMode);
64 } -  
Line 65... Line 49...
65 public override async Task<Generic.RadioMode> ReadAsync(CancellationToken cancellationToken) 49 }
66 { 50  
67 var payload = Constants.Encoding.GetBytes($"{Name}0{Constants.EOT}"); -  
68 await SerialPort.WriteAsync(payload, 0, payload.Length, cancellationToken); -  
69 var buffer = new byte[CatLength.Answer]; 51 public override async Task<Generic.RadioMode> ReadAsync(CancellationToken cancellationToken)
70 if (await SerialPort.ReadAsync(buffer, 0, buffer.Length, cancellationToken) != CatLength.Answer) -  
Line 71... Line 52...
71 { 52 {
72 throw new UnexpectedRadioResponseException(Name, buffer); 53 var payload = Encoding.GetBytes($"{Name}0{Eot}");
73 } 54 await SerialPort.WriteAsync(payload, 0, payload.Length, cancellationToken);
74   55 var buffer = new byte[CatLength.Answer];
75 var answer = Constants.Encoding.GetString(buffer); 56 if (await SerialPort.ReadAsync(buffer, 0, buffer.Length, cancellationToken) != CatLength.Answer)
76 var match = readRegex.Match(answer); 57 throw new UnexpectedRadioResponseException(Name, buffer);