[go: up one dir, main page]

Menu

[r888]: / trunk / src / common / com.cpp  Maximize  Restore  History

Download this file

639 lines (536 with data), 12.6 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
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
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
/*!
* \file com.cpp
* \brief implementation file for the Linux comm class.
* \author Ake Hedman, CC Systems AB,
* \author akhe@users.sourceforge.net
* \date 2002-2003
*
*
// Copyright (C) 2000-2008 Ake Hedman, eurosource, <akhe@eurosource.se>
//
// This software is placed into
// the public domain and may be used for any purpose. However, this
// notice must not be changed or removed and no warranty is either
// expressed or implied by its publication or distribution.
*
* HISTORY
* =======
* 020629
* Handling error returs from readChar in drainInput.
*
*/
#define _POSIX
#include <stdio.h>
#include <unistd.h>
#include <pthread.h>
#include <time.h>
#include "com.h"
///////////////////////////////////////////////////////////////////////////////
// Constructor
Comm::Comm( void )
{
pthread_mutex_init( &m_mutex, NULL );
m_fd = 0;
}
///////////////////////////////////////////////////////////////////////////////
// Destructor
Comm::~Comm( void )
{
close();
pthread_mutex_destroy( &m_mutex );
}
///////////////////////////////////////////////////////////////////////////////
// open
bool Comm::open( char *szDevice )
{
// Not allowed to open if already open
if ( m_fd != 0 ) return false;
if ( NULL != szDevice ) {
if ( -1 == ( m_fd = ::open( szDevice, O_RDWR | O_NONBLOCK ) ) ) {
return false;
}
}
else {
if ( -1 == ( m_fd = ::open( m_szDevice, O_RDWR | O_NONBLOCK ) ) ) {
return false;
}
}
flock( m_fd, LOCK_EX );
return true;
}
///////////////////////////////////////////////////////////////////////////////
// comm_gets
//
// Get string (max is maximum chars to read) from COM port.
//
int Comm::comm_gets( char *Buffer, int max )
{
unsigned char c;
int x=0;
Buffer = NULL;
do {
read( m_fd, &c, 1 );
Buffer[ x++ ] = c;
} while( isCharReady() && ( x<max ) );
Buffer[x-1]=0;
return x;
}
///////////////////////////////////////////////////////////////////////////////
// comm_gets
//
// Get a string of characters with timeout
//
int Comm::comm_gets( char *Buffer, int nChars, long timeout )
{
fd_set filedescr;
struct timeval tval;
int cnt;
int us_to = 0;
int s_to = 0;
if ( timeout > 1000000 ) {
s_to = ( timeout/1000000 );
if ( s_to > 60 ) s_to = 60; // max one minute
us_to = timeout - ( long )s_to*1000000L;
}
else {
us_to = timeout;
}
// loop to wait until each byte is available and read it
for ( cnt = 0; cnt < nChars; cnt++ ) {
// set a descriptor to wait for a character available
FD_ZERO( &filedescr );
FD_SET( m_fd, &filedescr );
// set timeout
tval.tv_sec = s_to;
tval.tv_usec = us_to;
// if byte available read or return bytes read
if ( select( m_fd+1, &filedescr, NULL, NULL, &tval ) != 0 ) {
if ( read( m_fd, &Buffer[cnt], 1 ) != 1 ) {
return cnt;
}
}
else {
return cnt;
}
}
// success, so return desired length
return nChars;
}
///////////////////////////////////////////////////////////////////////////////
// comm_puts
//
// Put characters on COM port.
//
int Comm::comm_puts(char *Buffer, bool bDrain )
{
int rv = write( m_fd, Buffer, strlen( Buffer ) );
if ( bDrain ) {
Drain();
}
return rv;
}
///////////////////////////////////////////////////////////////////////////////
// comm_puts
//
// Send an array of characters
int Comm::comm_puts( char *Buffer, int len, bool bDrain )
{
int rv = write( m_fd, Buffer, len );
if ( bDrain ) {
Drain();
}
return rv;
}
///////////////////////////////////////////////////////////////////////////////
// comm_getc
//
// Get Character from COM port.
//
unsigned char Comm::comm_getc(void)
{
unsigned char c = 0;
read( m_fd, &c, 1 );
return c;
}
///////////////////////////////////////////////////////////////////////////////
// readChar
//
// Get Character from COM port.
//
char Comm::readChar( int *cnt )
{
unsigned char c = 0;
*cnt = read( m_fd, &c, 1 );
return c;
}
///////////////////////////////////////////////////////////////////////////////
// comm_putc
//
// Put Character on COM port.
//
void Comm::comm_putc(unsigned char c, bool bDrain )
{
write( m_fd, &c, 1 );
//printf("Char=%2X\n", c );
if ( bDrain ) {
Drain();
}
}
///////////////////////////////////////////////////////////////////////////////
// setHWFlow
//
// Set hardware flow control.
//
void Comm::setHWFlow( int on )
{
struct termios tty;
tcgetattr(m_fd, &tty);
if (on) {
tty.c_cflag |= CRTSCTS;
}
else {
tty.c_cflag &= ~CRTSCTS;
}
tcsetattr(m_fd, TCSANOW, &tty);
}
///////////////////////////////////////////////////////////////////////////////
// ToggleDTR
//
// Toggle DTR.
//
void Comm::ToggleDTR( int delay )
{
int f;
f = TIOCM_DTR;
ioctl( m_fd, TIOCMBIC, &f );
if ( 0 < delay ) {
sleep( delay );
}
ioctl( m_fd, TIOCMBIS, &f );
}
///////////////////////////////////////////////////////////////////////////////
// SendBreak
//
// Send break.
//
void Comm::SendBreak( void )
{
tcsendbreak(m_fd, 0);
}
///////////////////////////////////////////////////////////////////////////////
// getDCD
//
// Get carrier status.
//
int Comm::getDCD( void )
{
int mcs;
ioctl(m_fd, TIOCMGET, &mcs);
return(mcs & TIOCM_CAR ? 1 : 0);
}
///////////////////////////////////////////////////////////////////////////////
// Flush
//
// Flush output que.
//
void Comm::Flush( void )
{
ioctl( m_fd, TCFLSH, 2);
}
///////////////////////////////////////////////////////////////////////////////
// Drain
//
// Drain output que.
//
void Comm::Drain( void )
{
tcdrain( m_fd );
}
///////////////////////////////////////////////////////////////////////////////
// FlushInQue
//
void Comm::FlushInQue( void )
{
while( isCharReady() ) {
comm_getc();
}
}
///////////////////////////////////////////////////////////////////////////////
// isCharReady
//
// Check if there are available characters.
//
int Comm::isCharReady( void )
{
long i = -1;
(void) ioctl( m_fd, FIONREAD, &i );
return( (int)i );
}
///////////////////////////////////////////////////////////////////////////////
// getMaxBaud
//
// Get maximum baudrate.
//
int Comm::getMaxBaud( void )
{
#ifdef B115200
return(1152);
#elif defined(B57600)
return(576);
#elif defined(B38400)
return(384);
#elif defined(EXTB)
return(384);
#elif defined(B19200)
return(192);
#elif defined(EXTA)
return(192);
#else
return(96);
#endif
}
///////////////////////////////////////////////////////////////////////////////
// DtrOn
//
// Turn on DTR.
//
void Comm::DtrOn()
{
int f;
f = TIOCM_DTR;
ioctl( m_fd, TIOCMBIS, &f );
}
///////////////////////////////////////////////////////////////////////////////
// DtrOff
//
// Turn off DTR.
//
void Comm::DtrOff()
{
int f;
f = TIOCM_DTR;
ioctl( m_fd, TIOCMBIC, &f );
}
///////////////////////////////////////////////////////////////////////////////
// RtsOn
//
// Turn on RTS.
//
void Comm::RtsOn()
{
int f;
f = TIOCM_RTS;
ioctl( m_fd, TIOCMBIS, &f );
}
///////////////////////////////////////////////////////////////////////////////
// RtsOff
//
// Turn off RTS.
//
void Comm::RtsOff()
{
int f;
f = TIOCM_RTS;
ioctl( m_fd, TIOCMBIC, &f );
}
///////////////////////////////////////////////////////////////////////////////
// isTransmitterEmpty
//
// get_lsr_info - get line status register info
//
// Let user call ioctl() to get info when the UART physically
// is emptied. On bus types like RS485, the transmitter must
// release the bus after transmitting. This must be done when
// the transmit shift register is empty, not be done when the
// transmit holding register is empty. This functionality
// allows an RS485 driver to be written in user space.
// TIOCSER_TEMT
//
// These are the definitions for the Line Status Register
//
//#define UART_LSR_TEMT 0x40 Transmitter empty
//#define UART_LSR_THRE 0x20 Transmit-hold-register empty
int Comm::isTransmitterEmpty()
{
int rv;
ioctl( m_fd, TIOCSERGETLSR, &rv );
return rv;
}
///////////////////////////////////////////////////////////////////////////////
// setParam
//
// Set port parameters.
//
void Comm::setParam( char *baud,
char *parity,
char *bits,
int HWFlow,
int SWFlow )
{
int spd = -1;
int newbaud;
int bit = bits[0];
struct termios tty;
tcgetattr(m_fd, &tty);
// We generate mark and space parity ourself.
if ( bit == '7' && ( parity[0] == 'M' || parity[0] == 'S' ) ) {
bit = '8';
}
// Check if 'baudr' is really a number
if ( ( newbaud = ( atol(baud) / 100)) == 0 && baud[0] != '0') {
newbaud = -1;
}
switch( newbaud ) {
case 0:
#ifdef B0
spd = B0;
break;
#else
spd = 0;
break;
#endif
case 3:
spd = B300;
break;
case 6:
spd = B600;
break;
case 12:
spd = B1200;
break;
case 24:
spd = B2400;
break;
case 48:
spd = B4800;
break;
case 96:
spd = B9600;
break;
#ifdef B19200
case 192:
spd = B19200;
break;
#else /* B19200 */
# ifdef EXTA
case 192:
spd = EXTA;
break;
# else /* EXTA */
case 192:
spd = B9600;
break;
# endif /* EXTA */
#endif /* B19200 */
#ifdef B38400
case 384:
spd = B38400;
break;
#else /* B38400 */
# ifdef EXTB
case 384:
spd = EXTB;
break;
# else /* EXTB */
case 384:
spd = B9600;
break;
# endif /* EXTB */
#endif /* B38400 */
#ifdef B57600
case 576:
spd = B57600;
break;
#endif
#ifdef B115200
case 1152:
spd = B115200;
break;
#endif
} // Switch
if (spd != -1) {
cfsetospeed( &tty, (speed_t)spd );
cfsetispeed( &tty, (speed_t)spd );
}
switch (bit) {
case '5':
tty.c_cflag = (tty.c_cflag & ~CSIZE) | CS5;
break;
case '6':
tty.c_cflag = (tty.c_cflag & ~CSIZE) | CS6;
break;
case '7':
tty.c_cflag = (tty.c_cflag & ~CSIZE) | CS7;
break;
case '8':
default:
tty.c_cflag = (tty.c_cflag & ~CSIZE) | CS8;
break;
}
// Set into raw, no echo mode
tty.c_iflag = IGNBRK;
tty.c_lflag = 0;
tty.c_oflag = 0;
tty.c_cflag |= CLOCAL | CREAD;
#ifdef _DCDFLOW
tty.c_cflag &= ~CRTSCTS;
#endif
tty.c_cc[VMIN] = 1;
tty.c_cc[VTIME] = 5;
if ( SWFlow ) {
tty.c_iflag |= IXON | IXOFF;
}
else {
tty.c_iflag &= ~(IXON|IXOFF|IXANY);
}
tty.c_cflag &= ~(PARENB | PARODD);
if (parity[0] == 'E') {
tty.c_cflag |= PARENB;
}
else if (parity[0] == 'O') {
tty.c_cflag |= PARODD;
}
tcsetattr( m_fd, TCSANOW, &tty );
RtsOn();
#ifndef _DCDFLOW
setHWFlow( HWFlow );
#endif
}
///////////////////////////////////////////////////////////////////////////////
// msGettick
//--------------------------------------------------------------------------
// Get the current millisecond tick count. Does not have to represent
// an actual time, it just needs to be an incrementing timer.
//
long Comm::msGettick(void)
{
struct timezone tmzone;
struct timeval tmval;
long ms;
gettimeofday(&tmval,&tmzone);
ms = (tmval.tv_sec & 0xFFFF) * 1000 + tmval.tv_usec / 1000;
return ms;
}
///////////////////////////////////////////////////////////////////////////////
// msDelay
//--------------------------------------------------------------------------
// Description:
// Delay for at least 'len' ms
//
void Comm::msDelay(int len)
{
struct timespec s; // Set aside memory space on the stack
s.tv_sec = len / 1000;
s.tv_nsec = (len - (s.tv_sec * 1000)) * 1000000;
nanosleep(&s, NULL);
}
///////////////////////////////////////////////////////////////////////////////
// drainInput
void Comm::drainInput( void )
{
int cnt;
char c;
do {
c = readChar( &cnt );
} while (cnt && ( -1 != cnt) );
}