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