@@ -21,34 +21,31 @@ public class SerialManager
21
21
const byte CMD_MYSTERY1 = 0xa2 ;
22
22
const byte CMD_MYSTERY2 = 0x94 ;
23
23
const byte CMD_START_AUTO_SCAN = 0xc9 ;
24
- // const byte CMD_BEGIN_WRITE = 0x77;
25
- // const byte CMD_NEXT_WRITE = 0x20;
24
+ const byte CMD_BEGIN_WRITE = 0x77 ;
25
+ const byte CMD_NEXT_WRITE = 0x20 ;
26
26
27
- private Thread _sendThread ;
28
- public AutoResetEvent TouchEvent = new ( false ) ;
27
+ private static Thread _sendThread ;
28
+ public static AutoResetEvent TouchEvent = new ( false ) ;
29
29
30
30
private static SerialPort ComL = new ( "COM5" , 115200 ) ;
31
31
private static SerialPort ComR = new ( "COM6" , 115200 ) ;
32
32
33
- bool init = false ;
33
+ static bool init = false ;
34
34
const string SYNC_BOARD_VER = "190523" ;
35
35
const string UNIT_BOARD_VER = "190514" ;
36
- const string read1 = " 0 0 1 2 3 4 5 15 15 15 15 15 15 11 11 11" ;
37
- const string read2 = " 11 11 11 128 103 103 115 138 127 103 105 111 126 113 95 100" ;
38
- const string read3 = " 101 115 98 86 76 67 68 48 117 0 82 154 0 6 35 4" ;
39
- private readonly Dictionary < byte , List < byte > > readMap = new ( ) {
40
- { 0x31 , ByteHelper . ConvertStringToByteArray ( read1 ) } ,
41
- { 0x32 , ByteHelper . ConvertStringToByteArray ( read2 ) } ,
42
- { 0x33 , ByteHelper . ConvertStringToByteArray ( read3 ) }
36
+ private static readonly Dictionary < byte , string > readMap = new ( ) {
37
+ { 0x30 , " 0 0 1 2 3 4 5 15 15 15 15 15 15 11 11 11" } ,
38
+ { 0x31 , " 11 11 11 128 103 103 115 138 127 103 105 111 126 113 95 100" } ,
39
+ { 0x33 , " 101 115 98 86 76 67 68 48 117 0 82 154 0 6 35 4" }
43
40
} ;
44
41
// private readonly byte[] SettingData_160 = new byte[8] { 160, 49, 57, 48, 53, 50, 51, 44 };
45
- private readonly byte [ ] SettingData_162 = [ 162 , 63 , 29 ] ;
46
- private readonly byte [ ] SettingData_148 = [ 148 , 0 , 20 ] ;
47
- private readonly byte [ ] SettingData_201 = [ 201 , 0 , 73 ] ;
48
- private readonly byte [ ] TouchPackL = new byte [ 36 ] ;
49
- private readonly byte [ ] TouchPackR = new byte [ 36 ] ;
42
+ private static readonly byte [ ] SettingData_162 = [ 162 , 63 , 29 ] ;
43
+ private static readonly byte [ ] SettingData_148 = [ 148 , 0 , 20 ] ;
44
+ private static readonly byte [ ] SettingData_201 = [ 201 , 0 , 73 ] ;
45
+ private static byte [ ] TouchPackL = new byte [ 36 ] ;
46
+ private static byte [ ] TouchPackR = new byte [ 36 ] ;
50
47
51
- public void Start ( )
48
+ public static void Start ( )
52
49
{
53
50
try
54
51
{
@@ -64,7 +61,7 @@ public void Start()
64
61
_sendThread = new Thread ( SendLoop ) ;
65
62
}
66
63
67
- private void SendLoop ( )
64
+ private static void SendLoop ( )
68
65
{
69
66
while ( true )
70
67
{
@@ -73,7 +70,7 @@ private void SendLoop()
73
70
}
74
71
}
75
72
76
- public void SetTouch ( int area , bool state )
73
+ public static void SetTouch ( int area , bool state )
77
74
{
78
75
area ++ ; // area: 1 - 240
79
76
if ( area < 121 )
@@ -89,7 +86,7 @@ public void SetTouch(int area, bool state)
89
86
}
90
87
}
91
88
92
- private void SendTouchState ( )
89
+ private static void SendTouchState ( )
93
90
{
94
91
if ( ! init )
95
92
{
@@ -109,7 +106,7 @@ private static byte[] ToTouchPack(byte[] Pack)
109
106
Pack [ 34 ] = 0 ;
110
107
return Pack ;
111
108
}
112
- private async Task PeriodicReadPortLoop ( )
109
+ private static async Task PeriodicReadPortLoop ( )
113
110
{
114
111
while ( true )
115
112
{
@@ -120,7 +117,7 @@ private async Task PeriodicReadPortLoop()
120
117
await Task . Delay ( 16 ) ;
121
118
}
122
119
}
123
- private void ReadAndResp ( SerialPort Serial , int side )
120
+ private static void ReadAndResp ( SerialPort Serial , int side )
124
121
{
125
122
if ( Serial . BytesToRead <= 0 )
126
123
return ;
@@ -130,23 +127,31 @@ private void ReadAndResp(SerialPort Serial, int side)
130
127
List < byte > respBytes = new ( ) ;
131
128
switch ( inByte )
132
129
{
130
+
133
131
case CMD_GET_SYNC_BOARD_VER :
134
132
init = false ;
133
+ Logger . Info ( $ "CMD_GET_SYNC_BOARD_VER { side } ") ;
135
134
respBytes . Add ( inByte ) ;
136
135
respBytes . AddRange ( ByteHelper . ConvertStringToByteArray ( SYNC_BOARD_VER ) ) ;
137
136
respBytes . Add ( 44 ) ;
138
137
139
138
break ;
140
139
case CMD_NEXT_READ :
141
140
init = false ;
142
- if ( readMap . TryGetValue ( Convert . ToByte ( data [ 2 ] ) , out respBytes ) )
141
+ Logger . Info ( $ "CMD_NEXT_READ { side } { Convert . ToByte ( data [ 2 ] ) } ") ;
142
+ if ( readMap . TryGetValue ( Convert . ToByte ( data [ 2 ] ) , out string readData ) )
143
143
{
144
+ respBytes . AddRange ( ByteHelper . ConvertStringToByteArray ( readData ) ) ;
144
145
respBytes . Add ( ByteHelper . CalCheckSum ( respBytes . ToArray ( ) , respBytes . Count ) ) ;
145
146
}
146
- else return ;
147
+ else
148
+ {
149
+ Logger . Info ( $ "EXTRA READ { side } { Convert . ToByte ( data [ 2 ] ) } ") ;
150
+ } ;
147
151
break ;
148
152
case CMD_GET_UNIT_BOARD_VER :
149
153
init = false ;
154
+ Logger . Info ( $ "CMD_GET_UNIT_BOARD_VER { side } ") ;
150
155
byte sideByte = side == 0 ? Convert . ToByte ( 'R' ) : Convert . ToByte ( 'L' ) ;
151
156
byte unitCheckSum = side == 0 ? ( byte ) 118 : ( byte ) 104 ;
152
157
respBytes . Add ( inByte ) ;
@@ -160,13 +165,16 @@ private void ReadAndResp(SerialPort Serial, int side)
160
165
break ;
161
166
case CMD_MYSTERY1 :
162
167
init = false ;
168
+ Logger . Info ( $ "CMD_MYSTERY1 { side } ") ;
163
169
respBytes . AddRange ( SettingData_162 ) ;
164
170
break ;
165
171
case CMD_MYSTERY2 :
166
172
init = false ;
173
+ Logger . Info ( $ "CMD_MYSTERY2 { side } ") ;
167
174
respBytes . AddRange ( SettingData_148 ) ;
168
175
break ;
169
176
case CMD_START_AUTO_SCAN :
177
+ Logger . Info ( $ "CMD_START_AUTO_SCAN { side } ") ;
170
178
respBytes . AddRange ( SettingData_201 ) ;
171
179
init = true ;
172
180
if ( ! _sendThread . IsAlive )
@@ -178,7 +186,15 @@ private void ReadAndResp(SerialPort Serial, int side)
178
186
init = false ;
179
187
Logger . Warn ( "BAD" ) ;
180
188
break ;
181
-
189
+ case CMD_BEGIN_WRITE :
190
+ Logger . Info ( $ "CMD_BEGIN_WRITE { side } ") ;
191
+ break ;
192
+ case CMD_NEXT_WRITE :
193
+ Logger . Info ( $ "CMD_NEXT_WRITE { side } ") ;
194
+ break ;
195
+ default :
196
+ Logger . Info ( $ "COMMAND { Convert . ToByte ( inByte ) } { side } ") ;
197
+ break ;
182
198
}
183
199
Serial . Write ( respBytes . ToArray ( ) , 0 , respBytes . Count ) ;
184
200
}
0 commit comments