HamBook – Blame information for rev 17
?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 |
||
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}}(?<ctcssMode>[012]{{1}})00(?<phase>[012]{{1}})(?<tag>[01]{{1}})(?<text>[\x20-\x7E]{{0,12}}){Generic.Constants.EOT}$", RegexOptions.Compiled); |
||
29 | } |
||
30 | |||
31 | public override MemoryChannel Read(string location) |
||
32 | { |
||
33 | SerialPort.Write($"{Name}{location}{Generic.Constants.EOT}"); |
||
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 | |||
45 | public override async Task<MemoryChannel> ReadAsync(string location, CancellationToken cancellationToken) |
||
46 | { |
||
47 | var payload = Constants.Encoding.GetBytes($"{Name}{location}{Generic.Constants.EOT}"); |
||
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 | |||
60 | public override bool Set(MemoryChannel channel) |
||
61 | { |
||
62 | var payload = Constants.Encoding.GetBytes(Compose(channel)); |
||
63 | |||
64 | SerialPort.Write(payload, 0, payload.Length); |
||
65 | |||
66 | return Read(channel.CurrentLocation).Equals(channel); |
||
67 | } |
||
68 | |||
69 | public override async Task<bool> SetAsync(MemoryChannel channel, CancellationToken cancellationToken) |
||
70 | { |
||
71 | var compose = Compose(channel); |
||
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 | |||
82 | public override string Compose(MemoryChannel channel) |
||
83 | { |
||
84 | return $"{Name}{channel.CurrentLocation}{channel.Frequency:000000000}{(char)channel.ClarifierDirection}{channel.ClarifierOffset:0000}{(channel.Clar ? 1 : 0)}0{(char)channel.MemoryRadioMode}1{(int)channel.CtcssMode}00{(int)channel.Phase}{(channel.Tag ? 1 : 0)}{channel.Text,-12}{Generic.Constants.EOT}"; |
||
85 | } |
||
86 | |||
87 | public override MemoryChannel Parse(string input) |
||
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}")); |
||
102 | var ctcssMode = int.Parse(match.Result("${ctcssMode}")); |
||
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, |
||
114 | MemoryRadioMode = radioMode, |
||
115 | CtcssMode = ctcssMode, |
||
116 | Phase = phase, |
||
117 | Tag = tag, |
||
118 | Text = text |
||
119 | |||
120 | }; |
||
121 | |||
122 | return memoryChannel; |
||
123 | } |
||
124 | |||
125 | public override void Write(MemoryChannel channel) |
||
126 | { |
||
127 | var payload = Constants.Encoding.GetBytes(Compose(channel)); |
||
128 | |||
129 | SerialPort.Write(payload, 0, payload.Length); |
||
130 | } |
||
131 | |||
132 | public override async Task WriteAsync(MemoryChannel channel, CancellationToken cancellationToken) |
||
133 | { |
||
134 | var compose = Compose(channel); |
||
135 | |||
136 | var payload = Constants.Encoding.GetBytes(compose); |
||
137 | |||
138 | await SerialPort.WriteAsync(payload, 0, payload.Length, cancellationToken); |
||
139 | } |
||
140 | } |
||
141 | } |