HamBook – Diff between revs 7 and 9

Subversion Repositories:
Rev:
Show entire fileIgnore whitespace
Rev 7 Rev 9
Line 7... Line 7...
7 using System.Linq; 7 using System.Linq;
8 using System.Text; 8 using System.Text;
9 using System.Text.RegularExpressions; 9 using System.Text.RegularExpressions;
10 using System.Threading.Tasks; 10 using System.Threading.Tasks;
11 using static HamBook.Radios.Yaesu.FT_891.Constants; 11 using static HamBook.Radios.Yaesu.FT_891.Constants;
-   12 using RJCP.IO.Ports;
-   13 using System.Threading;
Line 12... Line 14...
12   14  
13 namespace HamBook.Radios.Yaesu.FT_891 15 namespace HamBook.Radios.Yaesu.FT_891.CAT
14 { 16 {
15 [Radio("Yaesu FT-891")] 17 [Radio("Yaesu FT-891")]
16 public class PS : HamBook.Radios.Generic.CAT.PS 18 public class PS : HamBook.Radios.Generic.CAT.PS
17 { 19 {
Line -... Line 20...
-   20 private readonly Regex readRegex;
-   21  
18 private static readonly Regex readRegex = new Regex(@"^PS(?<state>[0,1])$", RegexOptions.Compiled); 22 public override CatLength CatLength => new CatLength { Set = 4, Answer = 4, Read = 3 };
19   23  
-   24 public PS(SerialPortStream serialPort) : base(serialPort)
20 public PS(SerialPort serialPort) : base(serialPort) 25 {
Line 21... Line 26...
21 { 26 readRegex = new Regex($"^{Name}(?<state>[01]){Generic.Constants.EOT}$", RegexOptions.Compiled);
22 } 27 }
23   28  
24 public override async Task<bool> Set(PowerState state) 29 public override async Task<bool> SetAsync(PowerState state, CancellationToken cancellationToken)
25 { 30 {
26 var taskCompletionSource = new TaskCompletionSource<bool>(); 31 var taskCompletionSource = new TaskCompletionSource<bool>();
Line 44... Line 49...
44 }); 49 });
45 #pragma warning restore CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed 50 #pragma warning restore CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed
Line 46... Line 51...
46   51  
47 SerialPort.Write($"POWER"); 52 SerialPort.Write($"POWER");
48 await taskCompletionSource.Task; 53 await taskCompletionSource.Task;
49 return await Read() == state; 54 return await ReadAsync(cancellationToken) == state;
Line 50... Line 55...
50 } 55 }
51   56  
52 public override async Task<PowerState> Read() 57 public override async Task<PowerState> ReadAsync(CancellationToken cancellationToken)
53 { 58 {
54 var taskCompletionSource = new TaskCompletionSource<int>(); 59 var taskCompletionSource = new TaskCompletionSource<int>();
55 #pragma warning disable CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed 60 #pragma warning disable CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed
56 Task.Delay(Constants.POWER_ON_DELAY).ContinueWith(_ => 61 Task.Delay(Constants.POWER_ON_DELAY).ContinueWith(_ =>
57 { 62 {
58 SerialPort.Write($"{Name}{Generic.Constants.EOT}"); 63 SerialPort.Write($"{Name}{Generic.Constants.EOT}");
59 var result = SerialPort.ReadTo(Generic.Constants.EOT); 64 var result = SerialPort.ReadTo($"{Generic.Constants.EOT}");
Line 60... Line 65...
60 var match = readRegex.Match(result); 65 var match = readRegex.Match(result);
61 var state = int.Parse(match.Result("${state}")); 66 var state = int.Parse(match.Result("${state}"));