HamBook – Diff between revs 3 and 9
?pathlinks?
Rev 3 | Rev 9 | |||
---|---|---|---|---|
Line 7... | Line 7... | |||
7 | using System.IO.Ports; |
7 | using System.IO.Ports; |
|
8 | using System.Linq; |
8 | using System.Linq; |
|
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; |
||
12 | using System.Threading.Tasks; |
13 | using System.Threading.Tasks; |
|
13 | using static HamBook.Radios.Yaesu.FT_891.Constants; |
14 | using static HamBook.Radios.Yaesu.FT_891.Constants; |
|
14 | |
- | ||
- | 15 | using RJCP.IO.Ports; |
||
Line 15... | Line 16... | |||
15 | |
16 | |
|
16 | namespace HamBook.Radios.Yaesu.FT_891.CAT |
17 | namespace HamBook.Radios.Yaesu.FT_891.CAT |
|
17 | { |
18 | { |
|
18 | [Radio("Yaesu FT-891")] |
19 | [Radio("Yaesu FT-891")] |
|
19 | public class AI : Generic.CAT.AI |
20 | public class AI : Generic.CAT.AI |
|
- | 21 | { |
||
- | 22 | private readonly Regex readRegex; |
||
20 | { |
23 | |
|
Line 21... | Line 24... | |||
21 | private static readonly Regex readRegex = new Regex(@"^AI(?<state>[0,1])$", RegexOptions.Compiled); |
24 | public override CatLength CatLength => new CatLength { Set = 4, Read = 3, Answer = 4 }; |
|
22 | |
25 | |
|
- | 26 | public AI(SerialPortStream serialPort) : base(serialPort) |
||
23 | public AI(SerialPort serialPort) : base(serialPort) |
27 | { |
|
Line 24... | Line 28... | |||
24 | { |
28 | readRegex = new Regex($"^{Name}(?<state>[01]){Generic.Constants.EOT}$", RegexOptions.Compiled); |
|
25 | } |
29 | } |
|
26 | |
30 | |
|
- | 31 | public override InformationState Read() |
||
- | 32 | { |
||
- | 33 | SerialPort.Write($"{Name}{Generic.Constants.EOT}"); |
||
- | 34 | var buffer = new byte[CatLength.Answer]; |
||
- | 35 | if (SerialPort.Read(buffer, 0, CatLength.Answer) != CatLength.Answer) |
||
- | 36 | { |
||
27 | public override InformationState Read() |
37 | throw new ArgumentException(); |
|
28 | { |
38 | } |
|
- | 39 | |
||
- | 40 | var answer = Encoding.ASCII.GetString(buffer); |
||
- | 41 | var match = readRegex.Match(answer); |
||
- | 42 | if (!match.Success) |
||
29 | SerialPort.Write($"{Name}{Generic.Constants.EOT}"); |
43 | { |
|
Line 30... | Line 44... | |||
30 | var result = SerialPort.ReadTo(Generic.Constants.EOT); |
44 | throw new ArgumentException(); |
|
31 | var match = readRegex.Match(result); |
45 | } |
|
- | 46 | var state = (InformationState)int.Parse(match.Result("${state}")); |
||
- | 47 | |
||
- | 48 | return state; |
||
- | 49 | } |
||
- | 50 | public override async Task<InformationState> ReadAsync(CancellationToken cancellationToken) |
||
- | 51 | { |
||
- | 52 | var payload = Encoding.ASCII.GetBytes($"{Name}{Generic.Constants.EOT}"); |
||
- | 53 | await SerialPort.WriteAsync(payload, 0, payload.Length, cancellationToken); |
||
- | 54 | var buffer = new byte[CatLength.Answer]; |
||
Line -... | Line 55... | |||
- | 55 | if (await SerialPort.ReadAsync(buffer, 0, CatLength.Answer, cancellationToken) != CatLength.Answer) |
||
- | 56 | { |
||
- | 57 | throw new ArgumentException(); |
||
- | 58 | } |
||
- | 59 | |
||
- | 60 | var answer = Encoding.ASCII.GetString(buffer); |
||
- | 61 | var match = readRegex.Match(answer); |
||
- | 62 | if (!match.Success) |
||
- | 63 | { |
||
- | 64 | throw new ArgumentException(); |
||
- | 65 | } |
||
32 | var state = (InformationState)int.Parse(match.Result("${state}")); |
66 | |
|
33 | |
67 | var state = (InformationState)int.Parse(match.Result("${state}")); |
|
34 | return state; |
68 | return state; |
|
- | 69 | } |
||
- | 70 | |
||
- | 71 | public override bool Set(InformationState state) |
||
- | 72 | { |
||
- | 73 | SerialPort.Write($"{Name}{(int)state}{Generic.Constants.EOT}"); |
||
- | 74 | return Read() == state; |
||
- | 75 | } |
||
35 | } |
76 | public override async Task<bool> SetAsync(InformationState state, CancellationToken cancellationToken) |
|
Line 36... | Line 77... | |||
36 | |
77 | { |
|
37 | public override void Set(InformationState state) |
78 | var payload = Encoding.ASCII.GetBytes($"{Name}{(int)state}{Generic.Constants.EOT}"); |
|
38 | { |
79 | await SerialPort.WriteAsync(payload, 0, payload.Length, cancellationToken); |
|
Line 71... | Line 112... | |||
71 | Phase =phase |
112 | Phase =phase |
|
72 | }; |
113 | }; |
|
Line 73... | Line 114... | |||
73 | |
114 | |
|
74 | return information; |
115 | return information; |
|
- | 116 | } |
||
- | 117 | |
||
- | 118 | public override void Write(InformationState state) |
||
- | 119 | { |
||
- | 120 | SerialPort.Write($"{Name}{(int)state}{Generic.Constants.EOT}"); |
||
- | 121 | } |
||
- | 122 | |
||
- | 123 | public override async Task WriteAsync(InformationState state, CancellationToken cancellationToken) |
||
- | 124 | { |
||
- | 125 | var payload = Encoding.ASCII.GetBytes($"{Name}{(int)state}{Generic.Constants.EOT}"); |
||
- | 126 | |
||
- | 127 | await SerialPort.WriteAsync(payload, 0, payload.Length, cancellationToken); |
||
75 | } |
128 | } |
|
76 | } |
129 | } |