~mightyiam/rain8net/trunk

« back to all changes in this revision

Viewing changes to serial.cpp

  • Committer: torford
  • Date: 2008-03-05 19:05:50 UTC
  • Revision ID: vcs-imports@canonical.com-20080305190550-m8zeogudavy7mdkl
Add copyright and license info.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#include <cstdlib>
2
 
#include <climits>
3
 
#include <termios.h>
4
 
#include <cerrno>
5
 
#include <memory.h>
6
 
#include <sys/types.h>
7
 
#include <sys/stat.h>
8
 
#include <fcntl.h>
9
 
#include <sys/ioctl.h>
10
 
#include <poll.h>
11
 
#include <time.h>
12
 
#include <unistd.h>
13
 
 
14
 
#include "serial.h"
15
 
 
16
 
#ifndef MAX_INPUT
17
 
#define MAX_INPUT 255
18
 
#endif
19
 
 
20
 
#ifndef MAX_CANON
21
 
#define MAX_CANON MAX_INPUT
22
 
#endif
23
 
 
24
 
 
25
 
#ifndef CRTSCTS
26
 
#ifdef  CNEW_RTSCTS
27
 
#define CRTSCTS (CNEW_RTSCTS)
28
 
#endif
29
 
#endif
30
 
 
31
 
#if defined(CTSXON) && defined(RTSXOFF) && !defined(CRTSCTS)
32
 
#define CRTSCTS (CTSXON | RTSXOFF)
33
 
#endif
34
 
 
35
 
#ifndef CRTSCTS
36
 
#define CRTSCTS 0
37
 
#endif
38
 
 
39
 
int Serial::devInit(const char *fname)
40
 
{
41
 
        comDevice = -1;
42
 
        current = new struct termios;
43
 
        original = new struct termios;
44
 
 
45
 
        devOpen(fname);
46
 
 
47
 
        if (-1 == comDevice)
48
 
                return -1;
49
 
 
50
 
        if(!isatty(comDevice)) 
51
 
        {
52
 
                devClose();
53
 
                return -2;
54
 
        }
55
 
        return 0;
56
 
}
57
 
 
58
 
Serial::~Serial()
59
 
{
60
 
        endSerial();
61
 
}
62
 
 
63
 
void Serial::initConfig(void)
64
 
{
65
 
 
66
 
        fcntl(comDevice, F_SETFL, FNDELAY);
67
 
        
68
 
        tcgetattr(comDevice, (struct termios *)original);
69
 
 
70
 
        bzero(current, sizeof(struct termios));
71
 
        
72
 
        ((struct termios *)current)->c_cc[VMIN] = 0;
73
 
        ((struct termios *)current)->c_cc[VTIME] = 0;
74
 
        
75
 
        ((struct termios *)current)->c_oflag &= ~OPOST;
76
 
        ((struct termios *)current)->c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
77
 
        ((struct termios *)current)->c_cflag &= ~(PARENB | CSTOPB | CSIZE);
78
 
        ((struct termios *)current)->c_cflag |= (CRTSCTS|CREAD|CLOCAL|CS8);
79
 
        ((struct termios *)current)->c_iflag |= (INPCK | ISTRIP);
80
 
        
81
 
        cfsetispeed(((struct termios *)current), B4800);
82
 
        cfsetospeed(((struct termios *)current), B4800);
83
 
        
84
 
        tcsetattr(comDevice, TCSANOW, (struct termios *)current);
85
 
        
86
 
}
87
 
 
88
 
void Serial::restore(void)
89
 
{
90
 
        memcpy(current, original, sizeof(struct termios));
91
 
        tcsetattr(comDevice, TCSANOW, (struct termios *)current);
92
 
}
93
 
 
94
 
void Serial::endSerial(void)
95
 
{
96
 
        if(comDevice < 0 && original)
97
 
                tcsetattr(comDevice, TCSANOW, (struct termios *)original);
98
 
 
99
 
        if(current)
100
 
                delete (struct termios *)current;
101
 
 
102
 
        if(original)
103
 
                delete (struct termios *)original;
104
 
 
105
 
        devClose();
106
 
 
107
 
        current = NULL;
108
 
        original = NULL;
109
 
 
110
 
}
111
 
 
112
 
