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