HamBook – Blame information for rev 54
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
54 | office | 1 | using System.Text.RegularExpressions; |
2 | using System.Threading; |
||
3 | using System.Threading.Tasks; |
||
4 | using RJCP.IO.Ports; |
||
5 | using static HamBook.Radios.Yaesu.FT_891.Constants; |
||
6 | |||
7 | namespace HamBook.Radios.Yaesu.FT_891.CAT |
||
8 | { |
||
9 | [Radio("Yaesu FT-891")] |
||
10 | public class Ex : Generic.CAT.Ex |
||
11 | { |
||
12 | private readonly Regex _readRegex; |
||
13 | |||
14 | public Ex(SerialPortStream serialPort) : base(serialPort) |
||
15 | { |
||
16 | _readRegex = new Regex($"^{Name}(?<menu>[0-9]{{4}})(?<parameter>[0-9\\+\\-]+?){Eot}", |
||
17 | RegexOptions.Compiled); |
||
18 | } |
||
19 | |||
20 | public override CatLength CatLength => new CatLength { Set = 12, Answer = 12, Read = 7 }; |
||
21 | |||
22 | public override bool Set(int menu, int parameter) |
||
23 | { |
||
24 | var m = $"{menu:0000}"; |
||
25 | var p = $"{parameter}"; |
||
26 | |||
27 | SerialPort.Write($"{Name}{m}{p}{Eot}"); |
||
28 | return Read(menu) == parameter; |
||
29 | } |
||
30 | |||
31 | public override async Task<bool> SetAsync(int menu, int parameter, CancellationToken cancellation) |
||
32 | { |
||
33 | var m = $"{menu:0000}"; |
||
34 | var p = $"{parameter}"; |
||
35 | |||
36 | var payload = Encoding.GetBytes($"{Name}{m}{p}{Eot}"); |
||
37 | |||
38 | await SerialPort.WriteAsync(payload, 0, payload.Length, cancellation); |
||
39 | |||
40 | return await ReadAsync(menu, cancellation) == parameter; |
||
41 | } |
||
42 | |||
43 | public override int Read(int menu) |
||
44 | { |
||
45 | var m = $"{menu:0000}"; |
||
46 | |||
47 | SerialPort.Write($"{Name}{m}{Eot}"); |
||
48 | var buffer = new byte[CatLength.Answer]; |
||
49 | if (SerialPort.Read(buffer, 0, CatLength.Answer) > CatLength.Answer) |
||
50 | throw new UnexpectedRadioResponseException(Name, buffer); |
||
51 | |||
52 | var answer = Encoding.GetString(buffer); |
||
53 | var match = _readRegex.Match(answer); |
||
54 | if (!match.Success) throw new UnmatchedRadioResponseException(Name, answer); |
||
55 | return int.Parse(match.Result("${parameter}")); |
||
56 | } |
||
57 | |||
58 | public override async Task<int> ReadAsync(int menu, CancellationToken cancellationToken) |
||
59 | { |
||
60 | var m = $"{menu:0000}"; |
||
61 | |||
62 | var payload = Encoding.GetBytes($"{Name}{m}{Eot}"); |
||
63 | await SerialPort.WriteAsync(payload, 0, payload.Length, cancellationToken); |
||
64 | var buffer = new byte[CatLength.Answer]; |
||
65 | if (await SerialPort.ReadAsync(buffer, 0, CatLength.Answer, cancellationToken) > CatLength.Answer) |
||
66 | throw new UnexpectedRadioResponseException(Name, buffer); |
||
67 | |||
68 | var answer = Encoding.GetString(buffer); |
||
69 | var match = _readRegex.Match(answer); |
||
70 | if (!match.Success) throw new UnmatchedRadioResponseException(Name, answer); |
||
71 | |||
72 | return int.Parse(match.Result("${parameter}")); |
||
73 | } |
||
74 | |||
75 | public override void Write(int menu, int parameter) |
||
76 | { |
||
77 | var m = $"{menu:0000}"; |
||
78 | var p = $"{parameter}"; |
||
79 | |||
80 | SerialPort.Write($"{Name}{m}{p}{Eot}"); |
||
81 | } |
||
82 | |||
83 | public override async Task WriteAsync(int menu, int parameter, CancellationToken cancellationToken) |
||
84 | { |
||
85 | var m = $"{menu:0000}"; |
||
86 | var p = $"{parameter}"; |
||
87 | |||
88 | var result = $"{Name}{m}{p}{Eot}"; |
||
89 | |||
90 | var payload = Encoding.GetBytes(result); |
||
91 | |||
92 | await SerialPort.WriteAsync(payload, 0, payload.Length, cancellationToken); |
||
93 | } |
||
94 | } |
||
95 | } |