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 PC : Generic.CAT.PC 10 public class Pc : Generic.CAT.Pc
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 PC(SerialPortStream serialPort) : base(serialPort) 14 public Pc(SerialPortStream serialPort) : base(serialPort)
24 { 15 {
Line -... Line 16...
-   16 _readRegex = new Regex($"^{Name}(?<power>[0-9]{{3}}){Eot}$", RegexOptions.Compiled);
-   17 }
25 readRegex = new Regex($"^{Name}(?<power>[0-9]{{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 int Read() 21 public override int 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 int.Parse(match.Result("${power}")); 31 return int.Parse(match.Result("${power}"));
44 } 32 }
45   33  
46 public override async Task<int> ReadAsync(CancellationToken cancellationToken) 34 public override async Task<int> 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 int.Parse(match.Result("${power}")); 46 return int.Parse(match.Result("${power}"));
Line 64... Line 47...
64 } 47 }
65   48  
66 public override bool Set(int power) 49 public override bool Set(int power)
Line 67... Line 50...
67 { 50 {
Line 68... Line 51...
68 var payload = Constants.Encoding.GetBytes($"{Name}{power:000}{Constants.EOT}"); 51 var payload = Encoding.GetBytes($"{Name}{power:000}{Eot}");
Line 69... Line 52...
69   52  
70 SerialPort.Write(payload, 0, payload.Length); 53 SerialPort.Write(payload, 0, payload.Length);
Line 71... Line 54...
71   54  
72 return Read() == power; 55 return Read() == power;
73 } 56 }
74   57  
Line 75... Line 58...
75 public override async Task<bool> SetAsync(int power, CancellationToken cancellationToken) 58 public override async Task<bool> SetAsync(int power, CancellationToken cancellationToken)
76 { 59 {
77 var payload = Constants.Encoding.GetBytes($"{Name}{power:000}{Constants.EOT}"); 60 var payload = Encoding.GetBytes($"{Name}{power:000}{Eot}");
Line 78... Line 61...
78   61  
79 await SerialPort.WriteAsync(payload, 0, payload.Length, cancellationToken); 62 await SerialPort.WriteAsync(payload, 0, payload.Length, cancellationToken);
80   63  
81 var result = await ReadAsync(cancellationToken); 64 var result = await ReadAsync(cancellationToken);
82   65