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;
1 office 3 using System.Threading.Tasks;
54 office 4 using RJCP.IO.Ports;
1 office 5 using static HamBook.Radios.Yaesu.FT_891.Constants;
6  
9 office 7 namespace HamBook.Radios.Yaesu.FT_891.CAT
1 office 8 {
9 [Radio("Yaesu FT-891")]
54 office 10 public class Md : Generic.CAT.Md
1 office 11 {
54 office 12 private readonly Regex _readRegex;
11 office 13  
54 office 14 public Md(SerialPortStream serialPort) : base(serialPort)
1 office 15 {
54 office 16 _readRegex = new Regex($"^MD0(?<mode>[123456789ABCD]){Eot}$", RegexOptions.Compiled);
1 office 17 }
18  
54 office 19 public override CatLength CatLength => new CatLength { Set = 5, Answer = 5, Read = 4 };
20  
43 office 21 public override bool Set(Generic.RadioMode radioMode)
1 office 22 {
54 office 23 SerialPort.Write($"{Name}0{radioMode.Code}{Eot}");
11 office 24  
7 office 25 return Read() == radioMode;
1 office 26 }
27  
43 office 28 public override async Task<bool> SetAsync(Generic.RadioMode radioMode, CancellationToken cancellationToken)
9 office 29 {
54 office 30 var payload = Encoding.GetBytes($"{Name}0{radioMode.Code}{Eot}");
9 office 31  
32 await SerialPort.WriteAsync(payload, 0, payload.Length, cancellationToken);
33  
44 office 34 return (await ReadAsync(cancellationToken)).Code == radioMode.Code;
9 office 35 }
36  
43 office 37 public override Generic.RadioMode Read()
1 office 38 {
54 office 39 SerialPort.Write($"{Name}0{Eot}");
9 office 40 var buffer = new byte[CatLength.Answer];
41 if (SerialPort.Read(buffer, 0, CatLength.Answer) != CatLength.Answer)
11 office 42 throw new UnexpectedRadioResponseException(Name, buffer);
9 office 43  
54 office 44 var answer = Encoding.GetString(buffer);
45 var match = _readRegex.Match(answer);
46 if (!match.Success) throw new UnmatchedRadioResponseException(Name, answer);
3 office 47 var radioMode = char.Parse(match.Result("${mode}"));
43 office 48 return new RadioMode(radioMode);
1 office 49 }
54 office 50  
43 office 51 public override async Task<Generic.RadioMode> ReadAsync(CancellationToken cancellationToken)
9 office 52 {
54 office 53 var payload = Encoding.GetBytes($"{Name}0{Eot}");
9 office 54 await SerialPort.WriteAsync(payload, 0, payload.Length, cancellationToken);
55 var buffer = new byte[CatLength.Answer];
11 office 56 if (await SerialPort.ReadAsync(buffer, 0, buffer.Length, cancellationToken) != CatLength.Answer)
57 throw new UnexpectedRadioResponseException(Name, buffer);
9 office 58  
54 office 59 var answer = Encoding.GetString(buffer);
60 var match = _readRegex.Match(answer);
61 if (!match.Success) throw new UnmatchedRadioResponseException(Name, answer);
9 office 62  
63 var radioMode = char.Parse(match.Result("${mode}"));
43 office 64 return new RadioMode(radioMode);
9 office 65 }
1 office 66 }
54 office 67 }