-
Notifications
You must be signed in to change notification settings - Fork 1
/
SerialManager.cs
210 lines (197 loc) · 7.11 KB
/
SerialManager.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
using System;
using System.Collections;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Net.WebSockets;
using System.IO.Ports;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;
namespace toucca
{
public class SerialManager
{
const byte CMD_GET_SYNC_BOARD_VER = 0xa0;
const byte CMD_NEXT_READ = 0x72;
const byte CMD_GET_UNIT_BOARD_VER = 0xa8;
const byte CMD_MYSTERY1 = 0xa2;
const byte CMD_MYSTERY2 = 0x94;
const byte CMD_START_AUTO_SCAN = 0xc9;
// const byte CMD_BEGIN_WRITE = 0x77;
// const byte CMD_NEXT_WRITE = 0x20;
private Thread _sendThread;
public AutoResetEvent TouchEvent = new(false);
private static SerialPort ComL = new("COM5", 115200);
private static SerialPort ComR = new("COM6", 115200);
bool init = false;
const string SYNC_BOARD_VER = "190523";
const string UNIT_BOARD_VER = "190514";
const string read1 = " 0 0 1 2 3 4 5 15 15 15 15 15 15 11 11 11";
const string read2 = " 11 11 11 128 103 103 115 138 127 103 105 111 126 113 95 100";
const string read3 = " 101 115 98 86 76 67 68 48 117 0 82 154 0 6 35 4";
private readonly Dictionary<byte, List<byte>> readMap = new() {
{ 0x31, ByteHelper.ConvertStringToByteArray(read1) },
{ 0x32, ByteHelper.ConvertStringToByteArray(read2) },
{ 0x33, ByteHelper.ConvertStringToByteArray(read3) }
};
// private readonly byte[] SettingData_160 = new byte[8] { 160, 49, 57, 48, 53, 50, 51, 44 };
private readonly byte[] SettingData_162 = [162, 63, 29];
private readonly byte[] SettingData_148 = [148, 0, 20];
private readonly byte[] SettingData_201 = [201, 0, 73];
private readonly byte[] TouchPackL = new byte[36];
private readonly byte[] TouchPackR = new byte[36];
public void Start()
{
try
{
ComL.Open();
ComR.Open();
}
catch (Exception ex)
{
Logger.Fatal("Failed to open serial ports", ex);
throw;
}
Task.Run(PeriodicReadPortLoop);
_sendThread = new Thread(SendLoop);
}
private void SendLoop()
{
while (true)
{
TouchEvent.WaitOne(1000);
SendTouchState();
}
}
public void SetTouch(int area, bool state)
{
area++; // area: 1 - 240
if (area < 121)
{
area += (area - 1) / 5 * 3 + 7;
TouchPackR.SetBit(area, state);
}
else
{
area -= 120;
area += (area - 1) / 5 * 3 + 7;
TouchPackL.SetBit(area, state);
}
}
private void SendTouchState()
{
if (!init)
{
return;
}
ComL.Write(ToTouchPack(TouchPackL), 0, 36);
ComR.Write(ToTouchPack(TouchPackR), 0, 36);
}
private static byte[] ToTouchPack(byte[] Pack)
{
Pack[0] = 129;
Pack[34]++;
Pack[35] = 128;
Pack[35] = ByteHelper.CalCheckSum(Pack, 36);
if (Pack[34] > 127)
Pack[34] = 0;
return Pack;
}
private async Task PeriodicReadPortLoop()
{
while (true)
{
if (ComL.IsOpen)
ReadAndResp(ComL, 0);
if (ComR.IsOpen)
ReadAndResp(ComR, 1);
await Task.Delay(16);
}
}
private void ReadAndResp(SerialPort Serial, int side)
{
if (Serial.BytesToRead <= 0)
return;
byte inByte = Convert.ToByte(Serial.ReadByte());
string data = Serial.ReadExisting();
List<byte> respBytes = new();
switch (inByte)
{
case CMD_GET_SYNC_BOARD_VER:
init = false;
respBytes.Add(inByte);
respBytes.AddRange(ByteHelper.ConvertStringToByteArray(SYNC_BOARD_VER));
respBytes.Add(44);
break;
case CMD_NEXT_READ:
init = false;
if (readMap.TryGetValue(Convert.ToByte(data[2]), out respBytes))
{
respBytes.Add(ByteHelper.CalCheckSum(respBytes.ToArray(), respBytes.Count));
}
else return;
break;
case CMD_GET_UNIT_BOARD_VER:
init = false;
byte sideByte = side == 0 ? Convert.ToByte('R') : Convert.ToByte('L');
byte unitCheckSum = side == 0 ? (byte)118 : (byte)104;
respBytes.Add(inByte);
respBytes.AddRange(ByteHelper.ConvertStringToByteArray(SYNC_BOARD_VER));
respBytes.Add(sideByte);
for (int i = 0; i < 6; ++i)
{
respBytes.AddRange(ByteHelper.ConvertStringToByteArray(UNIT_BOARD_VER));
}
respBytes.Add(unitCheckSum);
break;
case CMD_MYSTERY1:
init = false;
respBytes.AddRange(SettingData_162);
break;
case CMD_MYSTERY2:
init = false;
respBytes.AddRange(SettingData_148);
break;
case CMD_START_AUTO_SCAN:
respBytes.AddRange(SettingData_201);
init = true;
if (!_sendThread.IsAlive)
{
_sendThread.Start();
}
break;
case 154:
init = false;
Logger.Warn("BAD");
break;
}
Serial.Write(respBytes.ToArray(), 0, respBytes.Count);
}
}
internal static class ByteHelper
{
public static void SetBit(this byte[] self, int index, bool value)
{
var bitArray = new BitArray(self);
bitArray.Set(index, value);
bitArray.CopyTo(self, 0);
}
public static byte CalCheckSum(byte[] _PacketData, int PacketLength)
{
Byte _CheckSumByte = 0x00;
for (int i = 0; i < PacketLength; i++)
_CheckSumByte ^= _PacketData[i];
return _CheckSumByte;
}
public static List<byte> ConvertStringToByteArray(string data)
{
List<byte> tempList = new List<byte>(100);
for (int i = 0; i < data.Length; i++)
tempList.Add(Convert.ToByte(data[i]));
return tempList;
}
}
}