HamBook – Diff between revs 7 and 9

Subversion Repositories:
Rev:
Show entire fileIgnore whitespace
Rev 7 Rev 9
Line 9... Line 9...
9 using System.Reflection; 9 using System.Reflection;
10 using System.Text; 10 using System.Text;
11 using System.Text.RegularExpressions; 11 using System.Text.RegularExpressions;
12 using System.Threading.Tasks; 12 using System.Threading.Tasks;
13 using static HamBook.Radios.Yaesu.FT_891.Constants; 13 using static HamBook.Radios.Yaesu.FT_891.Constants;
-   14 using RJCP.IO.Ports;
-   15 using System.Threading;
Line 14... Line 16...
14   16  
15 namespace HamBook.Radios.Yaesu.FT_891 17 namespace HamBook.Radios.Yaesu.FT_891.CAT
16 { 18 {
17 [Radio("Yaesu FT-891")] 19 [Radio("Yaesu FT-891")]
18 public class MD : Generic.CAT.MD 20 public class MD : Generic.CAT.MD
-   21 {
19 { 22 private readonly Regex readRegex;
20 private static readonly Regex readRegex = new Regex(@"^MD0(?<mode>[123456789ABCD]){1}$", RegexOptions.Compiled); -  
21   23 public override CatLength CatLength => new CatLength { Set = 5, Answer = 5, Read = 4 };
22 public MD(SerialPort serialPort) : base(serialPort) 24 public MD(SerialPortStream serialPort) : base(serialPort)
-   25 {
23 { 26 readRegex = new Regex($"^MD0(?<mode>[123456789ABCD]){Generic.Constants.EOT}$", RegexOptions.Compiled);
Line 24... Line 27...
24 } 27 }
25   28  
26 public override bool Set(RadioMode radioMode) 29 public override bool Set(RadioMode radioMode)
27 { 30 {
28 SerialPort.Write($"{Name}0{radioMode.Mode}{Generic.Constants.EOT}"); 31 SerialPort.Write($"{Name}0{radioMode.Mode}{Generic.Constants.EOT}");
Line -... Line 32...
-   32 return Read() == radioMode;
-   33 }
-   34  
-   35 public override async Task<bool> SetAsync(RadioMode radioMode, CancellationToken cancellationToken)
-   36 {
-   37 var payload = Encoding.ASCII.GetBytes($"{Name}0{radioMode.Mode}{Generic.Constants.EOT}");
-   38  
-   39 await SerialPort.WriteAsync(payload, 0, payload.Length, cancellationToken);
-   40  
29 return Read() == radioMode; 41 return await ReadAsync(cancellationToken) == radioMode;
30 } 42 }
31   43  
-   44 public override RadioMode Read()
-   45 {
-   46 SerialPort.Write($"{Name}0{Generic.Constants.EOT}");
-   47 var buffer = new byte[CatLength.Answer];
-   48 if (SerialPort.Read(buffer, 0, CatLength.Answer) != CatLength.Answer)
-   49 {
-   50 throw new ArgumentException();
-   51 }
-   52  
-   53 var answer = Encoding.ASCII.GetString(buffer);
-   54 var match = readRegex.Match(answer);
-   55 if (!match.Success)
-   56 {
-   57 throw new ArgumentException();
-   58 }
-   59 var radioMode = char.Parse(match.Result("${mode}"));
-   60 return radioMode;
32 public override RadioMode Read() 61 }
-   62 public override async Task<RadioMode> ReadAsync(CancellationToken cancellationToken)
-   63 {
-   64 var payload = Encoding.ASCII.GetBytes($"{Name}0{Generic.Constants.EOT}");
-   65 await SerialPort.WriteAsync(payload, 0, payload.Length, cancellationToken);
-   66 var buffer = new byte[CatLength.Answer];
-   67 if(await SerialPort.ReadAsync(buffer, 0, buffer.Length, cancellationToken) != CatLength.Answer)
-   68 {
-   69 throw new ArgumentException();
33 { 70 }
-   71  
-   72 var answer = Encoding.ASCII.GetString(buffer);
-   73 var match = readRegex.Match(answer);
-   74 if (!match.Success)
-   75 {
34 SerialPort.Write($"{Name}0{Generic.Constants.EOT}"); 76 throw new ArgumentException();
35 var result = SerialPort.ReadTo(Generic.Constants.EOT); 77 }
36 var match = readRegex.Match(result); 78  
37 var radioMode = char.Parse(match.Result("${mode}")); 79 var radioMode = char.Parse(match.Result("${mode}"));
38 return radioMode; 80 return radioMode;