-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMTISerial.cpp
469 lines (408 loc) · 11.9 KB
/
MTISerial.cpp
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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
#define SERIAL_HARDWARE_FLOW_CONTROL 0
#include "MTISerial.h"
#ifdef MTI_UNIX
#include <poll.h>
#include <sys/ioctl.h>
#include <time.h>
#endif
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// MTISerialIO Implementation
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
MTISerialIO::MTISerialIO ()
{
m_hFile = 0;
#ifdef MTI_WINDOWS
m_hevtOverlapped = 0;
#endif
}
MTISerialIO::~MTISerialIO ()
{
if(m_hFile != 0)
Close();
}
bool MTISerialIO::IsPortAvailable (const char* port)
{
#ifdef MTI_WINDOWS
HANDLE hFile = ::CreateFile(port, GENERIC_READ|GENERIC_WRITE, 0, 0, OPEN_EXISTING, 0, 0);
if (hFile == INVALID_HANDLE_VALUE)
return false;
::CloseHandle(hFile);
#endif
#ifdef MTI_UNIX
int hFile = open( port, O_RDWR | O_NOCTTY | O_NONBLOCK);
if( hFile == -1 )
return false;
close(hFile);
#endif
return true;
}
long MTISerialIO::Open (const char* port, unsigned int baudRate, unsigned int inQueue, unsigned int outQueue)
{
if (m_hFile)
return MTI_ERR_SERIALCOMM;
#ifdef MTI_WINDOWS
// Open the device
m_hFile = ::CreateFile(port,GENERIC_READ|GENERIC_WRITE, 0, 0, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, 0);
if (m_hFile == INVALID_HANDLE_VALUE)
{
m_hFile = 0;
return MTI_ERR_INVALID_DEVICEID;
}
m_hevtOverlapped = ::CreateEvent(0,true,false,0);
if (m_hevtOverlapped == 0)
{
::CloseHandle(m_hFile);
m_hFile = 0;
return MTI_ERR_SERIALCOMM;
}
// Setup the COM-port
if (inQueue || outQueue)
{
if (!::SetupComm(m_hFile,inQueue,outQueue))
{
Close();
return MTI_ERR_SERIALCOMM;
}
}
DWORD dwMask = EV_BREAK|EV_ERR|EV_RXCHAR;
if (!::SetCommMask(m_hFile,dwMask))
return MTI_ERR_SERIALCOMM;
// Setup the device for default settings
COMMCONFIG commConfig = {0};
DWORD dwSize = sizeof(commConfig);
commConfig.dwSize = dwSize;
::GetDefaultCommConfig(port,&commConfig,&dwSize);
::SetCommConfig(m_hFile,&commConfig,dwSize);
#endif
#ifdef MTI_UNIX
m_hFile = open(port,O_RDWR|O_NOCTTY|O_NONBLOCK); //Open in non blocking read/write mode
if (m_hFile == -1)
{
m_hFile = 0;
return MTI_ERR_INVALID_DEVICEID;
}
#endif
SetBlockingMode();
SetSerialParams(baudRate);
return MTI_SUCCESS;
}
long MTISerialIO::Close (void)
{
if (m_hFile == 0)
return MTI_SUCCESS;
#ifdef MTI_WINDOWS
if (m_hevtOverlapped)
{
::CloseHandle(m_hevtOverlapped);
m_hevtOverlapped = 0;
}
::CloseHandle(m_hFile);
#endif
#ifdef MTI_UNIX
close( m_hFile );
#endif
m_hFile = 0;
return MTI_SUCCESS;
}
long MTISerialIO::SetSerialParams (unsigned int baudRate)
{
if (m_hFile == 0)
return MTI_ERR_INVALID_HANDLE;
#ifdef MTI_WINDOWS
// Obtain the DCB structure for the device
DCB dcb = {0};
dcb.DCBlength=sizeof(dcb);
if (!::GetCommState(m_hFile,&dcb))
return MTI_ERR_SERIALCOMM;
dcb.BaudRate = DWORD(baudRate);
dcb.ByteSize = 8;
dcb.Parity = NOPARITY;
dcb.StopBits = ONESTOPBIT;
if (!SERIAL_HARDWARE_FLOW_CONTROL) {
// Set the new data
dcb.fOutxCtsFlow = false; // Disable CTS monitoring
dcb.fOutxDsrFlow = false; // Disable DSR monitoring
dcb.fDtrControl = DTR_CONTROL_DISABLE; // Disable DTR monitoring
dcb.fOutX = false; // Disable XON/XOFF for transmission
dcb.fInX = false; // Disable XON/XOFF for receiving
dcb.fRtsControl = RTS_CONTROL_DISABLE; // Disable RTS (Ready To Send)
dcb.fParity = false;
}
else
{
// set XON/XOFF
dcb.fOutX = FALSE;
dcb.fInX = FALSE;
// set RTSCTS
dcb.fOutxCtsFlow = TRUE;
dcb.fRtsControl = RTS_CONTROL_HANDSHAKE;
// set DSRDTR
dcb.fOutxDsrFlow = FALSE;
dcb.fDtrControl = DTR_CONTROL_DISABLE;
}
if (!::SetCommState(m_hFile,&dcb))
return MTI_ERR_SERIALCOMM;
#endif
#ifdef MTI_UNIX
//CONFIGURE THE UART
//The flags (defined in /usr/include/termios.h - see http://pubs.opengroup.org/onlinepubs/007908799/xsh/termios.h.html):
// Baud rate:- B1200, B2400, B4800, B9600, B19200, B38400, B57600, B115200, B230400, B460800, B500000, B576000, B921600, B1000000, B1152000, B1500000, B2000000, B2500000, B3000000, B3500000, B4000000
// CSIZE:- CS5, CS6, CS7, CS8
// CLOCAL - Ignore modem status lines
// CREAD - Enable receiver
// IGNPAR = Ignore characters with parity errors
// ICRNL - Map CR to NL on input (Use for ASCII comms where you want to auto correct end of line characters - don't use for bianry comms!)
// PARENB - Parity enable
// PARODD - Odd parity (else even)
struct termios options;
tcgetattr(m_hFile, &options);
int baud_flag;
switch (baudRate) {
case 9600: baud_flag = B9600; break;
case 19200: baud_flag = B19200; break;
case 38400: baud_flag = B38400; break;
case 57600: baud_flag = B57600; break;
case 115200: baud_flag = B115200; break;
case 230400: baud_flag = B230400; break;
case 460800: baud_flag = B460800; break;
case 500000: baud_flag = B500000; break;
case 576000: baud_flag = B576000; break;
case 921600: baud_flag = B921600; break;
default: baud_flag = B9600; break;
}
cfsetspeed(&options, baud_flag);
options.c_cflag &= ~CSTOPB;
options.c_cflag |= CLOCAL;
options.c_cflag |= CREAD;
cfmakeraw(&options);
// next may be good to try for timeout control
//options.c_cc[VMIN] = 1;
//options.c_cc[VTIME] = 10;
//
tcflush(m_hFile, TCIFLUSH);
tcsetattr(m_hFile, TCSANOW, &options);
#endif
return MTI_SUCCESS;
}
long MTISerialIO::Write (unsigned char* pData, size_t lData, unsigned int* lWritten, unsigned int timeout)
{
if (m_hFile == 0)
return MTI_ERR_INVALID_HANDLE;
#ifdef MTI_WINDOWS
DWORD dwWritten = 0;
if (!m_hevtOverlapped && timeout != INFINITE)
return MTI_ERR_SERIALCOMM;
// Wait for the event to happen
OVERLAPPED ovInternal;
LPOVERLAPPED lpOverlapped = 0;
if (m_hevtOverlapped)
{
memset(&ovInternal,0,sizeof(ovInternal));
ovInternal.hEvent = m_hevtOverlapped;
lpOverlapped = &ovInternal;
}
// Make sure the overlapped structure isn't busy
if(!(!m_hevtOverlapped || HasOverlappedIoCompleted(lpOverlapped)))
return MTI_ERR_SERIALCOMM;
// Write the data
if (!::WriteFile(m_hFile,(LPCVOID)pData,lData,&dwWritten,lpOverlapped))
{
long lLastError = ::GetLastError();
if (lLastError != ERROR_IO_PENDING)
return MTI_ERR_SERIALCOMM;
// We need to block if the client didn't specify an overlapped structure Wait for the overlapped operation to complete
switch (::WaitForSingleObject(lpOverlapped->hEvent,timeout))
{
case WAIT_OBJECT_0:
// The overlapped operation has completed
if (!::GetOverlappedResult(m_hFile,lpOverlapped,&dwWritten,FALSE))
return MTI_ERR_SERIALCOMM;
break;
case WAIT_TIMEOUT:
::CancelIo(m_hFile);
return MTI_ERR_SERIALCOMM_READ_TIMEOUT;
default:
return MTI_ERR_SERIALCOMM;
}
}
else
{
// The operation completed immediatly. Just to be sure we'll set the overlapped structure's event handle.
if (lpOverlapped)
::SetEvent(lpOverlapped->hEvent);
}
if( lWritten != 0 )
*lWritten = dwWritten;
#endif
#ifdef MTI_UNIX
write(m_hFile, pData, lData);
// For some channels like Bluetooth, the write immediately returns even though the output buffer
// may take some time to clear. We implement a partially blocking write by polling the output buffer
// to be sure it has been sent before continuing. A timeout is used in case the output write
// blocks indefinitely.
if (!timeout)
return MTI_SUCCESS;
int out_bytes = -1;
clock_t start = clock();
while (out_bytes != 0) {
double elapsed = (double)(clock()-start) / CLOCKS_PER_SEC * 1000; // milliseconds
if (timeout != INFINITE && elapsed > timeout)
return MTI_ERR_SERIALCOMM;
ioctl(m_hFile, TIOCOUTQ, &out_bytes);
}
#endif
return MTI_SUCCESS;
}
long MTISerialIO::SetBlockingMode (int blockingMode)
{
if (m_hFile == 0)
return MTI_ERR_INVALID_HANDLE;
#ifdef MTI_WINDOWS
COMMTIMEOUTS cto;
if (!::GetCommTimeouts(m_hFile,&cto))
return MTI_ERR_SERIALCOMM;
if(blockingMode == MTI_BLOCKING_MODE_ON)
{
cto.ReadIntervalTimeout = 0;
cto.ReadTotalTimeoutConstant = 0;
cto.ReadTotalTimeoutMultiplier = 0;
}
else if(blockingMode == MTI_BLOCKING_MODE_OFF)
{
cto.ReadIntervalTimeout = MAXDWORD;
cto.ReadTotalTimeoutConstant = 0;
cto.ReadTotalTimeoutMultiplier = 0;
}
else
return MTI_ERR_SERIALCOMM;
if (!::SetCommTimeouts(m_hFile,&cto))
return MTI_ERR_SERIALCOMM;
#endif
#ifdef MTI_UNIX
const int flags = fcntl(m_hFile, F_GETFL);
fcntl(m_hFile, F_SETFL, blockingMode == MTI_BLOCKING_MODE_ON ? flags & ~O_NONBLOCK : flags | O_NONBLOCK);
#endif
return MTI_SUCCESS;
}
long MTISerialIO::Read (unsigned char* pData, size_t lData, unsigned int* lRead, unsigned int timeout, int blockingMode)
{
if (m_hFile == 0)
return MTI_ERR_INVALID_HANDLE;
#ifdef MTI_WINDOWS
if( blockingMode != MTI_BLOCKING_MODE_ERR )
SetBlockingMode( blockingMode );
DWORD dwRead = 0;
#ifdef _DEBUG
// The debug version fills the entire data structure with 0xDC bytes, to catch buffer errors as soon as possible.
memset(pData,0xDC,lData);
#endif
// Check if an overlapped structure has been specified
if (!m_hevtOverlapped && timeout != INFINITE)
return MTI_ERR_SERIALCOMM;
// Wait for the event to happen
OVERLAPPED ovInternal;
memset(&ovInternal,0,sizeof(ovInternal));
ovInternal.hEvent = m_hevtOverlapped;
LPOVERLAPPED lpOverlapped = &ovInternal;
// Make sure the overlapped structure isn't busy
if(!(!m_hevtOverlapped || HasOverlappedIoCompleted(lpOverlapped)))
return MTI_ERR_SERIALCOMM;
// Read the data
if (!::ReadFile(m_hFile,pData,lData,&dwRead,lpOverlapped))
{
long lLastError = ::GetLastError();
if (lLastError != ERROR_IO_PENDING)
return MTI_ERR_SERIALCOMM;
// We need to block if the client didn't specify an overlapped structure
switch (::WaitForSingleObject(lpOverlapped->hEvent,timeout))
{
case WAIT_OBJECT_0:
// The overlapped operation has completed
if (!::GetOverlappedResult(m_hFile,lpOverlapped,&dwRead,FALSE))
return MTI_ERR_SERIALCOMM;
break;
case WAIT_TIMEOUT:
::CancelIo(m_hFile);
return MTI_ERR_SERIALCOMM_READ_TIMEOUT;
default:
return MTI_ERR_SERIALCOMM;
}
}
else
{
// The operation completed immediatly. Just to be sure we'll set the overlapped structure's event handle.
if (lpOverlapped)
::SetEvent(lpOverlapped->hEvent);
}
if( lRead != 0 )
*lRead = dwRead;
#endif
#ifdef MTI_UNIX
if( blockingMode != MTI_BLOCKING_MODE_ERR )
SetBlockingMode( blockingMode );
// In each pass, the read() command returns the number of bytes read, which can be less than
// the number of bytes requested. Here we keep reading until the number of bytes read
// matches the number of bytes requested in lData.
unsigned int rtot = 0;
do
{
// If a timeout is specified, poll the file descriptor to make sure input is ready.
// poll() returns 0 if timeout occurs, -1 if an error occurs.
if (timeout != INFINITE)
{
struct pollfd fds[1];
fds[0].fd = m_hFile;
fds[0].events = POLLIN;
int perr = poll(fds, 1, timeout);
if (perr == 0) // timeout
return MTI_ERR_SERIALCOMM_READ_TIMEOUT;
if (perr == -1 || !(fds[0].revents & POLLIN)) // other error or data not ready
return MTI_ERR_SERIALCOMM;
}
rtot += read(m_hFile, pData + rtot, lData - rtot);
} while (rtot < lData);
if( lRead != 0 )
*lRead = rtot;
#endif
return MTI_SUCCESS;
}
long MTISerialIO::Purge()
{
if (m_hFile == 0)
return MTI_ERR_INVALID_HANDLE;
#ifdef MTI_WINDOWS
if (!::PurgeComm(m_hFile, PURGE_TXCLEAR | PURGE_RXCLEAR))
return MTI_ERR_SERIALCOMM;
#endif
#ifdef MTI_UNIX
tcflush(m_hFile, TCIOFLUSH);
#endif
return MTI_SUCCESS;
}
long MTISerialIO::ReadText (char* text, unsigned char delineationCharacter, unsigned int timeout)
{
bool wait = true;
long lastError;
unsigned int textLength=0;
unsigned char rdata[1];
SetBlockingMode( true );
while(wait)
{
lastError = Read( rdata, 1, 0, timeout, MTI_BLOCKING_MODE_ERR );
if (lastError != 0)
{
wait = false;
return lastError;
}
if (rdata[0] == delineationCharacter)
wait = false;
else
{
text[textLength++] = (char)rdata[0];
text[textLength] = 0;
}
if (textLength >= 80)
wait = false;
}
return lastError;
}