55 #define SERIAL_EXCEPT(except, msg, ...) \ 58 snprintf(buf, sizeof(buf), msg " (in SerialPort::%s)", ##__VA_ARGS__, __FUNCTION__); \ 82 fd_ =
::open(port_name, O_RDWR | O_NONBLOCK | O_NOCTTY);
86 const char* extra_msg =
"";
90 extra_msg =
"You probably don't have premission to open the port for reading and writing.";
94 extra_msg =
"The requested port does not exist. Is the device connected? Was the port name misspelled?";
105 fl.l_whence = SEEK_SET;
110 if (fcntl(
fd_, F_SETLK, &fl) != 0)
112 "currently have the port open.",
113 port_name, port_name);
116 struct termios newtio;
117 tcgetattr(
fd_, &newtio);
118 memset(&newtio.c_cc, 0,
sizeof(newtio.c_cc));
119 newtio.c_cflag = CS8 | CLOCAL | CREAD;
120 newtio.c_iflag = IGNPAR;
123 cfsetspeed(&newtio, baud_rate);
127 tcflush(
fd_, TCIFLUSH);
128 if (tcsetattr(
fd_, TCSANOW, &newtio) < 0)
130 "Unable to set serial port attributes. The port you specified (%s) may not be a serial port.",
159 int len = length == -1 ? strlen(data) : length;
162 int origflags = fcntl(
fd_, F_GETFL, 0);
163 fcntl(
fd_, F_SETFL, origflags & ~O_NONBLOCK);
165 fcntl(
fd_, F_SETFL, origflags | O_NONBLOCK);
177 struct pollfd ufd[1];
180 ufd[0].events = POLLIN;
185 if ((retval = poll(ufd, 1, timeout)) < 0)
191 if (ufd[0].revents & POLLERR)
196 if (ret == -1 && errno != EAGAIN && errno != EWOULDBLOCK)
207 struct pollfd ufd[1];
210 ufd[0].events = POLLIN;
215 while (current < length)
217 if ((retval = poll(ufd, 1, timeout)) < 0)
223 if (ufd[0].revents & POLLERR)
226 ret =
::read(
fd_, &buffer[current], length - current);
228 if (ret == -1 && errno != EAGAIN && errno != EWOULDBLOCK)
241 struct pollfd ufd[1];
244 ufd[0].events = POLLIN;
249 while (current < length - 1)
252 if (buffer[current - 1] ==
'\n')
255 if ((retval = poll(ufd, 1, timeout)) < 0)
261 if (ufd[0].revents & POLLERR)
264 ret =
::read(
fd_, &buffer[current], length - current);
266 if (ret == -1 && errno != EAGAIN && errno != EWOULDBLOCK)
278 struct pollfd ufd[1];
281 ufd[0].events = POLLIN;
287 while (buffer->size() < buffer->max_size() / 2)
290 ret = buffer->find_first_of(
'\n');
294 buffer->erase(ret + 1, buffer->size() - ret - 1);
298 if ((retval = poll(ufd, 1, timeout)) < 0)
304 if (ufd[0].revents & POLLERR)
307 char temp_buffer[128];
310 if (ret == -1 && errno != EAGAIN && errno != EWOULDBLOCK)
316 buffer->append(temp_buffer, ret);
318 catch (std::length_error& le)
330 struct pollfd ufd[1];
331 static std::string erased;
334 ufd[0].events = POLLIN;
341 while (buffer->size() < buffer->max_size() / 2)
343 if ((retval = poll(ufd, 1, timeout)) < 0)
349 if (ufd[0].revents & POLLERR)
357 buffer->append(erased);
360 catch (std::length_error& le)
369 if (ret == -1 && errno != EAGAIN && errno != EWOULDBLOCK)
375 buffer->append(temp_buffer, ret);
377 catch (std::length_error& le)
383 ret = buffer->find_first_of(start);
389 buffer->erase(0, ret);
392 ret = buffer->find_first_of(end);
396 erased = buffer->substr(ret + 1, buffer->size() - ret - 1);
398 buffer->erase(ret + 1, buffer->size() - ret - 1);
407 int retval = tcflush(
fd_, TCIOFLUSH);
433 struct pollfd ufd[1];
435 ufd[0].events = POLLIN;
441 if (poll(ufd, 1, 10) > 0)
443 if (!(ufd[0].revents & POLLERR))
489 if (!error && data.size() > 0)
529 if (!error && data.size() > 0)
bool startReadStream(std::function< void(char *, int)> f)
Start a stream of read()
void close()
Close the serial port.
bool startReadLineStream(std::function< void(std::string *)> f)
Start a stream of readLine(std::string*, int)
std::function< void(std::string *)> readBetweenCallback
Stream readBetween callback boost function.
int write(const char *data, int length=-1)
Write to the port.
bool stream_paused_
Whether streaming is paused or not.
void readThread()
Thread for a stream of read()
std::thread * stream_thread_
Stream thread.
void pauseStream()
Pause streaming.
void open(const char *port_name, int baud_rate=115200)
Open the serial port.
void resumeStream()
Resume streaming.
void readLineThread()
Thread for a stream of readLine(std::string*, int)
void stopStream()
Stop streaming.
bool stream_stopped_
Whether streaming is stopped or not.
int readBytes(char *data, int length, int timeout=-1)
Read a fixed number of bytes from the serial port.
bool portOpen()
Check whether the port is open or not.
int flush()
Wrapper around tcflush.
void readBetweenThread(char start, char end)
Thread for a stream of readBetween()
bool startReadBetweenStream(std::function< void(std::string *)> f, char start, char end)
Start a stream of readBetween()
std::function< void(std::string *)> readLineCallback
Stream readLine callback boost function.
#define SERIAL_EXCEPT(except, msg,...)
Macro for throwing an exception with a message, passing args.
std::function< void(char *, int)> readCallback
Stream read callback boost function.
int read(char *data, int max_length, int timeout=-1)
Read from the port.
bool readBetween(std::string *data, char start, char end, int timeout=-1)
Read from the serial port between a start char and an end char.
int readLine(char *data, int length, int timeout=-1)
Read a line from the serial port.