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