HamBook – Diff between revs 7 and 9
?pathlinks?
Rev 7 | Rev 9 | |||
---|---|---|---|---|
Line 4... | Line 4... | |||
4 | using System.Collections.Generic; |
4 | using System.Collections.Generic; |
|
5 | using System.IO.Ports; |
5 | using System.IO.Ports; |
|
6 | using System.Linq; |
6 | using System.Linq; |
|
7 | using System.Text; |
7 | using System.Text; |
|
8 | using System.Text.RegularExpressions; |
8 | using System.Text.RegularExpressions; |
|
- | 9 | using System.Threading; |
||
9 | using System.Threading.Tasks; |
10 | using System.Threading.Tasks; |
|
10 | using static HamBook.Radios.Yaesu.FT_891.Constants; |
11 | using static HamBook.Radios.Yaesu.FT_891.Constants; |
|
- | 12 | using RJCP.IO.Ports; |
||
Line 11... | Line 13... | |||
11 | |
13 | |
|
12 | namespace HamBook.Radios.Yaesu.FT_891 |
14 | namespace HamBook.Radios.Yaesu.FT_891.CAT |
|
13 | { |
15 | { |
|
14 | [Radio("Yaesu FT-891")] |
16 | [Radio("Yaesu FT-891")] |
|
15 | public class SQ : HamBook.Radios.Generic.CAT.SQ |
17 | public class SQ : HamBook.Radios.Generic.CAT.SQ |
|
16 | { |
18 | { |
|
Line -... | Line 19... | |||
- | 19 | private readonly Regex readRegex; |
||
- | 20 | |
||
17 | private static readonly Regex readRegex = new Regex(@"^SQ0(?<squelch>[0-9]{3})$", RegexOptions.Compiled); |
21 | public override CatLength CatLength => new CatLength { Set = 7, Answer = 7, Read = 4 }; |
|
18 | |
22 | |
|
- | 23 | public SQ(SerialPortStream serialPort) : base(serialPort) |
||
19 | public SQ(SerialPort serialPort) : base(serialPort) |
24 | { |
|
Line 20... | Line 25... | |||
20 | { |
25 | readRegex = new Regex($"^{Name}0(?<squelch>[0-9]{{3}}){Generic.Constants.EOT}$", RegexOptions.Compiled); |
|
21 | } |
26 | } |
|
22 | |
27 | |
|
23 | public override int GetDefault() |
28 | public override int GetDefault() |
|
Line 24... | Line 29... | |||
24 | { |
29 | { |
|
25 | return SQUELCH_MIN_STEP; |
30 | return SQUELCH_MIN_STEP; |
|
26 | } |
31 | } |
|
- | 32 | |
||
- | 33 | public override int Read() |
||
- | 34 | { |
||
- | 35 | SerialPort.Write($"{Name}0{Generic.Constants.EOT}"); |
||
- | 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 | } |
||
- | 48 | var level = int.Parse(match.Result("${squelch}")); |
||
- | 49 | return level; |
||
27 | |
50 | } |
|
- | 51 | |
||
- | 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 | { |
||
28 | public override int Read() |
60 | throw new ArgumentException(); |
|
- | 61 | } |
||
- | 62 | |
||
- | 63 | var answer = Encoding.ASCII.GetString(buffer); |
||
- | 64 | var match = readRegex.Match(answer); |
||
- | 65 | if (!match.Success) |
||
29 | { |
66 | { |
|
30 | SerialPort.Write($"{Name}0{Generic.Constants.EOT}"); |
67 | throw new ArgumentException(); |
|
31 | var result = SerialPort.ReadTo(Generic.Constants.EOT); |
68 | } |
|
Line 32... | Line 69... | |||
32 | var match = readRegex.Match(result); |
69 | |
|
33 | var level = int.Parse(match.Result("${squelch}")); |
70 | var level = int.Parse(match.Result("${squelch}")); |
|
34 | return level; |
71 | return level; |
|
Line 35... | Line 72... | |||
35 | } |
72 | } |
|
36 | |
73 | |
|
Line -... | Line 74... | |||
- | 74 | public override bool Set(int level) |
||
- | 75 | { |
||
- | 76 | SerialPort.Write($"{Name}0{level:000}{Generic.Constants.EOT}"); |
||
- | 77 | |
||
Line -... | Line 78... | |||
- | 78 | return Read() == level; |
||
- | 79 | } |
||
- | 80 | |
||
- | 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); |
||
- | 85 | |
||
- | 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}"); |
||
37 | public override bool Set(int level) |
92 | } |
|
38 | { |
93 | |