HamBook – Blame information for rev 54

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