void Serial::flushInput(void)
113
 
{
114
 
        tcflush(comDevice, TCIFLUSH);
115
 
}
116
 
 
117
 
void Serial::flushOutput(void)
118
 
{
119
 
        tcflush(comDevice, TCOFLUSH);
120
 
}
121
 
 
122
 
void Serial::waitOutput(void)
123
 
{
124
 
        tcdrain(comDevice);
125
 
}
126
 
 
127
 
int Serial::devOpen(const char * fname)
128
 
{
129
 
        int cflags = O_RDWR | O_NDELAY | O_NOCTTY;
130
 
        comDevice = open(fname, cflags);
131
 
        if (-1 == comDevice)
132
 
                return -1;
133
 
 
134
 
        initConfig();
135
 
        return 0;
136
 
}
137
 
 
138
 
int Serial::devRead(char *Data, const int Length)
139
 
{
140
 
        return read(comDevice, Data, Length);
141
 
}
142
 
 
143
 
int Serial::devWrite(const char *Data, const int Length)
144
 
{
145
 
        return write(comDevice, Data, Length);
146
 
}
147
 
 
148
 
void Serial::devClose()
149
 
{
150
 
        close(comDevice);
151
 
        comDevice = -1;
152
 
}
153
 
 
154
 
 
155
 
int Serial::setSpeed(unsigned long speed)
156
 
{
157
 
        unsigned long rate = B4800;
158
 
 
159
 
        switch(speed) 
160
 
        {
161
 
#ifdef B115200
162
 
                case 115200:
163
 
                        rate = B115200;
164
 
                        break;
165
 
#endif
166
 
#ifdef B57600
167
 
                case 57600:
168
 
                        rate = B57600;
169
 
                        break;
170
 
#endif
171
 
#ifdef B38400
172
 
                case 38400:
173
 
                        rate = B38400;
174
 
                        break;
175
 
#endif
176
 
                case 19200:
177
 
                        rate = B19200;
178
 
                        break;
179
 
                case 9600:
180
 
                        rate = B9600;
181
 
                        break;
182
 
                case 4800:
183
 
                        rate = B4800;
184
 
                        break;
185
 
                case 2400:
186
 
                        rate = B2400;
187
 
                        break;
188
 
                case 1200:
189
 
                        rate = B1200;
190
 
                        break;
191
 
                case 600:
192
 
                        rate = B600;
193
 
                        break;
194
 
                case 300:
195
 
                        rate = B300;
196
 
                        break;
197
 
                case 110:
198
 
                        rate = B110;
199
 
                        break;
200
 
#ifdef  B0
201
 
                case 0:
202
 
                        rate = B0;
203
 
                        break;
204
 
#endif
205
 
        }
206
 
 
207
 
        struct termios *attr = (struct termios *)current;
208
 
        cfsetispeed(attr, rate);
209
 
        cfsetospeed(attr, rate);
210
 
        if (0 != tcsetattr(comDevice, TCSANOW, attr))
211
 
                return errno;
212
 
 
213
 
        return 0;
214
 
}
215
 
 
216
 
unsigned long Serial::getSpeed(void)
217
 
