2012-01-11 17:48:31 -06:00
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
#include <fcntl.h>
|
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
|
#include <errno.h>
|
|
|
|
|
#include <paths.h>
|
|
|
|
|
#include <sysexits.h>
|
|
|
|
|
#include <termios.h>
|
|
|
|
|
#include <sys/param.h>
|
|
|
|
|
#include <sys/select.h>
|
|
|
|
|
#include <sys/time.h>
|
|
|
|
|
#include <time.h>
|
|
|
|
|
|
2012-01-10 14:19:56 -06:00
|
|
|
#include "serial/impl/unix.h"
|
|
|
|
|
|
2012-01-11 17:48:31 -06:00
|
|
|
#ifndef TIOCINQ
|
|
|
|
|
#ifdef FIONREAD
|
|
|
|
|
#define TIOCINQ FIONREAD
|
|
|
|
|
#else
|
|
|
|
|
#define TIOCINQ 0x541B
|
|
|
|
|
#endif
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
using ::serial::Serial;
|
|
|
|
|
using std::string;
|
2012-01-10 14:19:56 -06:00
|
|
|
|
2012-01-11 17:48:31 -06:00
|
|
|
Serial::SerialImpl::SerialImpl (const string &port, int baudrate,
|
2012-01-11 23:53:10 -06:00
|
|
|
long timeout, bytesize_t bytesize,
|
|
|
|
|
parity_t parity, stopbits_t stopbits,
|
|
|
|
|
flowcontrol_t flowcontrol)
|
|
|
|
|
: fd_(-1), isOpen_(false), interCharTimeout_(-1), port_(port),
|
|
|
|
|
baudrate_(baudrate), timeout_(timeout), bytesize_(bytesize),
|
|
|
|
|
parity_(parity), stopbits_(stopbits), flowcontrol_(flowcontrol)
|
2012-01-10 14:19:56 -06:00
|
|
|
{
|
2012-01-11 23:53:10 -06:00
|
|
|
if (port_.empty() == false) this->open();
|
2012-01-10 14:19:56 -06:00
|
|
|
}
|
|
|
|
|
|
2012-01-11 17:48:31 -06:00
|
|
|
Serial::SerialImpl::~SerialImpl () {
|
|
|
|
|
this->close();
|
2012-01-10 14:19:56 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
2012-01-11 17:48:31 -06:00
|
|
|
Serial::SerialImpl::open () {
|
2012-01-11 23:53:10 -06:00
|
|
|
if (port_.empty() == false) throw "error";
|
|
|
|
|
if (isOpen_ == false) throw "error";
|
|
|
|
|
|
|
|
|
|
fd_ = ::open (port_.c_str(), O_RDWR | O_NOCTTY | O_NONBLOCK);
|
|
|
|
|
|
|
|
|
|
if (fd_ == -1) throw "Error";
|
2012-01-10 14:19:56 -06:00
|
|
|
|
2012-01-11 23:53:10 -06:00
|
|
|
reconfigurePort();
|
|
|
|
|
isOpen_ = true;
|
2012-01-10 14:19:56 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
2012-01-11 17:48:31 -06:00
|
|
|
Serial::SerialImpl::reconfigurePort () {
|
2012-01-11 23:53:10 -06:00
|
|
|
if (fd_ == -1) throw "Error"; // Can only operate on a valid file descriptor
|
|
|
|
|
|
|
|
|
|
struct termios options; // The current options for the file descriptor
|
|
|
|
|
struct termios originalTTYAttrs; // The orignal file descriptor options
|
|
|
|
|
|
2012-01-11 17:48:31 -06:00
|
|
|
uint8_t vmin = 0, vtime = 0; // timeout is done via select
|
|
|
|
|
if (interCharTimeout_ == -1) {
|
2012-01-11 23:53:10 -06:00
|
|
|
vmin = 1;
|
|
|
|
|
vtime = int(interCharTimeout_ * 10);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (tcgetattr(fd_, &originalTTYAttrs) == -1) throw "Error";
|
|
|
|
|
|
|
|
|
|
options = originalTTYAttrs;
|
|
|
|
|
|
2012-01-11 17:48:31 -06:00
|
|
|
// set up raw mode / no echo / binary
|
|
|
|
|
options.c_cflag |= (CLOCAL|CREAD);
|
|
|
|
|
options.c_lflag &= ~(ICANON|ECHO|ECHOE|ECHOK|ECHONL|
|
|
|
|
|
ISIG|IEXTEN); //|ECHOPRT
|
|
|
|
|
|
|
|
|
|
options.c_oflag &= ~(OPOST);
|
|
|
|
|
options.c_iflag &= ~(INLCR|IGNCR|ICRNL|IGNBRK);
|
|
|
|
|
#ifdef IUCLC
|
|
|
|
|
options.c_iflag &= ~IUCLC;
|
|
|
|
|
#endif
|
|
|
|
|
#ifdef PARMRK
|
2012-01-11 23:53:10 -06:00
|
|
|
options.c_iflag &= ~PARMRK;
|
2012-01-11 17:48:31 -06:00
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
// setup baud rate
|
2012-01-11 23:53:10 -06:00
|
|
|
// TODO(ash_git): validate baud rate
|
|
|
|
|
cfsetspeed(&options, baudrate_);
|
2012-01-11 17:48:31 -06:00
|
|
|
|
|
|
|
|
// setup char len
|
|
|
|
|
options.c_cflag &= ~CSIZE;
|
|
|
|
|
if (bytesize_ == EIGHTBITS)
|
|
|
|
|
options.c_cflag |= CS8;
|
|
|
|
|
else if (bytesize_ == SEVENBITS)
|
|
|
|
|
options.c_cflag |= CS7;
|
|
|
|
|
else if (bytesize_ == SIXBITS)
|
|
|
|
|
options.c_cflag |= CS6;
|
|
|
|
|
else if (bytesize_ == FIVEBITS)
|
|
|
|
|
options.c_cflag |= CS5;
|
|
|
|
|
else
|
|
|
|
|
throw "ValueError(Invalid char len: %%r)";
|
|
|
|
|
// setup stopbits
|
|
|
|
|
if (stopbits_ == STOPBITS_ONE)
|
|
|
|
|
options.c_cflag &= ~(CSTOPB);
|
|
|
|
|
else if (stopbits_ == STOPBITS_ONE_POINT_FIVE)
|
|
|
|
|
options.c_cflag |= (CSTOPB); // XXX same as TWO.. there is no POSIX support for 1.5
|
|
|
|
|
else if (stopbits_ == STOPBITS_TWO)
|
|
|
|
|
options.c_cflag |= (CSTOPB);
|
2012-01-11 23:53:10 -06:00
|
|
|
else
|
2012-01-11 17:48:31 -06:00
|
|
|
throw "ValueError(Invalid stop bit specification:)";
|
|
|
|
|
// setup parity
|
|
|
|
|
options.c_iflag &= ~(INPCK|ISTRIP);
|
|
|
|
|
if (parity_ == PARITY_NONE) {
|
|
|
|
|
options.c_cflag &= ~(PARENB|PARODD);
|
|
|
|
|
}
|
2012-01-11 23:53:10 -06:00
|
|
|
else if (parity_ == PARITY_EVEN) {
|
2012-01-11 17:48:31 -06:00
|
|
|
options.c_cflag &= ~(PARODD);
|
|
|
|
|
options.c_cflag |= (PARENB);
|
|
|
|
|
}
|
2012-01-11 23:53:10 -06:00
|
|
|
else if (parity_ == PARITY_ODD) {
|
2012-01-11 17:48:31 -06:00
|
|
|
options.c_cflag |= (PARENB|PARODD);
|
|
|
|
|
}
|
2012-01-11 23:53:10 -06:00
|
|
|
else {
|
2012-01-11 17:48:31 -06:00
|
|
|
throw "ValueError(Invalid parity:";
|
2012-01-11 23:53:10 -06:00
|
|
|
}
|
2012-01-11 17:48:31 -06:00
|
|
|
// setup flow control
|
|
|
|
|
// xonxoff
|
|
|
|
|
#ifdef IXANY
|
|
|
|
|
if (xonxoff_)
|
|
|
|
|
options.c_iflag |= (IXON|IXOFF); //|IXANY)
|
|
|
|
|
else
|
|
|
|
|
options.c_iflag &= ~(IXON|IXOFF|IXANY);
|
|
|
|
|
#else
|
|
|
|
|
if (xonxoff_)
|
|
|
|
|
options.c_iflag |= (IXON|IXOFF);
|
|
|
|
|
else
|
|
|
|
|
options.c_iflag &= ~(IXON|IXOFF);
|
|
|
|
|
#endif
|
|
|
|
|
// rtscts
|
|
|
|
|
#ifdef CRTSCTS
|
|
|
|
|
if (rtscts_)
|
|
|
|
|
options.c_cflag |= (CRTSCTS);
|
|
|
|
|
else
|
|
|
|
|
options.c_cflag &= ~(CRTSCTS);
|
|
|
|
|
#elif defined CNEW_RTSCTS
|
|
|
|
|
if (rtscts_)
|
2012-01-11 23:53:10 -06:00
|
|
|
options.c_cflag |= (CNEW_RTSCTS);
|
2012-01-11 17:48:31 -06:00
|
|
|
else
|
|
|
|
|
options.c_cflag &= ~(CNEW_RTSCTS);
|
|
|
|
|
#else
|
|
|
|
|
#error "OS Support seems wrong."
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
// buffer
|
|
|
|
|
// vmin "minimal number of characters to be read. = for non blocking"
|
|
|
|
|
options.c_cc[VMIN] = vmin;
|
|
|
|
|
// vtime
|
|
|
|
|
options.c_cc[VTIME] = vtime;
|
|
|
|
|
|
|
|
|
|
// activate settings
|
2012-01-11 23:53:10 -06:00
|
|
|
::tcsetattr(fd_, TCSANOW, &options);
|
2012-01-10 14:19:56 -06:00
|
|
|
}
|
|
|
|
|
|
2012-01-11 17:48:31 -06:00
|
|
|
void
|
|
|
|
|
Serial::SerialImpl::close () {
|
2012-01-11 23:53:10 -06:00
|
|
|
if (isOpen_ == true) {
|
|
|
|
|
if (fd_ != -1) {
|
|
|
|
|
::close(fd_);
|
|
|
|
|
fd_ = -1;
|
|
|
|
|
}
|
|
|
|
|
isOpen_ = false;
|
|
|
|
|
}
|
2012-01-10 14:19:56 -06:00
|
|
|
}
|
|
|
|
|
|
2012-01-11 17:48:31 -06:00
|
|
|
bool
|
|
|
|
|
Serial::SerialImpl::isOpen () {
|
|
|
|
|
return isOpen_;
|
2012-01-10 14:19:56 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
size_t
|
2012-01-11 17:48:31 -06:00
|
|
|
Serial::SerialImpl::available () {
|
2012-01-11 23:53:10 -06:00
|
|
|
if (!isOpen_) {
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
int count = 0;
|
|
|
|
|
int result = ioctl(fd_, TIOCINQ, &count);
|
|
|
|
|
if (result == 0) {
|
|
|
|
|
return count;
|
|
|
|
|
} else {
|
|
|
|
|
throw "Error";
|
|
|
|
|
}
|
2012-01-10 14:19:56 -06:00
|
|
|
}
|
|
|
|
|
|
2012-01-11 17:48:31 -06:00
|
|
|
string
|
|
|
|
|
Serial::SerialImpl::read (size_t size) {
|
2012-01-11 23:53:10 -06:00
|
|
|
if (!isOpen_) throw "PortNotOpenError()";
|
|
|
|
|
string message = "";
|
|
|
|
|
char buf[1024];
|
|
|
|
|
fd_set readfds;
|
2012-01-11 17:48:31 -06:00
|
|
|
while (message.length() < size) {
|
2012-01-11 23:53:10 -06:00
|
|
|
FD_ZERO(&readfds);
|
|
|
|
|
FD_SET(fd_, &readfds);
|
|
|
|
|
struct timeval timeout;
|
|
|
|
|
timeout.tv_sec = timeout_ / 1000;
|
|
|
|
|
timeout.tv_usec = timeout_ % 1000;
|
|
|
|
|
int r = select(1, &readfds, NULL, NULL, &timeout);
|
|
|
|
|
|
|
|
|
|
if (r == -1 && errno == EINTR)
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
|
|
if (r == -1) {
|
2012-01-11 17:48:31 -06:00
|
|
|
perror("select()");
|
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (FD_ISSET(fd_, &readfds)) {
|
2012-01-11 23:53:10 -06:00
|
|
|
memset(buf, 0, 1024);
|
|
|
|
|
size_t bytes_read = ::read(fd_, buf, 1024);
|
|
|
|
|
// read should always return some data as select reported it was
|
|
|
|
|
// ready to read when we get to this point.
|
|
|
|
|
if (bytes_read < 1) {
|
|
|
|
|
// Disconnected devices, at least on Linux, show the
|
|
|
|
|
// behavior that they are always ready to read immediately
|
|
|
|
|
// but reading returns nothing.
|
|
|
|
|
throw "SerialException('device reports readiness to read but returned no data (device disconnected?)')";
|
|
|
|
|
}
|
|
|
|
|
message.append(buf, bytes_read);
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
break; // Timeout
|
|
|
|
|
}
|
|
|
|
|
}
|
2012-01-11 17:48:31 -06:00
|
|
|
return message;
|
2012-01-10 14:19:56 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
size_t
|
2012-01-11 17:48:31 -06:00
|
|
|
Serial::SerialImpl::write (const string &data) {
|
2012-01-11 23:53:10 -06:00
|
|
|
if (isOpen_ == false) throw "portNotOpenError";
|
|
|
|
|
|
2012-01-11 21:53:26 -06:00
|
|
|
size_t t = data.length();
|
2012-01-11 23:53:10 -06:00
|
|
|
size_t n = ::write(fd_, data.c_str(), data.length());
|
|
|
|
|
if (n == -1) {
|
|
|
|
|
throw "Write error";
|
|
|
|
|
}
|
2012-01-11 21:53:26 -06:00
|
|
|
return n;
|
2012-01-10 14:19:56 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
2012-01-11 17:48:31 -06:00
|
|
|
Serial::SerialImpl::setPort (const string &port) {
|
|
|
|
|
port_ = port;
|
2012-01-10 14:19:56 -06:00
|
|
|
}
|
|
|
|
|
|
2012-01-11 17:48:31 -06:00
|
|
|
string
|
|
|
|
|
Serial::SerialImpl::getPort () const {
|
|
|
|
|
return port_;
|
2012-01-10 14:19:56 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
2012-01-11 17:48:31 -06:00
|
|
|
Serial::SerialImpl::setTimeout (long timeout) {
|
2012-01-11 21:53:26 -06:00
|
|
|
timeout_ = timeout;
|
2012-01-10 14:19:56 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
long
|
2012-01-11 17:48:31 -06:00
|
|
|
Serial::SerialImpl::getTimeout () const {
|
2012-01-11 21:53:26 -06:00
|
|
|
return timeout_;
|
2012-01-10 14:19:56 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
2012-01-11 17:48:31 -06:00
|
|
|
Serial::SerialImpl::setBaudrate (int baudrate) {
|
2012-01-11 23:53:10 -06:00
|
|
|
baudrate_ = baudrate;
|
|
|
|
|
reconfigurePort();
|
2012-01-10 14:19:56 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int
|
2012-01-11 17:48:31 -06:00
|
|
|
Serial::SerialImpl::getBaudrate () const {
|
|
|
|
|
return baudrate_;
|
2012-01-10 14:19:56 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
2012-01-11 17:48:31 -06:00
|
|
|
Serial::SerialImpl::setBytesize (serial::bytesize_t bytesize) {
|
2012-01-11 23:53:10 -06:00
|
|
|
bytesize_ = bytesize;
|
2012-01-10 14:19:56 -06:00
|
|
|
}
|
|
|
|
|
|
2012-01-11 17:48:31 -06:00
|
|
|
serial::bytesize_t
|
|
|
|
|
Serial::SerialImpl::getBytesize () const {
|
2012-01-11 21:53:26 -06:00
|
|
|
return bytesize_;
|
2012-01-10 14:19:56 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
2012-01-11 17:48:31 -06:00
|
|
|
Serial::SerialImpl::setParity (serial::parity_t parity) {
|
2012-01-11 21:53:26 -06:00
|
|
|
parity_ = parity;
|
2012-01-10 14:19:56 -06:00
|
|
|
}
|
|
|
|
|
|
2012-01-11 17:48:31 -06:00
|
|
|
serial::parity_t
|
|
|
|
|
Serial::SerialImpl::getParity () const {
|
2012-01-11 21:53:26 -06:00
|
|
|
return parity_;
|
2012-01-10 14:19:56 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
2012-01-11 17:48:31 -06:00
|
|
|
Serial::SerialImpl::setStopbits (serial::stopbits_t stopbits) {
|
2012-01-11 21:53:26 -06:00
|
|
|
stopbits_ = stopbits;
|
2012-01-10 14:19:56 -06:00
|
|
|
}
|
|
|
|
|
|
2012-01-11 17:48:31 -06:00
|
|
|
serial::stopbits_t
|
|
|
|
|
Serial::SerialImpl::getStopbits () const {
|
2012-01-11 21:53:26 -06:00
|
|
|
return stopbits_;
|
2012-01-10 14:19:56 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
2012-01-11 17:48:31 -06:00
|
|
|
Serial::SerialImpl::setFlowcontrol (serial::flowcontrol_t flowcontrol) {
|
2012-01-11 21:53:26 -06:00
|
|
|
flowcontrol_ = flowcontrol;
|
2012-01-10 14:19:56 -06:00
|
|
|
}
|
|
|
|
|
|
2012-01-11 17:48:31 -06:00
|
|
|
serial::flowcontrol_t
|
|
|
|
|
Serial::SerialImpl::getFlowcontrol () const {
|
2012-01-11 21:53:26 -06:00
|
|
|
return flowcontrol_;
|
2012-01-10 14:19:56 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|