HamBook – Diff between revs 46 and 54

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