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