{
218
 
        unsigned long speed = 0;
219
 
        unsigned long rate = B2400;
220
 
        struct termios *attr = (struct termios *)current;
221
 
        tcgetattr(comDevice, attr);
222
 
        rate = cfgetispeed(attr);
223
 
        
224
 
        switch(rate) 
225
 
        {
226
 
#ifdef B115200
227
 
                case B115200:
228
 
                        speed = 115200;
229
 
                        break;
230
 
#endif
231
 
#ifdef B57600
232
 
                case B57600:
233
 
                        speed = 57600;
234
 
                        break;
235
 
#endif
236
 
#ifdef B38400
237
 
                case B38400:
238
 
                        speed = 38400;
239
 
                        break;
240
 
#endif
241
 
                case B19200:
242
 
                        speed = 19200;
243
 
                        break;
244
 
                case B9600:
245
 
                        speed = 9600;
246
 
                        break;
247
 
                case B4800:
248
 
                        speed = 4800;
249
 
                        break;
250
 
                case B2400:
251
 
                        speed = 2400;
252
 
                        break;
253
 
                case B1200:
254
 
                        speed = 1200;
255
 
                        break;
256
 
                case B600:
257
 
                        speed = 600;
258
 
                        break;
259
 
                case B300:
260
 
                        speed = 300;
261
 
                        break;
262
 
                case B110:
263
 
                        speed = 110;
264
 
                        break;
265
 
#ifdef  B0
266
 
                case B0:
267
 
                        speed = 0;
268
 
                        break;
269
 
#endif
270
 
        }
271
 
 
272
 
        return speed;
273
 
}
274
 
 
275
 
int Serial::setFlowControl(Flow flow)
276
 
{
277
 
 
278
 
        struct termios *attr = (struct termios *)current;
279
 
 
280
 
        attr->c_cflag &= ~CRTSCTS;
281
 
        attr->c_iflag &= ~(IXON | IXANY | IXOFF);
282
 
 
283
 
        switch(flow) {
284
 
        case flowSoft:
285
 
                attr->c_iflag |= (IXON | IXANY | IXOFF);
286
 
                break;
287
 
        case flowBoth:
288
 
                attr->c_iflag |= (IXON | IXANY | IXOFF);
289
 
        case flowHard:
290
 
                attr->c_cflag |= CRTSCTS;
291
 
                break;
292
 
        case flowNone:
293
 
        default:
294
 
                break;
295
 
        }
296
 
 
297
 
        if (0 != tcsetattr(comDevice, TCSANOW, attr))
298
 
                return errno;
299
 
 
300
 
        return 0;
301
 
}
302
 
 
303
 
int Serial::setStopBits(int bits)
304
 
{
305
 
        struct termios *attr = (struct termios *)current;
306
 
        attr->c_cflag &= ~CSTOPB;
307
 
 
308
 
        switch(bits) 
309
 
        {
310
 
        case 2:
311
 
                attr->c_cflag |= CSTOPB;
312
 
                break;
313
 
        }
314
 
 
315
 
        if (0 != tcsetattr(comDevice, TCSANOW, attr))
316
 
                return errno;
317
 
 
318
 
        return 0;
319
 
}
320
 
 
321
 
int Serial::setCharBits(int bits)
322
 
{
323
 
        struct termios *attr = (struct termios *)current;
324
 
        attr->c_cflag &= ~CSIZE;
325
 
 
326
 
        switch(bits) 
327
 
        {
328
 
        case 5:
329
 
                attr->c_cflag |= CS5;
330
 
                break;
331
 
        case 6:
332
 
                attr->c_cflag |= CS6;
333
 
                break;
334
 
        case 7:
335
 
                attr->c_cflag |= CS7;
336
 
                break;
337
 
        case 8:
338
 
        default:
339
 
                attr->c_cflag |= CS8;
340
 
                break;
341
 
        }
342
 
 
343
 
        if (0 != tcsetattr(comDevice, TCSANOW, attr))
344
 
                return errno;
345
 
 
346
 
        return 0;
347
 
}
348
 
 
349
 
int Serial::setParity(Parity parity)
350
 
{
351
 
        struct termios *attr = (struct termios *)current;
352
 
        attr->c_cflag &= ~(PARENB | PARODD);
353
 
 
354
 
        switch(parity) 
355
 
        {
356
 
        case parityEven:
357
 
                attr->c_cflag |= PARENB;
358
 
                break;
359
 
        case parityOdd:
360
 
                attr->c_cflag |= (PARENB | PARODD);
361
 
                break;
362
 
        case parityNone:
363
 
        default:
364
 
                break;
365
 
        }
366
 
 
367
 
        if (0 != tcsetattr(comDevice, TCSANOW, attr))
368
 
                return errno;
369
 
 
370
 
        return 0;
371
 
}
372
 
 
373
 
