-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSerialConn.cpp
More file actions
359 lines (325 loc) · 11.1 KB
/
SerialConn.cpp
File metadata and controls
359 lines (325 loc) · 11.1 KB
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
/*
* SerialConn.cpp
* Film Scanner Master PC Control Software by Kyle Mikolajczyk
* kyle@kylem.org
*/
#include "SerialConn.h"
/*
* Constructor for the SerialConn class.
*/
SerialConn::SerialConn(int baudRate, const char* portId) : io(), work(io), serial(io, portId)
{
try {
serial.set_option(serial_port_base::baud_rate(baudRate));
serial.set_option(serial_port_base::character_size(8));
serial.set_option(serial_port_base::parity(serial_port_base::parity::none));
serial.set_option(serial_port_base::stop_bits(serial_port_base::stop_bits::one));
serial.set_option(serial_port_base::flow_control(serial_port_base::flow_control::none));
// Start the io_service in a separate thread
ioThread = std::thread([this]() { io.run(); });
}
catch (boost::system::system_error& e) {
std::cerr << "Error: " << e.what() << std::endl;
}
}
void SerialConn::checkDeadline(boost::asio::deadline_timer* timer, boost::asio::serial_port* serial)
{
if (timer->expires_at() <= boost::asio::deadline_timer::traits_type::now())
{
timed_out = true;
boost::system::error_code ignored_ec;
serial->cancel(ignored_ec);
timer->expires_at(boost::posix_time::pos_infin);
}
timer->async_wait([this, timer, serial](const boost::system::error_code& ec)
{
if (!ec)
{
checkDeadline(timer, serial);
}
});
}
/*
* Reads a single character from the serial connection.
*/
char SerialConn::getCharFromConn()
{
//std::cout << "Reading char" << std::endl;
readComplete = false;
timed_out = false;
boost::asio::deadline_timer timer(io);
timer.expires_from_now(boost::posix_time::seconds(5)); // Set timeout duration
checkDeadline(&timer, &serial);
serial.async_read_some(boost::asio::buffer(&readChar, 1),
[this, &timer](const boost::system::error_code& ec, std::size_t bytes_transferred)
{
if (!ec && bytes_transferred > 0)
{
timer.expires_at(boost::posix_time::pos_infin); // Cancel the timer
readComplete = true;
}
else if (ec != boost::asio::error::operation_aborted)
{
std::cerr << "Error: " << ec.message() << std::endl;
}
});
//std::cout << "Done reading char" << std::endl;
// Run the I/O service to process the asynchronous operation
while (!readComplete && !timed_out)
{
//std::cout << "In while loop" << std::endl;
if (io.poll_one() == 0) {
std::this_thread::sleep_for(std::chrono::milliseconds(10));
}
}
//std::cout << "Out of while loop" << std::endl;
// Ensure the timer is canceled and no longer active
boost::system::error_code ignored_ec;
timer.cancel(ignored_ec);
if (timed_out)
{
std::cerr << "Timeout reading from serial connection." << std::endl;
return '\t'; // Return a null character in case of timeout
}
if (readComplete)
{
// NOTE: When debugging you can remove the comment to see the raw chars being received
//std::cout << "Finished reading char: " << readChar << std::endl;
return readChar;
}
return '\t'; // Return a null character in case of error
}
/*
* Reads from the serial connection until the start and end delimiters are found.
*/
char* SerialConn::readMessage(const char startDelim, const char endDelim)
{
try {
char* buffer = new char[256](); // Initialize buffer to zero
int i = 0;
bool started = false;
//std::cout << "Starting read message..." << std::endl;
while (true) {
//std::cout << "Reading char" << std::endl;
char c = getCharFromConn(); // NOTE: Can hang here
//std::cout << c << std::endl;
if (c == '\t') {
std::cerr << "Error reading from serial connection." << std::endl;
return nullptr;
}
if (c == startDelim) {
started = true; // Start reading when start delimiter is found
continue;
}
if (started) {
if (isprint(static_cast<unsigned char>(c)) || c == '\n' || c == '\r') { // Check for valid characters
buffer[i] = c;
if (buffer[i] == endDelim) {
buffer[i] = '\0';
break;
}
i++;
if (i >= 255) { // Prevent buffer overflow
buffer[255] = '\0';
break;
}
}
}
}
return buffer;
}
catch (boost::system::system_error& e) {
std::cout << "error happened";
std::cerr << "Error: " << e.what() << std::endl;
}
return nullptr;
}
void SerialConn::parseMessage(const char* message)
{
// Determine the message type from the message string
Arduino_Message_Type messageType;
int value = -1;
if (strcmp(message, "ACK") == 0)
{
messageType = ACK;
}
else if (strcmp(message, "READY") == 0)
{
messageType = READY;
}
else if (strcmp(message, "READY_RED") == 0)
{
messageType = READY_RED;
}
else if (strcmp(message, "READY_GREEN") == 0)
{
messageType = READY_GREEN;
}
else if (strcmp(message, "READY_BLUE") == 0)
{
messageType = READY_BLUE;
}
else if (strcmp(message, "READY_FRAME") == 0)
{
messageType = READY_FRAME;
}
else if (strncmp(message, "CURRENT_FRAME_ID:", 17) == 0)
{
messageType = CURRENT_FRAME_ID;
char* endPtr;
value = strtol(message + 17, &endPtr, 10); // Extract the number after "CURRENT_FRAME_ID:", 10 here is base10 number system
if (*endPtr != '\0') {
std::cerr << "Invalid number format in message: " << message << std::endl;
return;
}
}
else if (strncmp(message, "STEPPER_POS:", 12) == 0)
{
messageType = CURRENT_STEPPER_POS;
char* endPtr;
value = strtol(message + 12, &endPtr, 10); // Extract the number after "CURRENT_FRAME_ID:", 10 here is base10 number system
if (*endPtr != '\0') {
std::cerr << "Invalid number format in message: " << message << std::endl;
return;
}
}
else
{
std::cerr << "Unknown message received from Arduino: " << message << std::endl;
return;
}
// Handle the message
handleArduinoMessage(messageType, value);
}
void SerialConn::handleArduinoMessage(Arduino_Message_Type messageType, int value)
{
switch (messageType)
{
case ACK:
std::cout << "Received ACK from Arduino" << std::endl;
// Handle ACK message
break;
case READY:
std::cout << "Arduino is ready to receive a command" << std::endl;
// Handle READY message
break;
case READY_RED:
std::cout << "Arduino is ready to scan RED color" << std::endl;
// Handle READY_RED message
break;
case READY_GREEN:
std::cout << "Arduino is ready to scan GREEN color" << std::endl;
// Handle READY_GREEN message
break;
case READY_BLUE:
std::cout << "Arduino is ready to scan BLUE color" << std::endl;
// Handle READY_BLUE message
break;
case READY_FRAME:
std::cout << "Frame ready to be captured (all colors)" << std::endl;
// Handle READY_FRAME message
break;
case CURRENT_FRAME_ID:
std::cout << "Current Frame ID received: " << value << std::endl;
// Handle CURRENT_FRAME_ID message with frameId
break;
case CURRENT_STEPPER_POS:
std::cout << "Stepper position received: " << value << std::endl;
// Handle STEPPER_POS message
break;
default:
std::cerr << "Unknown message type received from Arduino" << std::endl;
break;
}
}
void SerialConn::printToSerialWithDelimiters(const char* message)
{
// Buffer to hold the formatted message
char formattedMessage[MSG_SIZE];
for (int i = 0; i < MSG_SIZE; i++) {
formattedMessage[i] = 0;
}
// Add the start delimiter to the message
formattedMessage[0] = MSG_START_DELIM;
int i = 1;
// Copy the message to the buffer
for (int j = 0; j < strlen(message); j++) {
formattedMessage[i] = message[j];
i++;
}
// Add the end delimiter to the message
formattedMessage[i] = MSG_END_DELIM;
// Write the message to the serial connection
boost::asio::write(serial, buffer(formattedMessage, MSG_SIZE));
}
/*
* Messag Types TO Arduino
* SET_COLOR:RED
* SET_COLOR:GREEN
* SET_COLOR:BLUE
* GOTO_FRAME_ID:0- Frame ID; This is to advance (or rewind) to the given frame ID
* FRAME_STEP:0- Frame step; This is to advance (or rewind) the given number of frames
* GOTO_STEPPER_POS:0- Stepper position; This is to move the stepper to the given position
* GET_FRAME_ID:0- Get frame ID; This is to get the current frame ID
* GET_STEPPER_POS:0- Get stepper position; This is to get the current stepper position
* SET_FRAME_OFFSET:- Set frame offset; This is to set the frame offset
* RESET_FRAME_ID- Reset frame ID; This is to reset the frame ID to the given value
*/
void SerialConn::sendCommand(Arduino_Command_Type command)
{
// Simple overload to send a command without a required value
sendCommand(command, 0);
}
void SerialConn::sendCommand(Arduino_Command_Type command, int value)
{
std::string enrichedCommand;
switch (command)
{
case SET_COLOR_RED:
printToSerialWithDelimiters("SET_COLOR_RED");
break;
case SET_COLOR_GREEN:
printToSerialWithDelimiters("SET_COLOR_GREEN");
break;
case SET_COLOR_BLUE:
printToSerialWithDelimiters("SET_COLOR_BLUE");
break;
case GOTO_FRAME_ID:
enrichedCommand = "GOTO_FRAME_ID:" + std::to_string(value);
printToSerialWithDelimiters(enrichedCommand.c_str());
break;
case FRAME_STEP:
enrichedCommand = "FRAME_STEP:" + std::to_string(value);
printToSerialWithDelimiters(enrichedCommand.c_str());
break;
case GOTO_STEPPER_POS:
enrichedCommand = "GOTO_STEPPER_POS:" + std::to_string(value);
printToSerialWithDelimiters(enrichedCommand.c_str());
break;
case GET_FRAME_ID:
printToSerialWithDelimiters("GET_FRAME_ID");
break;
case GET_STEPPER_POS:
printToSerialWithDelimiters("GET_STEPPER_POS");
break;
case SET_FRAME_OFFSET:
enrichedCommand = "SET_FRAME_OFFSET:" + std::to_string(value);
printToSerialWithDelimiters(enrichedCommand.c_str());
break;
case RESET_FRAME_ID:
printToSerialWithDelimiters("RESET_FRAME_ID");
break;
default:
std::cerr << "Unknown command type received" << std::endl;
break;
}
}
SerialConn::~SerialConn()
{
// Destructor body can be empty as the io_service and serial_port
// will automatically clean up their resources.
io.stop();
if (ioThread.joinable()) {
ioThread.join();
}
}