forked from ArduPilot/MissionPlanner
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCommsFile.cs
213 lines (172 loc) · 5.23 KB
/
CommsFile.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
211
212
213
using System;
using System.IO;
using System.Threading;
namespace MissionPlanner.Comms
{
public class CommsFile : CommsBase, ICommsSerial, IDisposable
{
// Properties
private Stream _stream;
private int bytecredit;
private int currentbps;
private DateTime lastread = DateTime.MinValue;
private int lastsecond;
private long length;
private int step;
public CommsFile()
{
}
public CommsFile(string filename)
{
Open(filename);
}
public int bps { get; set; }
// Methods
public void Close()
{
BaseStream.Dispose();
}
public void DiscardInBuffer()
{
}
public void Open()
{
bps = int.MaxValue;
BaseStream = File.OpenRead(PortName);
length = BaseStream.Length;
}
public int Read(byte[] buffer, int offset, int count)
{
if (!IsOpen)
throw new EndOfStreamException("File not open");
while (true)
{
// check if we have credit and continue
if (count < bytecredit)
{
bytecredit -= count;
break;
}
// get the time taken since last read in seconds
var LapsedSinceLastRead = (DateTime.Now - lastread).TotalSeconds;
// escape if we are out of range
if (LapsedSinceLastRead < 0 || LapsedSinceLastRead > 2)
break;
// get our target bps for this time slice.
var targetbps = (int) (bps * LapsedSinceLastRead) + bytecredit;
// check if out target+count is less than our required bps
if (count < targetbps)
{
bytecredit = targetbps - count;
break;
}
Thread.Sleep(1);
}
lastread = DateTime.Now;
if (lastsecond != DateTime.Now.Second)
{
//Console.WriteLine("CommsFile Read bps {0}", currentbps);
currentbps = 0;
lastsecond = DateTime.Now.Second;
}
currentbps += count;
var ret = BaseStream.Read(buffer, offset, count);
if (buffer[0] == 254 && offset == 1)
step = buffer[1] + 5 + 2; // + header + checksum
step -= ret;
// read the timestamp
//if (step == 0)
//BaseStream.Read(new byte[8], 0, 8);
return ret;
}
//int Read(char[] buffer, int offset, int count);
public int ReadByte()
{
return BaseStream.ReadByte();
}
public int ReadChar()
{
return BaseStream.ReadByte();
}
public string ReadExisting()
{
return "";
}
public string ReadLine()
{
return "";
}
//string ReadTo(string value);
public void Write(string text)
{
}
public void Write(byte[] buffer, int offset, int count)
{
}
//void Write(char[] buffer, int offset, int count);
public void WriteLine(string text)
{
}
public void toggleDTR()
{
}
public void Dispose()
{
try
{
if (BaseStream != null)
Close();
}
catch
{
}
}
public Stream BaseStream
{
get => _stream;
set
{
_stream = value;
length = _stream.Length;
}
}
public int BaudRate { get; set; }
public int BytesToRead
{
get
{
if (!BaseStream.CanRead) return 0;
return (int) (BaseStream.Length - BaseStream.Position);
}
}
public int BytesToWrite { get; set; }
public int DataBits { get; set; }
public bool DtrEnable { get; set; }
public bool IsOpen
{
get
{
if (BaseStream != null && BaseStream.CanRead) return BaseStream.Position < length;
return false;
}
}
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; }
//void DiscardOutBuffer();
public void Open(string filename)
{
bps = int.MaxValue;
PortName = filename;
BaseStream = File.OpenRead(PortName);
length = BaseStream.Length;
}
public void SetLength(long length)
{
this.length = length;
}
}
}