void Serial::sendBreak(void)
374
 
{
375
 
        tcsendbreak(comDevice, 0);
376
 
}
377
 
 
378
 
int Serial::bytesAvailable(int & count)
379
 
{
380
 
        int bytes = 0;
381
 
        int rc = ioctl(comDevice, FIONREAD, &bytes);
382
 
        if (-1 == rc)
383
 
        {
384
 
                count = 0;
385
 
                return errno;
386
 
        }
387
 
        count = bytes;
388
 
        return 0;
389
 
}
390
 
 
391
 
int Serial::raiseDTR(void)
392
 
{
393
 
        int status;
394
 
 
395
 
        int rc = ioctl(comDevice, TIOCMGET, &status);
396
 
        if (-1 == rc)
397
 
                return errno;
398
 
 
399
 
        status &= ~TIOCM_DTR;
400
 
 
401
 
        rc = ioctl(comDevice, TIOCMGET, &status);
402
 
        if (-1 == rc)
403
 
                return errno;
404
 
 
405
 
        return 0;
406
 
}
407
 
 
408
 
int Serial::lowerDTR(void)
409
 
{
410
 
        int status;
411
 
 
412
 
        int rc = ioctl(comDevice, TIOCMGET, &status);
413
 
        if (-1 == rc)
414
 
                return errno;
415
 
 
416
 
        status |= TIOCM_DTR;
417
 
 
418
 
        rc = ioctl(comDevice, TIOCMGET, &status);
419
 
        if (-1 == rc)
420
 
                return errno;
421
 
 
422
 
        return 0;
423
 
}
424
 
 
425
 
int Serial::toggleDTR(timeout_t millisec)
426
 
{
427
 
        int rc = lowerDTR();
428
 
        if (rc)
429
 
                return rc;
430
 
 
431
 
        if(millisec) 
432
 
        {
433
 
                wait(millisec);
434
 
                rc = lowerDTR();
435
 
                if (rc)
436
 
                        return rc;
437
 
        }
438
 
        return 0;
439
 
}
440
 
 
441
 
void Serial::wait(timeout_t ms)
442
 
{
443
 
        struct timespec t;
444
 
        t.tv_sec = ms / 1000;
445
 
        t.tv_nsec = ((ms % 1000) * 1000000l);
446
 
        nanosleep(&t, 0);
447
 
}
448
 
 
449
 
bool Serial::isPending(Pending pending, timeout_t timeout)
450
 
{
451
 
 
452
 
        int status;
453
 
        struct pollfd pfd;
454
 
 
455
 
        pfd.fd = comDevice;
456
 
        pfd.revents = 0;
457
 
        switch(pending) 
458
 
        {
459
 
        case pendingInput:
460
 
                pfd.events = POLLIN;
461
 
                break;
462
 
        case pendingOutput:
463
 
                pfd.events = POLLOUT;
464
 
                break;
465
 
        case pendingError:
466
 
                pfd.events = POLLERR | POLLHUP;
467
 
                break;
468
 
        }
469
 
 
470
 
        status = 0;
471
 
        while(status < 1) 
472
 
        {
473
 
                if(timeout == TIMEOUT_INFINITE)
474
 
                        status = poll(&pfd, 1, -1);
475
 
                else
476
 
                        status = poll(&pfd, 1, timeout);
477
 
 
478
 
                if(status < 1) 
479
 
                {
480
 
                        if(status == -1 && errno == EINTR)
481
 
                                continue;
482
 
                        return false;
483
 
                }
484
 
        }
485
 
 
486
 
        if(pfd.revents & pfd.events)
487
 
                return true;
488
 
 
489
 
        return false;
490
 
}
491
 
 
 
