-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathDevice.cs
144 lines (129 loc) · 4.55 KB
/
Device.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
/*
K5TOOL UV-K5 toolkit utility
Copyright (C) 2024 qrp73
https://github.com/qrp73/K5TOOL
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
using System;
using System.Linq;
using System.IO;
using System.IO.Ports;
using System.Threading;
using System.Text;
using K5TOOL.Packets;
namespace K5TOOL
{
public class Device : IDisposable
{
private SerialPort _serial;
public Device(string portName, int baudRate = 38400)
{
Console.WriteLine("Opening {0}", portName);
Logger.Trace("Opening {0}", portName);
_serial = new SerialPort();
_serial.PortName = portName;
_serial.BaudRate = baudRate;
_serial.Parity = Parity.None;
_serial.DataBits = 8;
_serial.StopBits = StopBits.One;
_serial.ReadTimeout = 1500;
_serial.Open();
// empty rx buffer
if (_serial.BytesToRead > 0)
{
var data = RecvRaw(_serial.BytesToRead);
Logger.Trace("open rx: {0}", Utils.ToHex(data));
}
}
public void Dispose()
{
_serial.Dispose();
_serial = null;
}
public byte[] ReadRawBuffer()
{
return RecvRaw(_serial.BytesToRead);
}
private byte[] RecvRaw(int length)
{
var data = new byte[length];
for (var index=0; index < length;)
{
try
{
var read = _serial.Read(data, index, data.Length - index);
//Logger.Trace("rxx: ", data.Skip(index).Take(read));
if (read < 0)
throw new InvalidOperationException();
index += read;
}
catch
{
Logger.LogRxRaw(data.Take(index).ToArray());
throw;
}
}
return data;
}
public void SendRaw(byte[] data)
{
_serial.Write(data, 0, data.Length);
_serial.BaseStream.Flush();
}
public void Send(Packet packet)
{
SendRaw(new Envelope(packet).Serialize());
}
private void LogRecvWithTail(byte[] data)
{
// read the tail of packet data
Thread.Sleep(1000);
var tail = RecvRaw(_serial.BytesToRead);
var fullData = new byte[data.Length + tail.Length];
Array.Copy(data, 0, fullData, 0, data.Length);
Array.Copy(tail, 0, fullData, data.Length, tail.Length);
Logger.LogRxRaw(fullData);
}
public Packet Recv()
{
var data = RecvRaw(4);
if (data.Length != 4 || data[0] != 0xab || data[1] != 0xcd)
{
LogRecvWithTail(data);
throw new InvalidOperationException(
string.Format(
"Recv: invalid protocol header {0}",
Utils.ToHex(data)));
}
var len = data[2] | (data[3] << 8);
//if (len > 0xff)
//{
// LogRecvWithTail(data);
// throw new InvalidOperationException(
// string.Format(
// "Recv: invalid protocol length {0}", len));
//}
var tail = RecvRaw(len + 4);
var fullData = new byte[tail.Length + data.Length];
Array.Copy(data, 0, fullData, 0, data.Length);
Array.Copy(tail, 0, fullData, data.Length, tail.Length);
if (tail.Length != len + 4)
{
LogRecvWithTail(fullData);
throw new InvalidOperationException(
string.Format(
"Recv: invalid tail length {0}, expected {1}", tail.Length, len));
}
return new Envelope(fullData).Deserialize();
}
}
}