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