1
/*
 
2
 * Written by and Copyright (C) 2008 the SourceForge
 
3
 *      Rain8Net team. http://rain8net.sourceforge.net/
 
4
 *
 
5
 * This file is part of Rain8Net.
 
6
 * 
 
7
 * Rain8Net is free software: you can redistribute it and/or modify
 
8
 * it under the terms of the GNU General Public License as published by
 
9
 * the Free Software Foundation, either version 3 of the License, or
 
10
 * (at your option) any later version.
 
11
 * 
 
12
 * Rain8Net is distributed in the hope that it will be useful,
 
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
15
 * GNU General Public License for more details.
 
16
 * 
 
17
 * You should have received a copy of the GNU General Public License
 
18
 * along with Rain8Net.  If not, see <http://www.gnu.org/licenses/>.
 
19
 *
 
20
 */
 
21
#include <cstdlib>
 
22
#include <climits>
 
23
#include <termios.h>
 
24
#include <cerrno>
 
25
#include <memory.h>
 
26
#include <sys/types.h>
 
27
#include <sys/stat.h>
 
28
#include <fcntl.h>
 
29
#include <sys/ioctl.h>
 
30
#include <poll.h>
 
31
#include <time.h>
 
32
#include <unistd.h>
 
33
 
 
34
#include "serial.h"
 
35
 
 
36
#ifndef MAX_INPUT
 
37
#define MAX_INPUT 255
 
38
#endif
 
39
 
 
40
#ifndef MAX_CANON
 
41
#define MAX_CANON MAX_INPUT
 
42
#endif
 
43
 
 
44
 
 
45
#ifndef CRTSCTS
 
46
#ifdef  CNEW_RTSCTS
 
47
#define CRTSCTS (CNEW_RTSCTS)
 
48
#endif
 
49
#endif
 
50
 
 
51
#if defined(CTSXON) && defined(RTSXOFF) && !defined(CRTSCTS)
 
52
#define CRTSCTS (CTSXON | RTSXOFF)
 
53
#endif
 
54
 
 
55
#ifndef CRTSCTS
 
56
#define CRTSCTS 0
 
57
#endif
 
58
 
 
59
int Serial::devInit(const char *fname)
 
60
{
 
61
        comDevice = -1;
 
62
        current = new struct termios;
 
63
        original = new struct termios;
 
64
 
 
65
        devOpen(fname);
 
66
 
 
67
        if (-1 == comDevice)
 
68
                return -1;
 
69
 
 
70
        if(!isatty(comDevice)) 
 
71
        {
 
72
                devClose();
 
73
                return -2;
 
74
        }
 
75
        return 0;
 
76
}
 
77
 
 
78
Serial::~Serial()
 
79
{
 
80
        endSerial();
 
81
}
 
82
 
 
83
void Serial::initConfig(void)
 
84
{
 
85
 
 
86
        fcntl(comDevice, F_SETFL, FNDELAY);
 
87
        
 
88
        tcgetattr(comDevice, (struct termios *)original);
 
89
 
 
90
        bzero(current, sizeof(struct termios));
 
91
        
 
92
        ((struct termios *)current)->c_cc[VMIN] = 0;
 
93
        ((struct termios *)current)->c_cc[VTIME] = 0;
 
94
        
 
95
        ((struct termios *)current)->c_oflag &= ~OPOST;
 
96
        ((struct termios *)current)->c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
 
97
        ((struct termios *)current)->c_cflag &= ~(PARENB | CSTOPB | CSIZE);
 
98
        ((struct termios *)current)->c_cflag |= (CRTSCTS|CREAD|CLOCAL|CS8);
 
99
        ((struct termios *)current)->c_iflag |= (INPCK | ISTRIP);
 
100
        
 
101
        cfsetispeed(((struct termios *)current), B4800);
 
102
        cfsetospeed(((struct termios *)current), B4800);
 
103
        
 
104
        tcsetattr(comDevice, TCSANOW, (struct termios *)current);
 
105
        
 
106
}
 
107
 
 
108
void Serial::restore(void)
 
109
{
 
110
        memcpy(current, original, sizeof(struct termios));
 
111
        tcsetattr(comDevice, TCSANOW, (struct termios *)current);
 
112
}
 
113
 
 
114
void Serial::endSerial(void)
 
