HamBook – Diff between revs 46 and 54

Subversion Repositories:
Rev:
Show entire fileIgnore whitespace
Rev 46 Rev 54
Line 1... Line -...
1 using System; -  
2 using System.Collections.Generic; 1 using System.Text.RegularExpressions;
3 using System.ComponentModel; -  
4 using System.IO.Ports; -  
5 using System.Linq; -  
6 using System.Text; 2 using System.Threading;
7 using System.Threading.Tasks; 3 using System.Threading.Tasks;
8 using HamBook.Radios.Generic; 4 using HamBook.Radios.Generic;
9 using static HamBook.Radios.Yaesu.FT_891.Constants; -  
10 using RJCP.IO.Ports; 5 using RJCP.IO.Ports;
11 using System.Text.RegularExpressions; 6 using static HamBook.Radios.Yaesu.FT_891.Constants;
12 using System.Threading; -  
13 using System.Diagnostics; -  
Line 14... Line 7...
14   7  
15 namespace HamBook.Radios.Yaesu.FT_891.CAT 8 namespace HamBook.Radios.Yaesu.FT_891.CAT
16 { 9 {
17 [Radio("Yaesu FT-891")] 10 [Radio("Yaesu FT-891")]
18 public class MT : Generic.CAT.MT 11 public class Mt : Generic.CAT.Mt
19 { 12 {
20 private readonly Regex readRegex; -  
21   -  
Line 22... Line 13...
22 public override CatLength CatLength => new CatLength { Set = 41, Read = 6, Answer = 41}; 13 private readonly Regex _readRegex;
23   14  
24 public MT(SerialPortStream serialPort) : base(serialPort) 15 public Mt(SerialPortStream serialPort) : base(serialPort)
25 { 16 {
26 /// MTP9U026965000+0000004100001AAAAAAAAAAAA; 17 /// MTP9U026965000+0000004100001AAAAAAAAAAAA;
-   18 /// ^---- fixed: 0 in manual, but responds 1; looks like a radio bug
-   19 _readRegex = new Regex(
27 /// ^---- fixed: 0 in manual, but responds 1; looks like a radio bug 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}$",
Line -... Line 21...
-   21 RegexOptions.Compiled);
-   22 }
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); 23  
29 } 24 public override CatLength CatLength => new CatLength { Set = 41, Read = 6, Answer = 41 };
30   25  
31 public override Generic.MemoryChannel Read(string location) 26 public override Generic.MemoryChannel Read(string location)
32 { 27 {
33 SerialPort.Write($"{Name}{location}{Constants.EOT}"); -  
34 var buffer = new byte[CatLength.Answer]; 28 SerialPort.Write($"{Name}{location}{Eot}");
35 if (SerialPort.Read(buffer, 0, CatLength.Answer) != CatLength.Answer) -  
Line 36... Line 29...
36 { 29 var buffer = new byte[CatLength.Answer];
Line 37... Line 30...
37 throw new UnexpectedRadioResponseException(Name, buffer); 30 if (SerialPort.Read(buffer, 0, CatLength.Answer) != CatLength.Answer)
38 } 31 throw new UnexpectedRadioResponseException(Name, buffer);
Line 39... Line 32...
39   32  
-   33 var answer = Encoding.GetString(buffer);
40 var answer = Constants.Encoding.GetString(buffer); 34  
41   35 return Parse(answer);
42 return Parse(answer); 36 }
43 } 37  
44   38 public override async Task<Generic.MemoryChannel> ReadAsync(string location,
45 public override async Task<Generic.MemoryChannel> ReadAsync(string location, CancellationToken cancellationToken) -  
46 { 39 CancellationToken cancellationToken)
47 var payload = Constants.Encoding.GetBytes($"{Name}{location}{Constants.EOT}"); -  
Line 48... Line 40...
48 await SerialPort.WriteAsync(payload, 0, payload.Length); 40 {
Line 49... Line 41...
49 var buffer = new byte[CatLength.Answer]; 41 var payload = Encoding.GetBytes($"{Name}{location}{Eot}");
50 if (await SerialPort.ReadAsync(buffer, 0, CatLength.Answer) != CatLength.Answer) 42 await SerialPort.WriteAsync(payload, 0, payload.Length);
Line 51... Line 43...
51 { 43 var buffer = new byte[CatLength.Answer];
52 throw new UnexpectedRadioResponseException(Name, buffer); 44 if (await SerialPort.ReadAsync(buffer, 0, CatLength.Answer) != CatLength.Answer)
53 } 45 throw new UnexpectedRadioResponseException(Name, buffer);
Line 54... Line 46...
54   46  
Line 55... Line 47...
55 var answer = Constants.Encoding.GetString(buffer); 47 var answer = Encoding.GetString(buffer);
56   48  
Line 57... Line 49...
57 return Parse(answer); 49 return Parse(answer);
58 } 50 }
59   51  
Line 60... Line 52...
60 public override bool Set(Generic.MemoryChannel channel) 52 public override bool Set(Generic.MemoryChannel channel)
Line 61... Line 53...
61 { 53 {
Line 62... Line 54...
62 var payload = Constants.Encoding.GetBytes(Compose(new MemoryChannel(channel))); 54 var payload = Encoding.GetBytes(Compose(new MemoryChannel(channel)));
Line 63... Line 55...
63   55  
64 SerialPort.Write(payload, 0, payload.Length); 56 SerialPort.Write(payload, 0, payload.Length);
Line 65... Line 57...
65   57  
66 return Read(channel.CurrentLocation).Equals(channel); 58 return Read(channel.CurrentLocation).Equals(channel);
-   59 }
67 } 60  
68   61 public override async Task<bool> SetAsync(Generic.MemoryChannel channel, CancellationToken cancellationToken)
Line 69... Line 62...
69 public override async Task<bool> SetAsync(Generic.MemoryChannel channel, CancellationToken cancellationToken) 62 {
70 { 63 var compose = Compose(new MemoryChannel(channel));
71 var compose = Compose(new MemoryChannel(channel)); 64  
Line 72... Line -...
72   -  
73 var payload = Constants.Encoding.GetBytes(compose); -  
74   65 var payload = Encoding.GetBytes(compose);
75 await SerialPort.WriteAsync(payload, 0, payload.Length, cancellationToken); -  
Line 76... Line 66...
76   66  
77 var result = await ReadAsync(channel.CurrentLocation, cancellationToken); 67 await SerialPort.WriteAsync(payload, 0, payload.Length, cancellationToken);
78   68  
79 return result.Equals(channel); 69 var result = await ReadAsync(channel.CurrentLocation, cancellationToken);
Line 114... Line 104...
114 MemoryRadioMode = new MemoryRadioMode(radioMode), 104 MemoryRadioMode = new MemoryRadioMode(radioMode),
115 Ctcss = new Ctcss(ctcss), 105 Ctcss = new Ctcss(ctcss),
116 Phase = new Phase(phase), 106 Phase = new Phase(phase),
117 Tag = tag, 107 Tag = tag,
118 Text = text 108 Text = text
119   -  
120 }; 109 };
Line 121... Line 110...
121   110  
122 return memoryChannel; 111 return memoryChannel;
Line 123... Line 112...
123 } 112 }
124   113  
125 public override void Write(Generic.MemoryChannel channel) 114 public override void Write(Generic.MemoryChannel channel)
Line 126... Line 115...
126 { 115 {
127 var payload = Constants.Encoding.GetBytes(Compose(new MemoryChannel(channel))); 116 var payload = Encoding.GetBytes(Compose(new MemoryChannel(channel)));
Line 128... Line 117...
128   117  
129 SerialPort.Write(payload, 0, payload.Length); 118 SerialPort.Write(payload, 0, payload.Length);
130 } 119 }
Line 131... Line 120...
131   120  
Line 132... Line 121...
132 public override async Task WriteAsync(Generic.MemoryChannel channel, CancellationToken cancellationToken) 121 public override async Task WriteAsync(Generic.MemoryChannel channel, CancellationToken cancellationToken)
133 { 122 {
134 var compose = Compose(new MemoryChannel(channel)); 123 var compose = Compose(new MemoryChannel(channel));
135   124  
136 var payload = Constants.Encoding.GetBytes(compose); 125 var payload = Encoding.GetBytes(compose);