HamBook – Blame information for rev 46

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;
11 office 23  
9 office 24 public override CatLength CatLength => new CatLength { Set = 5, Answer = 5, Read = 4 };
11 office 25  
9 office 26 public MD(SerialPortStream serialPort) : base(serialPort)
1 office 27 {
46 office 28 readRegex = new Regex($"^MD0(?<mode>[123456789ABCD]){Constants.EOT}$", RegexOptions.Compiled);
1 office 29 }
30  
43 office 31 public override bool Set(Generic.RadioMode radioMode)
1 office 32 {
46 office 33 SerialPort.Write($"{Name}0{radioMode.Code}{Constants.EOT}");
11 office 34  
7 office 35 return Read() == radioMode;
1 office 36 }
37  
43 office 38 public override async Task<bool> SetAsync(Generic.RadioMode radioMode, CancellationToken cancellationToken)
9 office 39 {
46 office 40 var payload = Constants.Encoding.GetBytes($"{Name}0{radioMode.Code}{Constants.EOT}");
9 office 41  
42 await SerialPort.WriteAsync(payload, 0, payload.Length, cancellationToken);
43  
44 office 44 return (await ReadAsync(cancellationToken)).Code == radioMode.Code;
9 office 45 }
46  
43 office 47 public override Generic.RadioMode Read()
1 office 48 {
46 office 49 SerialPort.Write($"{Name}0{Constants.EOT}");
9 office 50 var buffer = new byte[CatLength.Answer];
51 if (SerialPort.Read(buffer, 0, CatLength.Answer) != CatLength.Answer)
52 {
11 office 53 throw new UnexpectedRadioResponseException(Name, buffer);
9 office 54 }
55  
11 office 56 var answer = Constants.Encoding.GetString(buffer);
9 office 57 var match = readRegex.Match(answer);
58 if (!match.Success)
59 {
11 office 60 throw new UnmatchedRadioResponseException(Name, answer);
9 office 61 }
3 office 62 var radioMode = char.Parse(match.Result("${mode}"));
43 office 63 return new RadioMode(radioMode);
1 office 64 }
43 office 65 public override async Task<Generic.RadioMode> ReadAsync(CancellationToken cancellationToken)
9 office 66 {
46 office 67 var payload = Constants.Encoding.GetBytes($"{Name}0{Constants.EOT}");
9 office 68 await SerialPort.WriteAsync(payload, 0, payload.Length, cancellationToken);
69 var buffer = new byte[CatLength.Answer];
11 office 70 if (await SerialPort.ReadAsync(buffer, 0, buffer.Length, cancellationToken) != CatLength.Answer)
9 office 71 {
11 office 72 throw new UnexpectedRadioResponseException(Name, buffer);
9 office 73 }
74  
11 office 75 var answer = Constants.Encoding.GetString(buffer);
9 office 76 var match = readRegex.Match(answer);
77 if (!match.Success)
78 {
11 office 79 throw new UnmatchedRadioResponseException(Name, answer);
9 office 80 }
81  
82 var radioMode = char.Parse(match.Result("${mode}"));
43 office 83 return new RadioMode(radioMode);
9 office 84 }
1 office 85 }
86 }