115
{
 
116
        if(comDevice < 0 && original)
 
117
                tcsetattr(comDevice, TCSANOW, (struct termios *)original);
 
118
 
 
119
        if(current)
 
120
                delete (struct termios *)current;
 
121
 
 
122
        if(original)
 
123
                delete (struct termios *)original;
 
124
 
 
125
        devClose();
 
126
 
 
127
        current = NULL;
 
128
        original = NULL;
 
129
 
 
130
}
 
131
 
 
132
void Serial::flushInput(void)
 
133
{
 
134
        tcflush(comDevice, TCIFLUSH);
 
135
}
 
136
 
 
137
void Serial::flushOutput(void)
 
138
{
 
139
        tcflush(comDevice, TCOFLUSH);
 
140
}
 
141
 
 
142
void Serial::waitOutput(void)
 
143
{
 
144
        tcdrain(comDevice);
 
145
}
 
146
 
 
147
int Serial::devOpen(const char * fname)
 
148
{
 
149
        int cflags = O_RDWR | O_NDELAY | O_NOCTTY;
 
150
        comDevice = open(fname, cflags);
 
151
        if (-1 == comDevice)
 
152
                return -1;
 
153
 
 
154
        initConfig();
 
155
        return 0;
 
156
}
 
157
 
 
158
int Serial::devRead(char *Data, const int Length)
 
159
{
 
160
        return read(comDevice, Data, Length);
 
161
}
 
162
 
 
163
int Serial::devWrite(const char *Data, const int Length)
 
164
{
 
165
        return write(comDevice, Data, Length);
 
166
}
 
167
 
 
168
void Serial::devClose()
 
169
{
 
170
        close(comDevice);
 
171
        comDevice = -1;
 
172
}
 
173
 
 
174
 
 
175
int Serial::setSpeed(unsigned long speed)
 
176
{
 
177
        unsigned long rate = B4800;
 
178
 
 
179
        switch(speed) 
 
180
        {
 
181
#ifdef B115200
 
182
                case 115200:
 
183
                        rate = B115200;
 
184
                        break;
 
185
#endif
 
186
#ifdef B57600
 
187
                case 57600:
 
188
                        rate = B57600;
 
189
                        break;
 
190
#endif
 
191
#ifdef B38400
 
192
                case 38400:
 
193
                        rate = B38400;
 
194
                        break;
 
195
#endif
 
196
                case 19200:
 
197
                        rate = B19200;
 
198
                        break;
 
199
                case 9600:
 
200
                        rate = B9600;
 
201
                        break;
 
202
                case 4800:
 
203
                        rate = B4800;
 
204
                        break;
 
205
                case 2400:
 
206
                        rate = B2400;
 
207
                        break;
 
208
                case 1200:
 
209
                        rate = B1200;
 
210
                        break;
 
211
                case 600:
 
212
                        rate = B600;
 
213
                        break;
 
214
                case 300:
 
215
                        rate = B300;
 
216
                        break;
 
217
                case 110:
 
218
                        rate = B110;
 
219
                        break;
 
220
#ifdef  B0
 
221
                case 0:
 
222
                        rate = B0;
 
223
                        break;
 
224
#endif
 
225
        }
 
226
 
 
227
        struct termios *attr = (struct termios *)current;
 
228
        cfsetispeed(attr, rate);
 
229
        cfsetospeed(attr, rate);
 
230
        if (0 != tcsetattr(comDevice, TCSANOW, attr))
 
231
                return errno;
 
232
 
 
233
        return 0;
 
234
}
 
235
 
 
236
unsigned long Serial::getSpeed(void)
 
