diff --git a/include/serial/impl/unix.h b/include/serial/impl/unix.h index 5d99ebc..9fe739a 100644 --- a/include/serial/impl/unix.h +++ b/include/serial/impl/unix.h @@ -105,6 +105,7 @@ private: bool isOpen_; int interCharTimeout_; + int writeTimeout_; int xonxoff_; int rtscts_; diff --git a/include/serial/serial.h b/include/serial/serial.h index 60733c4..b4bca19 100644 --- a/include/serial/serial.h +++ b/include/serial/serial.h @@ -39,12 +39,8 @@ #include #include - -#ifdef CXX_11 -#include -#else -// #include -#endif +#include +#include namespace serial { @@ -159,6 +155,11 @@ public: void close (); + /* Return the number of characters in the buffer. + */ + size_t + available(); + /*! Read a given amount of bytes from the serial port. * * If a timeout is set it may return less characters than requested. With @@ -172,8 +173,8 @@ public: * * \return A size_t representing the number of bytes actually read. */ - size_t - read (unsigned char* buffer, size_t size = 1); + //size_t + //read (unsigned char* buffer, size_t size = 1); /*! Read a given amount of bytes from the serial port. * @@ -188,6 +189,9 @@ public: std::string read (size_t size = 1); + std::string readline(size_t size = std::numeric_limits::max(), std::string eol = "\n"); + std::vector readlines(std::string eol = "\n"); + /*! Read a given amount of bytes from the serial port. * * Reads into a std::string by reference rather than returning it. @@ -199,8 +203,8 @@ public: * * \see Serial::read(size_t) */ - size_t - read (std::string &buffer, size_t size = 1); + //size_t + //read (std::string &buffer, size_t size = 1); /*! Write bytes from the data to the serial port by given length. * @@ -211,8 +215,8 @@ public: * * \return A size_t representing the number of bytes actually written. */ - size_t - write (unsigned char* data, size_t length); + //size_t + //write (unsigned char* data, size_t length); /*! Write a string to the serial port. * diff --git a/src/impl/unix.cc b/src/impl/unix.cc index 261824c..766aba4 100644 --- a/src/impl/unix.cc +++ b/src/impl/unix.cc @@ -219,8 +219,8 @@ Serial::SerialImpl::read (size_t size) { FD_ZERO(&readfds); FD_SET(fd_, &readfds); struct timeval timeout; - timeout.tv_sec = timeout_ / 1000000; - timeout.tv_usec = timeout_ % 1000000; + timeout.tv_sec = timeout_ / 1000; + timeout.tv_usec = timeout_ % 1000; int r = select(1, &readfds, NULL, NULL, &timeout); if (r == -1 && errno == EINTR) @@ -253,7 +253,15 @@ Serial::SerialImpl::read (size_t size) { size_t Serial::SerialImpl::write (const string &data) { - + if (isOpen_ == false) { + throw "portNotOpenError"; + } + size_t t = data.length(); + size_t n = ::write(fd_, data.c_str(), data.length()); + if (n == -1) { + throw "Write error"; + } + return n; } void @@ -268,12 +276,12 @@ Serial::SerialImpl::getPort () const { void Serial::SerialImpl::setTimeout (long timeout) { - + timeout_ = timeout; } long Serial::SerialImpl::getTimeout () const { - + return timeout_; } void @@ -289,42 +297,42 @@ Serial::SerialImpl::getBaudrate () const { void Serial::SerialImpl::setBytesize (serial::bytesize_t bytesize) { - + bytesize_ = bytesize; } serial::bytesize_t Serial::SerialImpl::getBytesize () const { - + return bytesize_; } void Serial::SerialImpl::setParity (serial::parity_t parity) { - + parity_ = parity; } serial::parity_t Serial::SerialImpl::getParity () const { - + return parity_; } void Serial::SerialImpl::setStopbits (serial::stopbits_t stopbits) { - + stopbits_ = stopbits; } serial::stopbits_t Serial::SerialImpl::getStopbits () const { - + return stopbits_; } void Serial::SerialImpl::setFlowcontrol (serial::flowcontrol_t flowcontrol) { - + flowcontrol_ = flowcontrol; } serial::flowcontrol_t Serial::SerialImpl::getFlowcontrol () const { - + return flowcontrol_; } diff --git a/src/serial.cc b/src/serial.cc index 4c1bfba..804c7f7 100644 --- a/src/serial.cc +++ b/src/serial.cc @@ -12,6 +12,7 @@ using serial::parity_t; using serial::stopbits_t; using serial::flowcontrol_t; using std::string; +using std::vector; Serial::Serial (const string &port, int baudrate, long timeout, bytesize_t bytesize, @@ -42,18 +43,65 @@ Serial::isOpen () { } size_t -Serial::read (unsigned char* buffer, size_t size) { - // return this->pimpl->read (buffer, size); +Serial::available () { + return this->pimpl->available(); } +//size_t +//Serial::read (unsigned char* buffer, size_t size) { + // return this->pimpl->read (buffer, size); +//} + string Serial::read (size_t size) { return this->pimpl->read (size); } -size_t -Serial::read (string &buffer, size_t size) { -// return this->pimpl->read (buffer, size); +string +Serial::readline(size_t size, string eol) { + size_t leneol = eol.length(); + string line; + while (true) { + string c = read(1); + if (c.empty()) { + line += c; + if (line.substr(line.length() - leneol, leneol) == eol) { + break; + } + if (line.length() >= size) { + break; + } + } + else { + // Timeout + break; + } + } + + return line; +} + +vector +Serial::readlines(string eol) { + if (this->pimpl->getTimeout() < 0) { + throw "Error, must be set for readlines"; + } + size_t leneol = eol.length(); + vector lines; + while (true) { + string line = readline(std::numeric_limits::max(), eol); + if (!line.empty()) { + lines.push_back(line); + if (line.substr(line.length() - leneol, leneol) == eol) + break; + } + else { + // Timeout + break; + } + } + + return lines; } //size_t