HamBook – Blame information for rev 9

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 office 1 using HamBook.Radios.Generic;
2 using Newtonsoft.Json.Linq;
3 using System;
4 using System.Collections.Concurrent;
5 using System.Collections.Generic;
6 using System.ComponentModel;
7 using System.IO.Ports;
8 using System.Linq;
9 using System.Reflection;
10 using System.Text;
11 using System.Text.RegularExpressions;
12 using System.Threading.Tasks;
13 using static HamBook.Radios.Yaesu.FT_891.Constants;
9 office 14 using RJCP.IO.Ports;
15 using System.Threading;
1 office 16  
9 office 17 namespace HamBook.Radios.Yaesu.FT_891.CAT
1 office 18 {
19 [Radio("Yaesu FT-891")]
20 public class MD : Generic.CAT.MD
21 {
9 office 22 private readonly Regex readRegex;
23 public override CatLength CatLength => new CatLength { Set = 5, Answer = 5, Read = 4 };
24 public MD(SerialPortStream serialPort) : base(serialPort)
1 office 25 {
9 office 26 readRegex = new Regex($"^MD0(?<mode>[123456789ABCD]){Generic.Constants.EOT}$", RegexOptions.Compiled);
1 office 27 }
28  
7 office 29 public override bool Set(RadioMode radioMode)
1 office 30 {
3 office 31 SerialPort.Write($"{Name}0{radioMode.Mode}{Generic.Constants.EOT}");
7 office 32 return Read() == radioMode;
1 office 33 }
34  
9 office 35 public override async Task<bool> SetAsync(RadioMode radioMode, CancellationToken cancellationToken)
36 {
37 var payload = Encoding.ASCII.GetBytes($"{Name}0{radioMode.Mode}{Generic.Constants.EOT}");
38  
39 await SerialPort.WriteAsync(payload, 0, payload.Length, cancellationToken);
40  
41 return await ReadAsync(cancellationToken) == radioMode;
42 }
43  
1 office 44 public override RadioMode Read()
45 {
3 office 46 SerialPort.Write($"{Name}0{Generic.Constants.EOT}");
9 office 47 var buffer = new byte[CatLength.Answer];
48 if (SerialPort.Read(buffer, 0, CatLength.Answer) != CatLength.Answer)
49 {
50 throw new ArgumentException();
51 }
52  
53 var answer = Encoding.ASCII.GetString(buffer);
54 var match = readRegex.Match(answer);
55 if (!match.Success)
56 {
57 throw new ArgumentException();
58 }
3 office 59 var radioMode = char.Parse(match.Result("${mode}"));
60 return radioMode;
1 office 61 }
9 office 62 public override async Task<RadioMode> ReadAsync(CancellationToken cancellationToken)
63 {
64 var payload = Encoding.ASCII.GetBytes($"{Name}0{Generic.Constants.EOT}");
65 await SerialPort.WriteAsync(payload, 0, payload.Length, cancellationToken);
66 var buffer = new byte[CatLength.Answer];
67 if(await SerialPort.ReadAsync(buffer, 0, buffer.Length, cancellationToken) != CatLength.Answer)
68 {
69 throw new ArgumentException();
70 }
71  
72 var answer = Encoding.ASCII.GetString(buffer);
73 var match = readRegex.Match(answer);
74 if (!match.Success)
75 {
76 throw new ArgumentException();
77 }
78  
79 var radioMode = char.Parse(match.Result("${mode}"));
80 return radioMode;
81 }
1 office 82 }
83 }