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