-
Notifications
You must be signed in to change notification settings - Fork 1
/
Reader.cs
278 lines (246 loc) · 8.46 KB
/
Reader.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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO.Ports;
using System.Threading;
namespace FlowWorks {
public class Reader
{
// public enums
public enum Event
{
DeviceReady,
ReadFailure,
ResetStream
}
// public delegates
public delegate void EventPublicationFunction(Event e);
public delegate void DeviceResponsePublicationFunction(string s);
public delegate void DeviceDataPublicationFunction(DeviceData deviceData);
public delegate void DeviceStatusPublicationFunction(DeviceStatus deviceStatus);
// public properties
public bool ConnectionIsOpen
{
set { this.connectionIsOpen = value; }
get { return this.connectionIsOpen; }
}
public bool DiscardBytes()
{
try
{
if (!this.serialPort.IsOpen) return false;
int bytesToRead = this.serialPort.BytesToRead;
byte[] byteBuffer = new byte[bytesToRead];
int bytesRead = 0;
if (bytesToRead == 0) return true;
if (this.serialPort.BytesToRead > 0)
{
bytesRead += this.serialPort.Read(byteBuffer, 0, bytesToRead);
}
if (bytesRead != bytesToRead)
{
if (DebugPrintsEnabled) Console.WriteLine(" Failed to clear buffer, only read " + bytesRead);
return false;
}
if (DebugPrintsEnabled) Console.WriteLine(DateTime.UtcNow.ToString("HH:mm:ss.fff") + " Clear Buffer, Discarded " + bytesToRead + " bytes " + byteBuffer[0]);
return true;
}
catch
{
return false;
}
}
public bool DebugPrintsEnabled = true;
public bool ErrorPrintsEnabled = true;
// constructor
public Reader(SerialPort port,
EventPublicationFunction publishEvent,
DeviceResponsePublicationFunction publishResponse,
DeviceDataPublicationFunction publishData,
DeviceStatusPublicationFunction publishStatus,
Form1 form)
{
// constants
this.kDeviceReadyMsg = "senddata";
this.serialPort = port;
// initial state
this.connectionIsOpen = false;
this.deviceResponse = "";
this.readingDeviceResponse = false;
this.lastStreamTime = new DateTime();
this.serialPort.ReadBufferSize = 8192;
// publishing functions
this.PublishEvent = publishEvent;
this.PublishDeviceResponse = publishResponse;
this.PublishDataResponse = publishData;
this.PublishStatusResponse = publishStatus;
this.formVar = form;
}
public void Run()
{ // endless loop
byte[] readBuffer = new byte[4];
bool clearBuffOnce = true;
while (true)
{
if (this.connectionIsOpen)
{
try
{
// After opening connection, discard whatever is in buffer
if (clearBuffOnce)
{
clearBuffOnce = false;
this.DiscardBytes();
}
// read one byte
int bytesRead = 0;
while (bytesRead == 0)
{
bytesRead = this.serialPort.Read(readBuffer, 0, 1);
}
char c = Convert.ToChar(readBuffer[0]);
switch (c)
{
// CR -- end of message from device
case '\r':
if ((this.deviceResponse != this.kDeviceReadyMsg) && (this.deviceResponse != "sendstatus") && (this.deviceResponse.Length > 1))
{
this.PublishDeviceResponse(this.deviceResponse);
if (this.deviceResponse.StartsWith(","))
{
formVar.logData(this.deviceResponse);
}
}
this.deviceResponse = "";
this.readingDeviceResponse = false;
break;
// newline -- ignore
case '\n':
break;
case '&':
if (this.readingDeviceResponse)
{ // normal char inside device response
this.deviceResponse += c;
} else
{
DeviceData deviceData = new DeviceData();
if (this.ParseDataStream(ref deviceData))
{
this.PublishDataResponse(deviceData);
}
}
break;
case '!':
if (this.readingDeviceResponse)
{ // normal char inside device response
this.deviceResponse += c;
}
else
{
DeviceStatus deviceStatus = new DeviceStatus();
if (this.ParseStatusStream(ref deviceStatus))
{
this.PublishStatusResponse(deviceStatus);
}
}
break;
// normal character -- add to the device response
default:
//if the character is not printable (<=0x1F or > 0x7E) except tab 0x09 - then the entire deviceResponse is bogus
if (c != 0x09 && (c < 0x20 || c > 0x7E))
{
this.deviceResponse = "";
this.readingDeviceResponse = false;
}
else
{
this.deviceResponse += c;
this.readingDeviceResponse = true;
}
break;
}
}
// if a read error occurred, connection is probably dead; post error msg and close com port
catch (Exception e)
{
this.PublishEvent(Event.ReadFailure);
if (ErrorPrintsEnabled) Console.WriteLine("Reader thread exception: " + e.Message);
}
}
else
{
clearBuffOnce = true;
// communication not yet established -- wait and try again
Thread.Sleep(100);
}
}
}
private bool ParseDataStream(ref DeviceData deviceData)
{
//if (this.ReadDataBlock(ref this.deviceStatusDataBuffer, deviceStatus.deviceStatusSize, 100))
this.deviceStatusDataBuffer = this.serialPort.ReadLine();
// Log the "sendData" data here, but maybe we don't need to see all that - just avgStream stuff starting with a comma
//formVar.logData(this.deviceStatusDataBuffer);
deviceData.Data = this.deviceStatusDataBuffer;
return true;
}
private bool ParseStatusStream(ref DeviceStatus deviceStatus)
{
//if (this.ReadDataBlock(ref this.deviceStatusDataBuffer, deviceStatus.deviceStatusSize, 100))
this.deviceStatusDataBuffer = this.serialPort.ReadLine();
deviceStatus.Status = this.deviceStatusDataBuffer;
return true;
}
public void Reset()
{
this.connectionIsOpen = false;
this.deviceResponse = "";
this.readingDeviceResponse = false;
if (ErrorPrintsEnabled) Console.WriteLine("Reset Read thread ");
}
public DateTime getTimeStamp()
{
return this.lastStreamTime;
}
public void updateTimeStamp()
{
this.lastStreamTime = DateTime.Now;
}
public void sendReadBufferContinuePacket()
{
//byte[] zlpByte = new byte[] { 0, (byte)'\r' };
byte[] zlpByte = Encoding.ASCII.GetBytes("**HOST_HB\r");
this.serialPort.Write(zlpByte,0,zlpByte.Length);
Thread.Sleep(5);
}
//
// private
//
// constants
private readonly string kDeviceReadyMsg;
// components
private SerialPort serialPort;
private Form1 formVar;
// state
private volatile bool connectionIsOpen;
private volatile string deviceResponse;
private volatile bool readingDeviceResponse;
private DateTime lastStreamTime;
// publication functions
private EventPublicationFunction PublishEvent;
private DeviceResponsePublicationFunction PublishDeviceResponse;
public DeviceDataPublicationFunction PublishDataResponse { get; }
public DeviceStatusPublicationFunction PublishStatusResponse { get; }
// read buffers
private string deviceStatusDataBuffer;
// private methods
private bool ReadDeviceData(ref DeviceData deviceData)
{
this.deviceStatusDataBuffer = this.serialPort.ReadLine();
//deviceStatus.Data = this.deviceStatusDataBuffer;
return true;
}
}
}