HamBook – Blame information for rev 46
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
15 | office | 1 | using System; |
2 | using System.Collections.Generic; |
||
3 | using System.ComponentModel; |
||
4 | using System.IO.Ports; |
||
5 | using System.Linq; |
||
6 | using System.Text; |
||
7 | using System.Threading.Tasks; |
||
8 | using HamBook.Radios.Generic; |
||
9 | using static HamBook.Radios.Yaesu.FT_891.Constants; |
||
10 | using RJCP.IO.Ports; |
||
11 | using System.Text.RegularExpressions; |
||
12 | using System.Threading; |
||
13 | using System.Diagnostics; |
||
14 | |||
15 | namespace HamBook.Radios.Yaesu.FT_891.CAT |
||
16 | { |
||
17 | [Radio("Yaesu FT-891")] |
||
18 | public class MT : Generic.CAT.MT |
||
19 | { |
||
20 | private readonly Regex readRegex; |
||
21 | |||
22 | public override CatLength CatLength => new CatLength { Set = 41, Read = 6, Answer = 41}; |
||
23 | |||
24 | public MT(SerialPortStream serialPort) : base(serialPort) |
||
25 | { |
||
26 | /// MTP9U026965000+0000004100001AAAAAAAAAAAA; |
||
27 | /// ^---- fixed: 0 in manual, but responds 1; looks like a radio bug |
||
46 | office | 28 | readRegex = new Regex($"^{Name}(?<currentLocation>[0-9PLU]{{3}})(?<frequency>[0-9]{{9}})(?<clarifierDirection>[\\+\\-]{{1}})(?<clarifierOffset>[0-9]{{4}})(?<clar>[01]{{1}})0(?<mode>[123456789ABCD]{{1}})[0-9]{{1}}(?<ctcss>[012]{{1}})00(?<phase>[012]{{1}})(?<tag>[01]{{1}})(?<text>[\x20-\x7E]{{0,12}}){Constants.EOT}$", RegexOptions.Compiled); |
15 | office | 29 | } |
30 | |||
46 | office | 31 | public override Generic.MemoryChannel Read(string location) |
15 | office | 32 | { |
46 | office | 33 | SerialPort.Write($"{Name}{location}{Constants.EOT}"); |
15 | office | 34 | var buffer = new byte[CatLength.Answer]; |
35 | if (SerialPort.Read(buffer, 0, CatLength.Answer) != CatLength.Answer) |
||
36 | { |
||
37 | throw new UnexpectedRadioResponseException(Name, buffer); |
||
38 | } |
||
39 | |||
40 | var answer = Constants.Encoding.GetString(buffer); |
||
41 | |||
42 | return Parse(answer); |
||
43 | } |
||
44 | |||
46 | office | 45 | public override async Task<Generic.MemoryChannel> ReadAsync(string location, CancellationToken cancellationToken) |
15 | office | 46 | { |
46 | office | 47 | var payload = Constants.Encoding.GetBytes($"{Name}{location}{Constants.EOT}"); |
15 | office | 48 | await SerialPort.WriteAsync(payload, 0, payload.Length); |
49 | var buffer = new byte[CatLength.Answer]; |
||
50 | if (await SerialPort.ReadAsync(buffer, 0, CatLength.Answer) != CatLength.Answer) |
||
51 | { |
||
52 | throw new UnexpectedRadioResponseException(Name, buffer); |
||
53 | } |
||
54 | |||
55 | var answer = Constants.Encoding.GetString(buffer); |
||
56 | |||
57 | return Parse(answer); |
||
58 | } |
||
59 | |||
46 | office | 60 | public override bool Set(Generic.MemoryChannel channel) |
15 | office | 61 | { |
46 | office | 62 | var payload = Constants.Encoding.GetBytes(Compose(new MemoryChannel(channel))); |
15 | office | 63 | |
64 | SerialPort.Write(payload, 0, payload.Length); |
||
65 | |||
66 | return Read(channel.CurrentLocation).Equals(channel); |
||
67 | } |
||
68 | |||
46 | office | 69 | public override async Task<bool> SetAsync(Generic.MemoryChannel channel, CancellationToken cancellationToken) |
15 | office | 70 | { |
46 | office | 71 | var compose = Compose(new MemoryChannel(channel)); |
15 | office | 72 | |
73 | var payload = Constants.Encoding.GetBytes(compose); |
||
74 | |||
75 | await SerialPort.WriteAsync(payload, 0, payload.Length, cancellationToken); |
||
76 | |||
17 | office | 77 | var result = await ReadAsync(channel.CurrentLocation, cancellationToken); |
78 | |||
79 | return result.Equals(channel); |
||
15 | office | 80 | } |
81 | |||
46 | office | 82 | private string Compose(MemoryChannel channel) |
15 | office | 83 | { |
46 | office | 84 | return $"{Name}{channel.CurrentLocation}{channel.Frequency:000000000}{(char)channel.ClarifierDirection}{channel.ClarifierOffset:0000}{(channel.Clar ? 1 : 0)}0{channel.MemoryRadioMode.Code}1{(int)channel.Ctcss.Mode}00{(int)channel.Phase.Mode}{(channel.Tag ? 1 : 0)}{channel.Text,-12}{Constants.EOT}"; |
15 | office | 85 | } |
86 | |||
46 | office | 87 | private MemoryChannel Parse(string input) |
15 | office | 88 | { |
89 | var match = readRegex.Match(input); |
||
90 | |||
91 | if (!match.Success) |
||
92 | { |
||
93 | throw new UnmatchedRadioResponseException(Name, input); |
||
94 | } |
||
95 | |||
96 | var currentLocation = match.Result("${currentLocation}"); |
||
97 | var frequency = int.Parse(match.Result("${frequency}")); |
||
98 | var clarifierDirection = char.Parse(match.Result("${clarifierDirection}")); |
||
99 | var clarifierOffset = int.Parse(match.Result("${clarifierOffset}")); |
||
100 | var clar = int.Parse(match.Result("${clar}")) == 0 ? false : true; |
||
101 | var radioMode = char.Parse(match.Result("${mode}")); |
||
46 | office | 102 | var ctcss = int.Parse(match.Result("${ctcss}")); |
15 | office | 103 | var phase = int.Parse(match.Result("${phase}")); |
104 | var tag = int.Parse(match.Result("${tag}")) == 0 ? false : true; |
||
105 | var text = match.Result("${text}"); |
||
106 | |||
107 | var memoryChannel = new MemoryChannel |
||
108 | { |
||
109 | CurrentLocation = currentLocation, |
||
110 | Frequency = frequency, |
||
111 | ClarifierDirection = clarifierDirection, |
||
112 | ClarifierOffset = clarifierOffset, |
||
113 | Clar = clar, |
||
44 | office | 114 | MemoryRadioMode = new MemoryRadioMode(radioMode), |
46 | office | 115 | Ctcss = new Ctcss(ctcss), |
116 | Phase = new Phase(phase), |
||
15 | office | 117 | Tag = tag, |
118 | Text = text |
||
119 | |||
120 | }; |
||
121 | |||
122 | return memoryChannel; |
||
123 | } |
||
124 | |||
46 | office | 125 | public override void Write(Generic.MemoryChannel channel) |
15 | office | 126 | { |
46 | office | 127 | var payload = Constants.Encoding.GetBytes(Compose(new MemoryChannel(channel))); |
15 | office | 128 | |
129 | SerialPort.Write(payload, 0, payload.Length); |
||
130 | } |
||
131 | |||
46 | office | 132 | public override async Task WriteAsync(Generic.MemoryChannel channel, CancellationToken cancellationToken) |
15 | office | 133 | { |
46 | office | 134 | var compose = Compose(new MemoryChannel(channel)); |
15 | office | 135 | |
136 | var payload = Constants.Encoding.GetBytes(compose); |
||
137 | |||
138 | await SerialPort.WriteAsync(payload, 0, payload.Length, cancellationToken); |
||
139 | } |
||
140 | } |
||
141 | } |