HamBook – Diff between revs 46 and 54
?pathlinks?
Rev 46 | Rev 54 | |||
---|---|---|---|---|
Line 1... | Line -... | |||
1 | using HamBook.Radios.Generic.CAT; |
- | ||
2 | using System; |
- | ||
3 | using System.Collections.Generic; |
- | ||
4 | using System.Drawing; |
- | ||
5 | using System.IO.Ports; |
- | ||
6 | using System.Linq; |
- | ||
7 | using System.Text; |
- | ||
8 | using System.Text.RegularExpressions; |
1 | using System.Text.RegularExpressions; |
|
9 | using System.Threading; |
2 | using System.Threading; |
|
10 | using System.Threading.Tasks; |
3 | using System.Threading.Tasks; |
|
11 | using static HamBook.Radios.Yaesu.FT_891.Constants; |
- | ||
12 | using RJCP.IO.Ports; |
4 | using RJCP.IO.Ports; |
|
- | 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 FB : Generic.CAT.FB |
10 | public class Fb : Generic.CAT.Fb |
|
18 | { |
11 | { |
|
19 | private readonly Regex readRegex; |
- | ||
20 | |
- | ||
Line 21... | Line 12... | |||
21 | public override CatLength CatLength => new CatLength { Set = 12, Answer = 12, Read = 3 }; |
12 | private readonly Regex _readRegex; |
|
22 | |
13 | |
|
23 | public FB(SerialPortStream serialPort) : base(serialPort) |
14 | public Fb(SerialPortStream serialPort) : base(serialPort) |
|
24 | { |
15 | { |
|
Line -... | Line 16... | |||
- | 16 | _readRegex = new Regex($"^{Name}(?<frequency>[0-9]{{9}}){Eot}$", RegexOptions.Compiled); |
||
- | 17 | } |
||
25 | readRegex = new Regex($"^{Name}(?<frequency>[0-9]{{9}}){Constants.EOT}$", RegexOptions.Compiled); |
18 | |
|
26 | } |
19 | public override CatLength CatLength => new CatLength { Set = 12, Answer = 12, Read = 3 }; |
|
27 | |
20 | |
|
Line 28... | Line 21... | |||
28 | public override bool Set(int frequency) |
21 | public override bool Set(int frequency) |
|
29 | { |
22 | { |
|
30 | var f = $"{frequency:000000000}"; |
23 | var f = $"{frequency:000000000}"; |
|
Line 31... | Line 24... | |||
31 | |
24 | |
|
32 | SerialPort.Write($"{Name}{f}{Constants.EOT}"); |
25 | SerialPort.Write($"{Name}{f}{Eot}"); |
|
33 | return Read() == frequency; |
26 | return Read() == frequency; |
|
Line 34... | Line 27... | |||
34 | } |
27 | } |
|
Line 35... | Line 28... | |||
35 | |
28 | |
|
Line 36... | Line 29... | |||
36 | public override async Task<bool> SetAsync(int frequency, CancellationToken cancellation) |
29 | public override async Task<bool> SetAsync(int frequency, CancellationToken cancellation) |
|
37 | { |
30 | { |
|
Line 38... | Line 31... | |||
38 | var f = $"{frequency:000000000}"; |
31 | var f = $"{frequency:000000000}"; |
|
39 | |
32 | |
|
40 | var payload = Constants.Encoding.GetBytes($"{Name}{f}{Constants.EOT}"); |
33 | var payload = Encoding.GetBytes($"{Name}{f}{Eot}"); |
|
41 | |
34 | |
|
42 | await SerialPort.WriteAsync(payload, 0, payload.Length, cancellation); |
35 | await SerialPort.WriteAsync(payload, 0, payload.Length, cancellation); |
|
43 | |
- | ||
44 | return await ReadAsync(cancellation) == frequency; |
36 | |
|
45 | } |
- | ||
Line 46... | Line 37... | |||
46 | |
37 | return await ReadAsync(cancellation) == frequency; |
|
47 | public override int Read() |
38 | } |
|
48 | { |
- | ||
49 | SerialPort.Write($"{Name}{Constants.EOT}"); |
- | ||
50 | var buffer = new byte[CatLength.Answer]; |
39 | |
|
51 | if (SerialPort.Read(buffer, 0, CatLength.Answer) != CatLength.Answer) |
- | ||
52 | { |
40 | public override int Read() |
|
53 | throw new UnexpectedRadioResponseException(Name, buffer); |
41 | { |
|
Line 54... | Line 42... | |||
54 | } |
42 | SerialPort.Write($"{Name}{Eot}"); |
|
55 | |
43 | var buffer = new byte[CatLength.Answer]; |
|
56 | var answer = Constants.Encoding.GetString(buffer); |
44 | if (SerialPort.Read(buffer, 0, CatLength.Answer) != CatLength.Answer) |
|
57 | var match = readRegex.Match(answer); |
45 | throw new UnexpectedRadioResponseException(Name, buffer); |
|
58 | if (!match.Success) |
46 | |
|
59 | { |
47 | var answer = Encoding.GetString(buffer); |
|
60 | throw new UnmatchedRadioResponseException(Name, answer); |
- | ||
61 | } |
48 | var match = _readRegex.Match(answer); |
|
62 | return int.Parse(match.Result("${frequency}")); |
- | ||
Line 63... | Line 49... | |||
63 | } |
49 | if (!match.Success) throw new UnmatchedRadioResponseException(Name, answer); |
|
64 | |
50 | return int.Parse(match.Result("${frequency}")); |
|
65 | public override async Task<int> ReadAsync(CancellationToken cancellationToken) |
- | ||
66 | { |
- | ||
67 | var payload = Constants.Encoding.GetBytes($"{Name}{Constants.EOT}"); |
51 | } |
|
68 | await SerialPort.WriteAsync(payload, 0, payload.Length, cancellationToken); |
- | ||
Line 69... | Line 52... | |||
69 | var buffer = new byte[CatLength.Answer]; |
52 | |
|
70 | if (await SerialPort.ReadAsync(buffer, 0, CatLength.Answer, cancellationToken) != CatLength.Answer) |
53 | public override async Task<int> ReadAsync(CancellationToken cancellationToken) |
|
Line 71... | Line 54... | |||
71 | { |
54 | { |
|
72 | throw new UnexpectedRadioResponseException(Name, buffer); |
55 | var payload = Encoding.GetBytes($"{Name}{Eot}"); |
|
73 | } |
56 | await SerialPort.WriteAsync(payload, 0, payload.Length, cancellationToken); |
|
Line 74... | Line 57... | |||
74 | |
57 | var buffer = new byte[CatLength.Answer]; |
|
75 | var answer = Constants.Encoding.GetString(buffer); |
58 | if (await SerialPort.ReadAsync(buffer, 0, CatLength.Answer, cancellationToken) != CatLength.Answer) |
|
Line 76... | Line 59... | |||
76 | var match = readRegex.Match(answer); |
59 | throw new UnexpectedRadioResponseException(Name, buffer); |
|
77 | if (!match.Success) |
60 | |
|
78 | { |
61 | var answer = Encoding.GetString(buffer); |
|
Line 79... | Line 62... | |||
79 | throw new UnmatchedRadioResponseException(Name, answer); |
62 | var match = _readRegex.Match(answer); |
|
Line 80... | Line 63... | |||
80 | } |
63 | if (!match.Success) throw new UnmatchedRadioResponseException(Name, answer); |
|
81 | |
64 | |
|
82 | return int.Parse(match.Result("${frequency}")); |
65 | return int.Parse(match.Result("${frequency}")); |
|
83 | } |
66 | } |
|
84 | |
67 | |