HamBook – Blame information for rev 54

Subversion Repositories:
Rev:
Rev Author Line No. Line
54 office 1 using System.Text.RegularExpressions;
2 using System.Threading;
23 office 3 using System.Threading.Tasks;
4 using HamBook.Radios.Generic;
54 office 5 using RJCP.IO.Ports;
23 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 Ac : Generic.CAT.Ac
23 office 12 {
54 office 13 private readonly Regex _readRegex;
23 office 14  
54 office 15 public Ac(SerialPortStream serialPort) : base(serialPort)
23 office 16 {
54 office 17 _readRegex = new Regex($"^{Name}00(?<state>[012]){Eot}$", RegexOptions.Compiled);
23 office 18 }
19  
54 office 20 public override CatLength CatLength => new CatLength { Set = 6, Read = 3, Answer = 6 };
21  
23 office 22 public override TunerState Read()
23 {
54 office 24 SerialPort.Write($"{Name}{Eot}");
23 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);
23 office 32 return (TunerState)int.Parse(match.Result("${state}"));
33 }
34  
35 public override async Task<TunerState> ReadAsync(CancellationToken cancellationToken)
36 {
54 office 37 var payload = Encoding.GetBytes($"{Name}{Eot}");
23 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);
23 office 46  
47 return (TunerState)int.Parse(match.Result("${state}"));
48 }
49  
50 public override bool Set(TunerState tunerState)
51 {
54 office 52 var payload = Encoding.GetBytes($"{Name}00{(int)tunerState}{Eot}");
23 office 53  
54 SerialPort.Write(payload, 0, payload.Length);
55  
56 return Read() == tunerState;
57 }
58  
59 public override async Task<bool> SetAsync(TunerState tunerState, CancellationToken cancellationToken)
60 {
54 office 61 var payload = Encoding.GetBytes($"{Name}00{(int)tunerState}{Eot}");
23 office 62  
63 await SerialPort.WriteAsync(payload, 0, payload.Length, cancellationToken);
64  
39 office 65 var read = await ReadAsync(cancellationToken);
66  
67 return read == tunerState;
23 office 68 }
69  
70 public override void Write(TunerState tunerState)
71 {
54 office 72 SerialPort.Write($"{Name}00{(int)tunerState}{Eot}");
23 office 73 }
74  
75 public override async Task WriteAsync(TunerState tunerState, CancellationToken cancellationToken)
76 {
54 office 77 var payload = Encoding.GetBytes($"{Name}00{(int)tunerState}{Eot}");
23 office 78  
79 await SerialPort.WriteAsync(payload, 0, payload.Length, cancellationToken);
80 }
81 }
54 office 82 }