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