HamBook – Blame information for rev 46

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