/
serial_pos.cpp
287 lines (251 loc) · 9.47 KB
/
serial_pos.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
/**
* @file /src/lib/serial_pos.cpp
*
* @brief Posix implementation for serial ports.
*
* @date September 2009
**/
/*****************************************************************************
** Platform Check
*****************************************************************************/
#include <ecl/config/ecl.hpp>
#ifdef ECL_IS_POSIX
/*****************************************************************************
** Includes
*****************************************************************************/
#include <fcntl.h>
#include <sys/ioctl.h>
#include <termios.h>
#include <cstdlib> // div
#include <ecl/exceptions/standard_exception.hpp>
#include <ecl/exceptions/macros.hpp>
#include "../../include/ecl/devices/serial_pos.hpp"
#include "../../include/ecl/devices/detail/error_handler_pos.hpp"
/*****************************************************************************
** Namespaces
*****************************************************************************/
namespace ecl {
/*****************************************************************************
** Using
*****************************************************************************/
using std::string;
using ecl::InvalidInputError;
using ecl::OpenError;
using ecl::StandardException;
/*****************************************************************************
** Implementation [Serial][C&D]
*****************************************************************************/
Serial::Serial(const std::string& port_name, const BaudRate &baud_rate, const DataBits &data_bits,
const StopBits &stop_bits, const Parity &parity ) ecl_throw_decl(StandardException) :
port(port_name),
read_timeout_ms(5000),
error_handler(NoError)
{
ecl_try {
open(port_name, baud_rate, data_bits, stop_bits, parity);
} ecl_catch( const StandardException &e ) {
ecl_throw(StandardException(LOC,e));
}
}
Serial::~Serial() {
close();
}
/*****************************************************************************
** Implementation [Serial][Open]
*****************************************************************************/
bool Serial::open(const std::string& port_name, const BaudRate &baud_rate, const DataBits &data_bits,
const StopBits &stop_bits, const Parity &parity ) ecl_throw_decl(StandardException) {
/*********************
** Input Checks
**********************/
if ( stop_bits == StopBits_15 ) {
ecl_throw(StandardException(LOC,ConfigurationError,"Standard serial device does not accept StopBits_15 as valid (used in ftdi)."));
error_handler = InvalidArgError;
is_open = false;
return false;
}
if ( open() ) {
close();
}
port = port_name;
// One curious thing here is O_NONBLOCK. If you do not specify this, it will try
// and wait till the line has an 'other' end. Which it can't possibly know, so
// it will effectively block here forever.
file_descriptor = ::open( port_name.c_str(), O_RDWR | O_NOCTTY | O_NONBLOCK );
if ( file_descriptor == -1 ) {
ecl_throw(devices::open_exception(LOC,port_name));
error_handler = devices::open_error();
is_open = false;
return false;
}
static const int baud_rate_flags[] = { B110, B300, B600, B1200, B2400, B4800, B9600, B19200, B38400, B57600, B115200, B921600 };
static const int data_bits_flags[] = { CS5, CS6, CS7, CS8 };
/*********************
** Current Settings
**********************/
// tcgetattr(file_descriptor,&options); // Need?
/******************************************
** Flow Control
*******************************************/
// Turn off the O_NONBLOCK we set back when opening (we want by default
// blocking and timeouts etc to occur).
// Using 0 as the argument here resets the file status flags to defaults.
fcntl(file_descriptor, F_SETFL, 0);
// this came from ros' hokoyu node code for setting a lock on the device file descriptor
// useful if you want to make sure other devices dont mess with your device!
struct flock file_lock;
file_lock.l_type = F_WRLCK;
file_lock.l_whence = SEEK_SET;
file_lock.l_start = 0;
file_lock.l_len = 0;
file_lock.l_pid = getpid();
if (fcntl(file_descriptor, F_SETLK, &file_lock) != 0) {
ecl_throw_decl(StandardException(LOC,OpenError,std::string("Device is already locked. Try 'lsof | grep ") + port + std::string("' to find other processes that currently have the port open.")));
error_handler = IsLockedError;
is_open = false;
return false;
}
/******************************************
** Initialise flags
*******************************************/
options.c_cflag = 0;
options.c_iflag = 0;
options.c_lflag = 0;
options.c_oflag = 0;
/*********************
** Baud rates
**********************/
cfsetispeed(&options, baud_rate_flags[baud_rate]);
/*********************
** Ownership
**********************/
/*
* CLOCAL and CREAD should always be set.
* They make sure the owner of the port is
* not changed by the program.
*/
options.c_cflag |= CLOCAL;
options.c_cflag |= CREAD;
/*********************
** Disable Flow control
**********************/
#if defined(CRTSCTS)
options.c_cflag &= ~CRTSCTS; // Disable hardware flow control (old)
#elif defined (CNEW_RTSCTS)
options.c_cflag &= ~CNEW_RTSCTS; // Disable hardware flow control (new)
#endif
/*********************
** StopBits
**********************/
if ( stop_bits == StopBits_1 ) {
options.c_cflag &= ~CSTOPB; // One stop bit only
} else {
options.c_cflag |= CSTOPB; // Two stop bits.
}
/******************************************
** DataBits
*******************************************/
options.c_cflag &= ~CSIZE; // Mask (i.e. reset) character size bits
options.c_cflag |= data_bits_flags[data_bits]; // Now set the # data bits
/******************************************
** Processing
*******************************************/
options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); // raw, no echoing or waiting till a flush or signals
// options.c_lflag |= (ICANON | ECHO | ECHOE); // like a chat session
/******************************************
** Input Options
*******************************************/
options.c_iflag &= ~(IXON | IXOFF | IXANY); // Disable software flow control
/******************************************
** Output Options
*******************************************/
// options.c_oflag |= OPOST; // Perform a little post processing
// options.c_oflag |= ONLCR; // Man NL to NL-CR
/*********************
** Parity
**********************/
if ( parity == NoParity ) {
options.c_cflag &= ~PARENB; // No Parity
} else if ( parity == EvenParity ) {
options.c_iflag |= (INPCK | ISTRIP); // Enable parity check and strip parity bit
options.c_cflag |= PARENB;
options.c_cflag &= ~PARODD;
} else { // OddParity
options.c_iflag |= (INPCK | ISTRIP); // Enable parity check and strip parity bit
options.c_cflag |= PARENB;
options.c_cflag |= PARODD;
}
/******************************************
** Enable
*******************************************
* TCSANOW - make changes immediately
* TCSADRAIN - wait till everything is transmitted
* TCSAFLUSH - clear input/output buffers and change
*******************************************/
tcsetattr(file_descriptor,TCSAFLUSH,&options);
block(5000);
is_open = true;
error_handler = NoError;
return true;
}
void Serial::close() {
if ( open() ) {
// should check return values here, it does have some, EBADF/EINTR/EIO
::close(file_descriptor);
is_open = false;
}
}
/*****************************************************************************
** Implementation [Serial][Reading Modes]
*****************************************************************************/
void Serial::block(const unsigned long &timeout) {
if ( timeout < 100 ) {
if ( timeout < 5 ) {
fake_snooze.period(ecl::Duration(0.001));
fake_loop_count = timeout;
} else if ( timeout < 20 ) {
fake_snooze.period(ecl::Duration(0.002));
div_t d = div(timeout,2);
if ( d.rem == 0 ) {
fake_loop_count = d.quot;
} else {
fake_loop_count = d.quot + 1;
}
} else {
fake_snooze.period(ecl::Duration(0.005));
div_t d = div(timeout,5);
if ( d.rem == 0 ) {
fake_loop_count = d.quot;
} else {
fake_loop_count = d.quot + 1;
}
}
this->unblock();
} else {
options.c_cc[VMIN] = 0;
// VTIME is in tenths of a second
if ( timeout < 100 ) { // just in case we're not running in debug mode or exceptions are disabled.
options.c_cc[VTIME] = static_cast<unsigned char>(1); // 100/100
} else {
options.c_cc[VTIME] = static_cast<unsigned char>(timeout/100);
}
tcsetattr(file_descriptor, TCSAFLUSH, &options);
}
read_timeout_ms = timeout;
}
void Serial::unblock() {
options.c_cc[VMIN] = 0;
options.c_cc[VTIME] = 0;
tcsetattr(file_descriptor, TCSAFLUSH, &options);
read_timeout_ms = 5000; // just so we don't trigger fake_read calls in read(xxx)
}
/*****************************************************************************
** Implementation [Serial][Reading]
*****************************************************************************/
long Serial::remaining() {
long bytes = 0;
ioctl(file_descriptor, FIONREAD, &bytes);
return bytes;
}
} // namespace ecl
#endif /* ECL_IS_POSIX */