HamBook – Diff between revs 46 and 54
?pathlinks?
Rev 46 | Rev 54 | |||
---|---|---|---|---|
Line 1... | Line -... | |||
1 | using System; |
- | ||
2 | using System.Collections.Generic; |
- | ||
3 | using System.Drawing; |
- | ||
4 | using System.IO.Ports; |
- | ||
5 | using System.Linq; |
- | ||
6 | using System.Text; |
- | ||
7 | using System.Text.RegularExpressions; |
1 | using System.Text.RegularExpressions; |
|
8 | using System.Threading; |
2 | using System.Threading; |
|
9 | using System.Threading.Tasks; |
3 | using System.Threading.Tasks; |
|
10 | using static HamBook.Radios.Yaesu.FT_891.Constants; |
- | ||
11 | using RJCP.IO.Ports; |
4 | using RJCP.IO.Ports; |
|
- | 5 | using static HamBook.Radios.Yaesu.FT_891.Constants; |
||
Line 12... | Line 6... | |||
12 | |
6 | |
|
13 | namespace HamBook.Radios.Yaesu.FT_891.CAT |
7 | namespace HamBook.Radios.Yaesu.FT_891.CAT |
|
14 | { |
8 | { |
|
15 | [Radio("Yaesu FT-891")] |
9 | [Radio("Yaesu FT-891")] |
|
16 | public class ID : Generic.CAT.ID |
10 | public class Id : Generic.CAT.Id |
|
17 | { |
11 | { |
|
Line 18... | Line -... | |||
18 | private readonly Regex readRegex; |
- | ||
19 | |
12 | private readonly Regex _readRegex; |
|
20 | public override CatLength CatLength => new CatLength { Answer = 7, Read = 3 }; |
13 | |
|
21 | public ID(SerialPortStream serialPort) : base(serialPort) |
14 | public Id(SerialPortStream serialPort) : base(serialPort) |
|
22 | { |
15 | { |
|
Line -... | Line 16... | |||
- | 16 | _readRegex = new Regex($"^{Name}(?<id>[0-9]{{4}}){Eot}$", RegexOptions.Compiled); |
||
- | 17 | } |
||
23 | readRegex = new Regex($"^{Name}(?<id>[0-9]{{4}}){Constants.EOT}$", RegexOptions.Compiled); |
18 | |
|
24 | } |
19 | public override CatLength CatLength => new CatLength { Answer = 7, Read = 3 }; |
|
25 | |
20 | |
|
26 | public override bool Read() |
21 | public override bool Read() |
|
27 | { |
22 | { |
|
28 | SerialPort.Write($"{Name}{Constants.EOT}"); |
- | ||
29 | var buffer = new byte[CatLength.Answer]; |
23 | SerialPort.Write($"{Name}{Eot}"); |
|
30 | if (SerialPort.Read(buffer, 0, CatLength.Answer) != CatLength.Answer) |
- | ||
Line 31... | Line 24... | |||
31 | { |
24 | var buffer = new byte[CatLength.Answer]; |
|
32 | throw new UnexpectedRadioResponseException(Name, buffer); |
25 | if (SerialPort.Read(buffer, 0, CatLength.Answer) != CatLength.Answer) |
|
33 | } |
- | ||
34 | |
- | ||
35 | var answer = Constants.Encoding.GetString(buffer); |
26 | throw new UnexpectedRadioResponseException(Name, buffer); |
|
36 | var match = readRegex.Match(answer); |
- | ||
37 | if (!match.Success) |
27 | |
|
38 | { |
28 | var answer = Encoding.GetString(buffer); |
|
Line 39... | Line 29... | |||
39 | throw new UnmatchedRadioResponseException(Name, answer); |
29 | var match = _readRegex.Match(answer); |
|
40 | } |
30 | if (!match.Success) throw new UnmatchedRadioResponseException(Name, answer); |
|
41 | return int.Parse(match.Result("${id}")) == int.Parse(Constants.ID); |
31 | return int.Parse(match.Result("${id}")) == int.Parse(Constants.Id); |
|
42 | } |
32 | } |
|
43 | |
33 | |
|
44 | public override async Task<bool> ReadAsync(CancellationToken cancellationToken) |
34 | public override async Task<bool> ReadAsync(CancellationToken cancellationToken) |
|
45 | { |
- | ||
46 | var payload = Constants.Encoding.GetBytes($"{Name}{Constants.EOT}"); |
35 | { |
|
47 | await SerialPort.WriteAsync(payload, 0, payload.Length); |
- | ||
Line 48... | Line 36... | |||
48 | var buffer = new byte[CatLength.Answer]; |
36 | var payload = Encoding.GetBytes($"{Name}{Eot}"); |
|
49 | if (await SerialPort.ReadAsync(buffer, 0, CatLength.Answer) != CatLength.Answer) |
37 | await SerialPort.WriteAsync(payload, 0, payload.Length); |
|
50 | { |
- | ||
51 | throw new UnexpectedRadioResponseException(Name, buffer); |
- | ||
52 | } |
38 | var buffer = new byte[CatLength.Answer]; |
|
53 | |
- | ||
Line 54... | Line 39... | |||
54 | var answer = Constants.Encoding.GetString(buffer); |
39 | if (await SerialPort.ReadAsync(buffer, 0, CatLength.Answer) != CatLength.Answer) |
|
55 | var match = readRegex.Match(answer); |
40 | throw new UnexpectedRadioResponseException(Name, buffer); |
|
56 | if (!match.Success) |
41 | |
|
57 | { |
42 | var answer = Encoding.GetString(buffer); |
|
58 | throw new UnmatchedRadioResponseException(Name, answer); |
43 | var match = _readRegex.Match(answer); |