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