HamBook – Blame information for rev 46
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
9 | office | 1 | using HamBook.Radios.Generic; |
2 | using System; |
||
3 | using System.IO.Ports; |
||
4 | using System.Text.RegularExpressions; |
||
5 | using RJCP.IO.Ports; |
||
6 | using System.Text; |
||
7 | |||
8 | namespace HamBook.Radios.Yaesu.FT_891.CAT |
||
9 | { |
||
10 | [Radio("Yaesu FT-891")] |
||
11 | public class RI : Generic.CAT.RI |
||
12 | { |
||
13 | private readonly Regex readRegex; |
||
14 | |||
15 | public override CatLength CatLength => new CatLength { Answer = 5, Read = 4 }; |
||
16 | |||
17 | public RI(SerialPortStream serialPort) : base(serialPort) |
||
18 | { |
||
46 | office | 19 | readRegex = new Regex($"^{Name}(?<type>[03AB]{{1}})(?<state>[01]{{1}}){Constants.EOT}$", RegexOptions.Compiled); |
9 | office | 20 | } |
21 | |||
22 | public override bool Read(RadioInformationType type) |
||
23 | { |
||
46 | office | 24 | SerialPort.Write($"{Name}{P1(type)}{Constants.EOT}"); |
9 | office | 25 | var buffer = new byte[CatLength.Answer]; |
26 | if (SerialPort.Read(buffer, 0, CatLength.Answer) != CatLength.Answer) |
||
27 | { |
||
11 | office | 28 | throw new UnexpectedRadioResponseException(Name, buffer); |
9 | office | 29 | } |
30 | |||
11 | office | 31 | var answer = Constants.Encoding.GetString(buffer); |
9 | office | 32 | var match = readRegex.Match(answer); |
33 | if (!match.Success) |
||
34 | { |
||
11 | office | 35 | throw new UnmatchedRadioResponseException(Name, answer); |
9 | office | 36 | } |
37 | |||
38 | var information = P1(char.Parse(match.Result("${type}"))); |
||
39 | |||
40 | if(information != type) |
||
41 | { |
||
42 | throw new ArgumentException(); |
||
43 | } |
||
44 | |||
45 | var state = int.Parse(match.Result("${state}")); |
||
46 | return state == 1; |
||
47 | } |
||
48 | |||
49 | private static RadioInformationType P1(char type) |
||
50 | { |
||
51 | switch (type) |
||
52 | { |
||
53 | case '0': |
||
54 | return RadioInformationType.HI_SWR; |
||
55 | case '3': |
||
56 | return RadioInformationType.REC; |
||
57 | case '4': |
||
58 | return RadioInformationType.PLAY; |
||
59 | case 'A': |
||
60 | return RadioInformationType.TX_LED; |
||
61 | case 'B': |
||
62 | return RadioInformationType.RX_LED; |
||
63 | default: |
||
64 | throw new ArgumentException(); |
||
65 | } |
||
66 | } |
||
67 | |||
68 | private static char P1(RadioInformationType type) |
||
69 | { |
||
70 | switch (type) |
||
71 | { |
||
72 | case RadioInformationType.HI_SWR: |
||
73 | return '0'; |
||
74 | case RadioInformationType.REC: |
||
75 | return '3'; |
||
76 | case RadioInformationType.PLAY: |
||
77 | return '4'; |
||
78 | case RadioInformationType.TX_LED: |
||
79 | return 'A'; |
||
80 | case RadioInformationType.RX_LED: |
||
81 | return 'B'; |
||
82 | default: |
||
83 | throw new ArgumentException(); |
||
84 | } |
||
85 | } |
||
86 | } |
||
87 | } |