HamBook – Blame information for rev 9

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 {
9 office 25 readRegex = new Regex($"^{Name}0(?<squelch>[0-9]{{3}}){Generic.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 {
35 SerialPort.Write($"{Name}0{Generic.Constants.EOT}");
9 office 36 var buffer = new byte[CatLength.Answer];
37 if (SerialPort.Read(buffer, 0, CatLength.Answer) != CatLength.Answer)
38 {
39 throw new ArgumentException();
40 }
41  
42 var answer = Encoding.ASCII.GetString(buffer);
43 var match = readRegex.Match(answer);
44 if (!match.Success)
45 {
46 throw new ArgumentException();
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 {
54 var payload = Encoding.ASCII.GetBytes($"{Name}0{Generic.Constants.EOT}");
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 {
60 throw new ArgumentException();
61 }
62  
63 var answer = Encoding.ASCII.GetString(buffer);
64 var match = readRegex.Match(answer);
65 if (!match.Success)
66 {
67 throw new ArgumentException();
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 {
9 office 76 SerialPort.Write($"{Name}0{level:000}{Generic.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 {
83 var payload = Encoding.ASCII.GetBytes($"{Name}0{level:000}{Generic.Constants.EOT}");
84 await SerialPort.WriteAsync(payload, 0, payload.Length, cancellationToken);
3 office 85  
9 office 86 return await ReadAsync(cancellationToken) == level;
87 }
88  
89 public override void Write(int level)
90 {
91 SerialPort.Write($"{Name}0{level:000}{Generic.Constants.EOT}");
92 }
93  
94 public override async Task WriteAsync(int level, CancellationToken cancellationToken)
95 {
96 var payload = Encoding.ASCII.GetBytes($"{Name}0{level:000}{Generic.Constants.EOT}");
97  
98 await SerialPort.WriteAsync(payload, 0, payload.Length, cancellationToken);
99 }
3 office 100 }
101 }