237
{
 
238
        unsigned long speed = 0;
 
239
        unsigned long rate = B2400;
 
240
        struct termios *attr = (struct termios *)current;
 
241
        tcgetattr(comDevice, attr);
 
242
        rate = cfgetispeed(attr);
 
243
        
 
244
        switch(rate) 
 
245
        {
 
246
#ifdef B115200
 
247
                case B115200:
 
248
                        speed = 115200;
 
249
                        break;
 
250
#endif
 
251
#ifdef B57600
 
252
                case B57600:
 
253
                        speed = 57600;
 
254
                        break;
 
255
#endif
 
256
#ifdef B38400
 
257
                case B38400:
 
258
                        speed = 38400;
 
259
                        break;
 
260
#endif
 
261
                case B19200:
 
262
                        speed = 19200;
 
263
                        break;
 
264
                case B9600:
 
265
                        speed = 9600;
 
266
                        break;
 
267
                case B4800:
 
268
                        speed = 4800;
 
269
                        break;
 
270
                case B2400:
 
271
                        speed = 2400;
 
272
                        break;
 
273
                case B1200:
 
274
                        speed = 1200;
 
275
                        break;
 
276
                case B600:
 
277
                        speed = 600;
 
278
                        break;
 
279
                case B300:
 
280
                        speed = 300;
 
281
                        break;
 
282
                case B110:
 
283
                        speed = 110;
 
284
                        break;
 
285
#ifdef  B0
 
286
                case B0:
 
287
                        speed = 0;
 
288
                        break;
 
289
#endif
 
290
        }
 
291
 
 
292
        return speed;
 
293
}
 
294
 
 
295
int Serial::setFlowControl(Flow flow)
 
296
{
 
297
 
 
298
        struct termios *attr = (struct termios *)current;
 
299
 
 
300
        attr->c_cflag &= ~CRTSCTS;
 
301
        attr->c_iflag &= ~(IXON | IXANY | IXOFF);
 
302
 
 
303
        switch(flow) {
 
304
        case flowSoft:
 
305
                attr->c_iflag |= (IXON | IXANY | IXOFF);
 
306
                break;
 
307
        case flowBoth:
 
308
                attr->c_iflag |= (IXON | IXANY | IXOFF);
 
309
        case flowHard:
 
310
                attr->c_cflag |= CRTSCTS;
 
311
                break;
 
312
        case flowNone:
 
313
        default:
 
314
                break;
 
315
        }
 
316
 
 
317
        if (0 != tcsetattr(comDevice, TCSANOW, attr))
 
318
                return errno;
 
319
 
 
320
        return 0;
 
321
}
 
322
 
 
323
int Serial::setStopBits(int bits)
 
324
{
 
325
        struct termios *attr = (struct termios *)current;
 
326
        attr->c_cflag &= ~CSTOPB;
 
327
 
 
328
        switch(bits) 
 
329
        {
 
330
        case 2:
 
331
                attr->c_cflag |= CSTOPB;
 
332
                break;
 
333
        }
 
334
 
 
335
        if (0 != tcsetattr(comDevice, TCSANOW, attr))
 
336
                return errno;
 
337
 
 
338
        return 0;
 
339
}
 
340
 
 
341
int Serial::setCharBits(int bits)
 
342
{
 
343
        struct termios *attr = (struct termios *)current;
 
344
        attr->c_cflag &= ~CSIZE;
 
345
 
 
346
        switch(bits) 
 
347
        {
 
348
        case 5:
 
349
                attr->c_cflag |= CS5;
 
350
                break;
 
351
        case 6:
 
352
                attr->c_cflag |= CS6;
 
353
                break;
 
354
        case 7:
 
355
                attr->c_cflag |= CS7;
 
356
                break;
 
357
        case 8:
 
358
        default:
 
359
                attr->c_cflag |= CS8;
 
360
                break;
 
361
        }
 
362
 
 
363
        if (0 != tcsetattr(comDevice, TCSANOW, attr))
 
364
                return errno;
 
365
 
 
366
        return 0;
 
367
}
 
368
 
 
369
int Serial::setParity(Parity parity)
 
