21
#define MAX_CANON MAX_INPUT
27
#define CRTSCTS (CNEW_RTSCTS)
31
#if defined(CTSXON) && defined(RTSXOFF) && !defined(CRTSCTS)
32
#define CRTSCTS (CTSXON | RTSXOFF)
39
int Serial::devInit(const char *fname)
42
current = new struct termios;
43
original = new struct termios;
50
if(!isatty(comDevice))
63
void Serial::initConfig(void)
66
fcntl(comDevice, F_SETFL, FNDELAY);
68
tcgetattr(comDevice, (struct termios *)original);
70
bzero(current, sizeof(struct termios));
72
((struct termios *)current)->c_cc[VMIN] = 0;
73
((struct termios *)current)->c_cc[VTIME] = 0;
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);
81
cfsetispeed(((struct termios *)current), B4800);
82
cfsetospeed(((struct termios *)current), B4800);
84
tcsetattr(comDevice, TCSANOW, (struct termios *)current);
88
void Serial::restore(void)
90
memcpy(current, original, sizeof(struct termios));
91
tcsetattr(comDevice, TCSANOW, (struct termios *)current);
94
void Serial::endSerial(void)
96
if(comDevice < 0 && original)
97
tcsetattr(comDevice, TCSANOW, (struct termios *)original);
100
delete (struct termios *)current;
103
delete (struct termios *)original;
112
void Serial::flushInput(void)
114
tcflush(comDevice, TCIFLUSH);
117
void Serial::flushOutput(void)
119
tcflush(comDevice, TCOFLUSH);
122
void Serial::waitOutput(void)
127
int Serial::devOpen(const char * fname)
129
int cflags = O_RDWR | O_NDELAY | O_NOCTTY;
130
comDevice = open(fname, cflags);
138
int Serial::devRead(char *Data, const int Length)
140
return read(comDevice, Data, Length);
143
int Serial::devWrite(const char *Data, const int Length)
145
return write(comDevice, Data, Length);
148
void Serial::devClose()
155
int Serial::setSpeed(unsigned long speed)
157
unsigned long rate = B4800;
207
struct termios *attr = (struct termios *)current;
208
cfsetispeed(attr, rate);
209
cfsetospeed(attr, rate);
210
if (0 != tcsetattr(comDevice, TCSANOW, attr))
216
unsigned long Serial::getSpeed(void)
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);
275
int Serial::setFlowControl(Flow flow)
278
struct termios *attr = (struct termios *)current;
280
attr->c_cflag &= ~CRTSCTS;
281
attr->c_iflag &= ~(IXON | IXANY | IXOFF);
285
attr->c_iflag |= (IXON | IXANY | IXOFF);
288
attr->c_iflag |= (IXON | IXANY | IXOFF);
290
attr->c_cflag |= CRTSCTS;
297
if (0 != tcsetattr(comDevice, TCSANOW, attr))
303
int Serial::setStopBits(int bits)
305
struct termios *attr = (struct termios *)current;
306
attr->c_cflag &= ~CSTOPB;
311
attr->c_cflag |= CSTOPB;
315
if (0 != tcsetattr(comDevice, TCSANOW, attr))
321
int Serial::setCharBits(int bits)
323
struct termios *attr = (struct termios *)current;
324
attr->c_cflag &= ~CSIZE;
329
attr->c_cflag |= CS5;
332
attr->c_cflag |= CS6;
335
attr->c_cflag |= CS7;
339
attr->c_cflag |= CS8;
343
if (0 != tcsetattr(comDevice, TCSANOW, attr))
349
int Serial::setParity(Parity parity)
351
struct termios *attr = (struct termios *)current;
352
attr->c_cflag &= ~(PARENB | PARODD);
357
attr->c_cflag |= PARENB;
360
attr->c_cflag |= (PARENB | PARODD);
367
if (0 != tcsetattr(comDevice, TCSANOW, attr))
373
void Serial::sendBreak(void)
375
tcsendbreak(comDevice, 0);
378
int Serial::bytesAvailable(int & count)
381
int rc = ioctl(comDevice, FIONREAD, &bytes);
391
int Serial::raiseDTR(void)
395
int rc = ioctl(comDevice, TIOCMGET, &status);
399
status &= ~TIOCM_DTR;
401
rc = ioctl(comDevice, TIOCMGET, &status);
408
int Serial::lowerDTR(void)
412
int rc = ioctl(comDevice, TIOCMGET, &status);
418
rc = ioctl(comDevice, TIOCMGET, &status);
425
int Serial::toggleDTR(timeout_t millisec)
441
void Serial::wait(timeout_t ms)
444
t.tv_sec = ms / 1000;
445
t.tv_nsec = ((ms % 1000) * 1000000l);
449
bool Serial::isPending(Pending pending, timeout_t timeout)
463
pfd.events = POLLOUT;
466
pfd.events = POLLERR | POLLHUP;
473
if(timeout == TIMEOUT_INFINITE)
474
status = poll(&pfd, 1, -1);
476
status = poll(&pfd, 1, timeout);
480
if(status == -1 && errno == EINTR)
486
if(pfd.revents & pfd.events)
2
* Written by and Copyright (C) 2008 the SourceForge
3
* Rain8Net team. http://rain8net.sourceforge.net/
5
* This file is part of Rain8Net.
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.
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.
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/>.
26
#include <sys/types.h>
29
#include <sys/ioctl.h>
41
#define MAX_CANON MAX_INPUT
47
#define CRTSCTS (CNEW_RTSCTS)
51
#if defined(CTSXON) && defined(RTSXOFF) && !defined(CRTSCTS)
52
#define CRTSCTS (CTSXON | RTSXOFF)
59
int Serial::devInit(const char *fname)
62
current = new struct termios;
63
original = new struct termios;
70
if(!isatty(comDevice))
83
void Serial::initConfig(void)
86
fcntl(comDevice, F_SETFL, FNDELAY);
88
tcgetattr(comDevice, (struct termios *)original);
90
bzero(current, sizeof(struct termios));
92
((struct termios *)current)->c_cc[VMIN] = 0;
93
((struct termios *)current)->c_cc[VTIME] = 0;
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);
101
cfsetispeed(((struct termios *)current), B4800);
102
cfsetospeed(((struct termios *)current), B4800);
104
tcsetattr(comDevice, TCSANOW, (struct termios *)current);
108
void Serial::restore(void)
110
memcpy(current, original, sizeof(struct termios));
111
tcsetattr(comDevice, TCSANOW, (struct termios *)current);
114
void Serial::endSerial(void)
116
if(comDevice < 0 && original)
117
tcsetattr(comDevice, TCSANOW, (struct termios *)original);
120
delete (struct termios *)current;
123
delete (struct termios *)original;
132
void Serial::flushInput(void)
134
tcflush(comDevice, TCIFLUSH);
137
void Serial::flushOutput(void)
139
tcflush(comDevice, TCOFLUSH);
142
void Serial::waitOutput(void)
147
int Serial::devOpen(const char * fname)
149
int cflags = O_RDWR | O_NDELAY | O_NOCTTY;
150
comDevice = open(fname, cflags);
158
int Serial::devRead(char *Data, const int Length)
160
return read(comDevice, Data, Length);
163
int Serial::devWrite(const char *Data, const int Length)
165
return write(comDevice, Data, Length);
168
void Serial::devClose()
175
int Serial::setSpeed(unsigned long speed)
177
unsigned long rate = B4800;
227
struct termios *attr = (struct termios *)current;
228
cfsetispeed(attr, rate);
229
cfsetospeed(attr, rate);
230
if (0 != tcsetattr(comDevice, TCSANOW, attr))
236
unsigned long Serial::getSpeed(void)
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);
295
int Serial::setFlowControl(Flow flow)
298
struct termios *attr = (struct termios *)current;
300
attr->c_cflag &= ~CRTSCTS;
301
attr->c_iflag &= ~(IXON | IXANY | IXOFF);
305
attr->c_iflag |= (IXON | IXANY | IXOFF);
308
attr->c_iflag |= (IXON | IXANY | IXOFF);
310
attr->c_cflag |= CRTSCTS;
317
if (0 != tcsetattr(comDevice, TCSANOW, attr))
323
int Serial::setStopBits(int bits)
325
struct termios *attr = (struct termios *)current;
326
attr->c_cflag &= ~CSTOPB;
331
attr->c_cflag |= CSTOPB;
335
if (0 != tcsetattr(comDevice, TCSANOW, attr))
341
int Serial::setCharBits(int bits)
343
struct termios *attr = (struct termios *)current;
344
attr->c_cflag &= ~CSIZE;
349
attr->c_cflag |= CS5;
352
attr->c_cflag |= CS6;
355
attr->c_cflag |= CS7;
359
attr->c_cflag |= CS8;
363
if (0 != tcsetattr(comDevice, TCSANOW, attr))
369
int Serial::setParity(Parity parity)
371
struct termios *attr = (struct termios *)current;
372
attr->c_cflag &= ~(PARENB | PARODD);
377
attr->c_cflag |= PARENB;
380
attr->c_cflag |= (PARENB | PARODD);
387
if (0 != tcsetattr(comDevice, TCSANOW, attr))
393
void Serial::sendBreak(void)
395
tcsendbreak(comDevice, 0);
398
int Serial::bytesAvailable(int & count)
401
int rc = ioctl(comDevice, FIONREAD, &bytes);
411
int Serial::raiseDTR(void)
415
int rc = ioctl(comDevice, TIOCMGET, &status);
419
status &= ~TIOCM_DTR;
421
rc = ioctl(comDevice, TIOCMGET, &status);
428
int Serial::lowerDTR(void)
432
int rc = ioctl(comDevice, TIOCMGET, &status);
438
rc = ioctl(comDevice, TIOCMGET, &status);
445
int Serial::toggleDTR(timeout_t millisec)
461
void Serial::wait(timeout_t ms)
464
t.tv_sec = ms / 1000;
465
t.tv_nsec = ((ms % 1000) * 1000000l);
469
bool Serial::isPending(Pending pending, timeout_t timeout)
483
pfd.events = POLLOUT;
486
pfd.events = POLLERR | POLLHUP;
493
if(timeout == TIMEOUT_INFINITE)
494
status = poll(&pfd, 1, -1);
496
status = poll(&pfd, 1, timeout);
500
if(status == -1 && errno == EINTR)
506
if(pfd.revents & pfd.events)