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