HamBook – Blame information for rev 11

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 office 1 using HamBook.Radios.Generic;
3 office 2 using HamBook.Radios.Generic.CAT;
1 office 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.Tasks;
10 using static HamBook.Radios.Yaesu.FT_891.Constants;
9 office 11 using RJCP.IO.Ports;
1 office 12  
9 office 13 namespace HamBook.Radios.Yaesu.FT_891.CAT
1 office 14 {
15 [Radio("Yaesu FT-891")]
3 office 16 public class SC : HamBook.Radios.Generic.CAT.SC
1 office 17 {
9 office 18 private readonly Regex readRegex;
1 office 19  
9 office 20 public override CatLength CatLength => new CatLength { Set = 4, Answer = 4, Read = 3 };
21  
22 public SC(SerialPortStream serialPort) : base(serialPort)
1 office 23 {
9 office 24 readRegex = new Regex($"^{Name}(?<state>[012]){Generic.Constants.EOT}$", RegexOptions.Compiled);
1 office 25 }
26  
27 public override ScanState Read()
28 {
3 office 29 SerialPort.Write($"{Name}{Generic.Constants.EOT}");
9 office 30 var buffer = new byte[CatLength.Answer];
31 if (SerialPort.Read(buffer, 0, CatLength.Answer) != CatLength.Answer)
32 {
11 office 33 throw new UnexpectedRadioResponseException(Name, buffer);
9 office 34 }
35  
11 office 36 var answer = Constants.Encoding.GetString(buffer);
9 office 37 var match = readRegex.Match(answer);
38 if (!match.Success)
39 {
11 office 40 throw new UnmatchedRadioResponseException(Name, answer);
9 office 41 }
1 office 42 var state = char.Parse(match.Result("${state}"));
43 switch(state)
44 {
45 case '0':
46 return ScanState.OFF;
47 case '1':
48 return ScanState.UP;
49 case '2':
50 return ScanState.DOWN;
51 }
52  
53 return ScanState.NONE;
54 }
55  
7 office 56 public override bool Set(ScanState state)
1 office 57 {
58 switch(state)
59 {
60 case ScanState.OFF:
3 office 61 SerialPort.Write($"{Name}0{Generic.Constants.EOT}");
1 office 62 break;
63 case ScanState.UP:
3 office 64 SerialPort.Write($"{Name}1{Generic.Constants.EOT}");
1 office 65 break;
66 case ScanState.DOWN:
3 office 67 SerialPort.Write($"{Name}2{Generic.Constants.EOT}");
1 office 68 break;
69 }
7 office 70  
71 return Read() == state;
1 office 72 }
73  
74  
75 }
76 }