forked from ArduPilot/MissionPlanner
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCommsInjection.cs
187 lines (154 loc) · 4.74 KB
/
CommsInjection.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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace MissionPlanner.Comms
{
/// <summary>
/// use AppendBuffer to populate the Read buffer, and WriteCallback to send
/// </summary>
public class CommsInjection : ICommsSerial
{
private readonly CircularBuffer<byte> _bufferRX = new CircularBuffer<byte>(1024 * 100);
public CommsInjection()
{
BaseStream = new CommsStream(this, 0);
}
public void AppendBuffer(byte[] indata)
{
lock (_bufferRX)
{
foreach (var b in indata)
{
_bufferRX.Add(b);
}
BaseStream.SetLength(BaseStream.Length + indata.Length);
}
}
public EventHandler<int> ReadBufferUpdate;
public EventHandler<IEnumerable<byte>> WriteCallback;
public void Close()
{
lock (_bufferRX)
_bufferRX.Clear();
}
public void DiscardInBuffer()
{
lock (_bufferRX)
_bufferRX.Clear();
}
public void Open()
{
lock (_bufferRX)
_bufferRX.Clear();
}
public int Read(byte[] buffer, int offset, int count)
{
var counttimeout = 0;
while (BytesToRead == 0)
{
Thread.Sleep(1);
if (counttimeout > ReadTimeout)
throw new Exception("CommsInjection Timeout on read");
counttimeout++;
}
lock (_bufferRX)
{
var read = Math.Min(count, _bufferRX.Length());
for (var i = 0; i < read; i++) buffer[offset + i] = _bufferRX.Read();
return read;
}
}
public int ReadByte()
{
var buffer = new byte[1];
Read(buffer, 0, 1);
return buffer[0];
}
public int ReadChar()
{
return ReadByte();
}
public string ReadExisting()
{
var data = new byte[0];
if (data.Length > 0)
Read(data, 0, data.Length);
var line = Encoding.ASCII.GetString(data, 0, data.Length);
return line;
}
public string ReadLine()
{
var temp = new byte[4000];
var count = 0;
var timeout = 0;
while (timeout <= 100)
{
if (!IsOpen) break;
if (BytesToRead > 0)
{
var letter = (byte) ReadByte();
temp[count] = letter;
if (letter == '\n') // normal line
break;
count++;
if (count == temp.Length)
break;
timeout = 0;
}
else
{
timeout++;
Thread.Sleep(5);
}
}
Array.Resize(ref temp, count + 1);
return Encoding.ASCII.GetString(temp, 0, temp.Length);
}
public void WriteLine(string line)
{
line = line + "\n";
Write(line);
}
public void Write(string line)
{
var data = new ASCIIEncoding().GetBytes(line);
Write(data, 0, data.Length);
}
public void Write(byte[] buffer, int offset, int count)
{
WriteCallback?.Invoke(this, buffer.Skip(offset).Take(count));
}
public void toggleDTR()
{
}
public void Dispose()
{
Close();
}
public Stream BaseStream { get; }
public int BaudRate { get; set; }
public int BytesToRead
{
get
{
ReadBufferUpdate?.Invoke(this, 0);
lock (_bufferRX)
return _bufferRX.Length();
}
}
public int BytesToWrite { get; }
public int DataBits { get; set; }
public bool DtrEnable { get; set; }
public bool IsOpen => true;
public string PortName { get; set; }
public int ReadBufferSize { get; set; }
public int ReadTimeout { get; set; }
public bool RtsEnable { get; set; }
public int WriteBufferSize { get; set; }
public int WriteTimeout { get; set; }
}
}