HamBook – Blame information for rev 46

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