370
{
 
371
        struct termios *attr = (struct termios *)current;
 
372
        attr->c_cflag &= ~(PARENB | PARODD);
 
373
 
 
374
        switch(parity) 
 
375
        {
 
376
        case parityEven:
 
377
                attr->c_cflag |= PARENB;
 
378
                break;
 
379
        case parityOdd:
 
380
                attr->c_cflag |= (PARENB | PARODD);
 
381
                break;
 
382
        case parityNone:
 
383
        default:
 
384
                break;
 
385
        }
 
386
 
 
387
        if (0 != tcsetattr(comDevice, TCSANOW, attr))
 
388
                return errno;
 
389
 
 
390
        return 0;
 
391
}
 
392
 
 
393
void Serial::sendBreak(void)
 
394
{
 
395
        tcsendbreak(comDevice, 0);
 
396
}
 
397
 
 
398
int Serial::bytesAvailable(int & count)
 
399
{
 
400
        int bytes = 0;
 
401
        int rc = ioctl(comDevice, FIONREAD, &bytes);
 
402
        if (-1 == rc)
 
403
        {
 
404
                count = 0;
 
405
                return errno;
 
406
        }
 
407
        count = bytes;
 
408
        return 0;
 
409
}
 
410
 
 
411
int Serial::raiseDTR(void)
 
412
{
 
413
        int status;
 
414
 
 
415
        int rc = ioctl(comDevice, TIOCMGET, &status);
 
416
        if (-1 == rc)
 
417
                return errno;
 
418
 
 
419
        status &= ~TIOCM_DTR;
 
420
 
 
421
        rc = ioctl(comDevice, TIOCMGET, &status);
 
422
        if (-1 == rc)
 
423
                return errno;
 
424
 
 
425
        return 0;
 
426
}
 
427
 
 
428
int Serial::lowerDTR(void)
 
429
{
 
430
        int status;
 
431
 
 
432
        int rc = ioctl(comDevice, TIOCMGET, &status);
 
433
        if (-1 == rc)
 
434
                return errno;
 
435
 
 
436
        status |= TIOCM_DTR;
 
437
 
 
438
        rc = ioctl(comDevice, TIOCMGET, &status);
 
439
        if (-1 == rc)
 
440
                return errno;
 
441
 
 
442
        return 0;
 
443
}
 
444
 
 
445
int Serial::toggleDTR(timeout_t millisec)
 
446
{
 
447
        int rc = lowerDTR();
 
448
        if (rc)
 
449
                return rc;
 
450
 
 
451
        if(millisec) 
 
452
        {
 
453
                wait(millisec);
 
454
                rc = lowerDTR();
 
455
                if (rc)
 
456
                        return rc;
 
457
        }
 
458
        return 0;
 
459
}
 
460
 
 
461
void Serial::wait(timeout_t ms)
 
462
{
 
463
        struct timespec t;
 
464
        t.tv_sec = ms / 1000;
 
465
        t.tv_nsec = ((ms % 1000) * 1000000l);
 
466
        nanosleep(&t, 0);
 
467
}
 
468
 
 
469
bool Serial::isPending(Pending pending, timeout_t timeout)
 
470
{
 
471
 
 
472
        int status;
 
473
        struct pollfd pfd;
 
474
 
 
475
        pfd.fd = comDevice;
 
476
        pfd.revents = 0;
 
477
        switch(pending) 
 
478
        {
 
479
        case pendingInput:
 
480
                pfd.events = POLLIN;
 
481
                break;
 
482
        case pendingOutput:
 
483
                pfd.events = POLLOUT;
 
484
                break;
 
485
        case pendingError:
 
486
                pfd.events = POLLERR | POLLHUP;
 
487
                break;
 
488
        }
 
489
 
 
490
        status = 0;
 
491
        while(status < 1) 
 
492
        {
 
493
                if(timeout == TIMEOUT_INFINITE)
 
494
                        status = poll(&pfd, 1, -1);
 
495
                else
 
496
                        status = poll(&pfd, 1, timeout);
 
497
 
 
498
                if(status < 1) 
 
499
                {
 
500
                        if(status == -1 && errno == EINTR)
 
501
                                continue;
 
502
                        return false;
 
503
                }
 
504
        }
 
505
 
 
506
        if(pfd.revents & pfd.events)
 
507
                return true;
 
508
 
 
509
        return false;
 
510
}
 
511