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