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