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