HamBook – Blame information for rev 46

Subversion Repositories:
Rev:
Rev Author Line No. Line
19 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.Threading;
12 using System.Text.RegularExpressions;
13  
14 namespace HamBook.Radios.Yaesu.FT_891.CAT
15 {
16 [Radio("Yaesu FT-891")]
17 public class MC : Generic.CAT.MC
18 {
19 private Regex readRegex;
20  
21 public override CatLength CatLength => new CatLength { Set = 6, Read = 3, Answer = 6 };
22  
23 public MC(SerialPortStream serialPort) : base(serialPort)
24 {
46 office 25 readRegex = new Regex($"^{Name}(?<channel>[0-9PLU]{{3}}){Constants.EOT}$", RegexOptions.Compiled);
19 office 26 }
27  
41 office 28 public override string Read()
19 office 29 {
46 office 30 SerialPort.Write($"{Name}{Constants.EOT}");
19 office 31 var buffer = new byte[CatLength.Answer];
32 if (SerialPort.Read(buffer, 0, CatLength.Answer) != CatLength.Answer)
33 {
34 throw new UnexpectedRadioResponseException(Name, buffer);
35 }
36  
37 var answer = Constants.Encoding.GetString(buffer);
38 var match = readRegex.Match(answer);
39 if (!match.Success)
40 {
41 throw new UnmatchedRadioResponseException(Name, answer);
42 }
41 office 43 return $"{match.Result("${channel}")}";
19 office 44 }
45  
41 office 46 public override async Task<string> ReadAsync(CancellationToken cancellationToken)
19 office 47 {
46 office 48 var payload = Constants.Encoding.GetBytes($"{Name}{Constants.EOT}");
19 office 49 await SerialPort.WriteAsync(payload, 0, payload.Length, cancellationToken);
50 var buffer = new byte[CatLength.Answer];
51 if (await SerialPort.ReadAsync(buffer, 0, CatLength.Answer, cancellationToken) != CatLength.Answer)
52 {
53 throw new UnexpectedRadioResponseException(Name, buffer);
54 }
55  
56 var answer = Constants.Encoding.GetString(buffer);
57 var match = readRegex.Match(answer);
58 if (!match.Success)
59 {
60 throw new UnmatchedRadioResponseException(Name, answer);
61 }
62  
41 office 63 return $"{match.Result("${channel}")}";
19 office 64 }
65  
41 office 66 public override bool Set(string channel)
19 office 67 {
46 office 68 var payload = Constants.Encoding.GetBytes($"{Name}{channel}{Constants.EOT}");
19 office 69  
70 SerialPort.Write(payload, 0, payload.Length);
71  
72 return Read() == channel;
73 }
74  
41 office 75 public override async Task<bool> SetAsync(string channel, CancellationToken cancellationToken)
19 office 76 {
46 office 77 var payload = Constants.Encoding.GetBytes($"{Name}{channel}{Constants.EOT}");
19 office 78  
79 await SerialPort.WriteAsync(payload, 0, payload.Length, cancellationToken);
80  
81 return await ReadAsync(cancellationToken) == channel;
82 }
83  
41 office 84 public override void Write(string channel)
19 office 85 {
46 office 86 SerialPort.Write($"{Name}{channel}{Constants.EOT}");
19 office 87 }
88  
41 office 89 public override async Task WriteAsync(string channel, CancellationToken cancellationToken)
19 office 90 {
46 office 91 var payload = Constants.Encoding.GetBytes($"{Name}{channel}{Constants.EOT}");
19 office 92  
93 await SerialPort.WriteAsync(payload, 0, payload.Length, cancellationToken);
94 }
95 }
96 }