diff --git a/include/serial/impl/unix.h b/include/serial/impl/unix.h index ec8e246..0f1801a 100644 --- a/include/serial/impl/unix.h +++ b/include/serial/impl/unix.h @@ -104,7 +104,7 @@ protected: private: string port_; // Path to the file descriptor - int fd_; // The current file descriptor. + int fd_; // The current file descriptor int interCharTimeout_; int writeTimeout_; @@ -118,7 +118,7 @@ private: unsigned long baudrate_; // Baudrate parity_t parity_; // Parity - bytesize_t bytesize_; // Size of the bytes + bytesize_t bytesize_; // Size of the bytes stopbits_t stopbits_; // Stop Bits flowcontrol_t flowcontrol_; // Flow Control }; diff --git a/include/serial/serial.h b/include/serial/serial.h index eda593b..d37fb96 100644 --- a/include/serial/serial.h +++ b/include/serial/serial.h @@ -82,21 +82,21 @@ typedef enum { } flowcontrol_t; class SerialExecption : public std::exception { - const char * e_what; + const char * e_what; public: - SerialExecption(const char *description) {e_what=description;}; - virtual const char* what() const throw() { + SerialExecption(const char *description) {e_what=description;}; + virtual const char* what() const throw() { std::stringstream ss; ss << "SerialException " << this->e_what << " failed."; return ss.str().c_str(); - } + } }; class IOException : public std::exception { const char * e_what; public: - IOException(const char * description) {this->e_what = description;} - + IOException(const char * description) {e_what = description;} + virtual const char* what() const throw() { std::stringstream ss; ss << "IO Exception " << this->e_what << " failed."; @@ -105,13 +105,13 @@ public: }; class PortNotOpenedException : public std::exception { - const char * e_what; + const char * e_what; public: - PortNotOpenedException(const char * description) {this->e_what = description;} - + PortNotOpenedException(const char * description) {e_what = description;} + virtual const char* what() const throw() { - std::stringstream ss; - ss << e_what << " called before port was opened."; + std::stringstream ss; + ss << e_what << " called before port was opened."; return ss.str().c_str(); } }; @@ -193,8 +193,7 @@ public: void close (); - /* Return the number of characters in the buffer. - */ + /*! Return the number of characters in the buffer. */ size_t available(); @@ -295,8 +294,8 @@ public: * timeout on reads occur. Setting this to zero (0) will cause reading to be * non-blocking, i.e. the available data will be returned immediately, but it * will not block to wait for more. Setting this to a number less than - * zero (-1) will result in infinite blocking behaviour, i.e. the serial port - * will block until either size bytes have been read or an exception has + * zero (-1) will result in infinite blocking behaviour, i.e. the serial port + * will block until either size bytes have been read or an exception has * occured. */ void @@ -337,8 +336,8 @@ public: /*! Sets the bytesize for the serial port. * - * \param bytesize Size of each byte in the serial transmission of data, - * default is EIGHTBITS, possible values are: FIVEBITS, SIXBITS, SEVENBITS, + * \param bytesize Size of each byte in the serial transmission of data, + * default is EIGHTBITS, possible values are: FIVEBITS, SIXBITS, SEVENBITS, * EIGHTBITS * * \throw InvalidConfigurationException @@ -357,7 +356,7 @@ public: /*! Sets the parity for the serial port. * - * \param parity Method of parity, default is PARITY_NONE, possible values + * \param parity Method of parity, default is PARITY_NONE, possible values * are: PARITY_NONE, PARITY_ODD, PARITY_EVEN * * \throw InvalidConfigurationException @@ -376,7 +375,7 @@ public: /*! Sets the stopbits for the serial port. * - * \param stopbits Number of stop bits used, default is STOPBITS_ONE, + * \param stopbits Number of stop bits used, default is STOPBITS_ONE, * possible values are: STOPBITS_ONE, STOPBITS_ONE_POINT_FIVE, STOPBITS_TWO * * \throw InvalidConfigurationException @@ -395,8 +394,8 @@ public: /*! Sets the flow control for the serial port. * - * \param flowcontrol Type of flowcontrol used, default is FLOWCONTROL_NONE, - * possible values are: FLOWCONTROL_NONE, FLOWCONTROL_SOFTWARE, + * \param flowcontrol Type of flowcontrol used, default is FLOWCONTROL_NONE, + * possible values are: FLOWCONTROL_NONE, FLOWCONTROL_SOFTWARE, * FLOWCONTROL_HARDWARE * * \throw InvalidConfigurationException @@ -436,19 +435,6 @@ private: SerialImpl *pimpl; }; -// why not use std::invalid_argument? -// class InvalidConfigurationException : public std::exception { -// int bytesize; -// public: -// InvalidConfigurationException(int bytesize) {this->bytesize = bytesize;} -// -// virtual const char* what() const throw() { -// std::stringstream ss; -// ss << "Invalid configuration provided: " << this->bytesize; -// return ss.str().c_str(); -// } -// }; - } // namespace serial #endif diff --git a/include/serial/serial_listener.h b/include/serial/serial_listener.h index f0ce392..bc53244 100644 --- a/include/serial/serial_listener.h +++ b/include/serial/serial_listener.h @@ -731,6 +731,12 @@ private: // exact comparator function static bool _exactly (const std::string& token, std::string exact_str) { + std::cout << token << " == " << exact_str << ": "; + if (token == exact_str) + std::cout << "True"; + else + std::cout << "False"; + std::cout << std::endl; return token == exact_str; } // startswith comparator function @@ -748,7 +754,7 @@ private: _contains (const std::string& token, std::string substr) { return token.find(substr) != std::string::npos; } - + // Gets some data from the serial port void readSomeData (std::string&, size_t); // Runs the new tokens through the filters diff --git a/src/impl/unix.cc b/src/impl/unix.cc index 2fe2fe2..fd937fe 100644 --- a/src/impl/unix.cc +++ b/src/impl/unix.cc @@ -74,12 +74,12 @@ Serial::SerialImpl::reconfigurePort () { struct termios options; // The current options for the file descriptor struct termios originalTTYAttrs; // The orignal file descriptor options - uint8_t vmin = 0, vtime = 0; // timeout is done via select + uint8_t vmin = 0, vtime = 0; // timeout is done via select if (interCharTimeout_ == -1) { vmin = 1; vtime = uint8_t(interCharTimeout_ * 10); } - + if (tcgetattr(fd_, &originalTTYAttrs) == -1) { throw IOException("::tcgetattr"); } @@ -89,7 +89,7 @@ Serial::SerialImpl::reconfigurePort () { // set up raw mode / no echo / binary options.c_cflag |= (unsigned long)(CLOCAL|CREAD); options.c_lflag &= (unsigned long) ~(ICANON|ECHO|ECHOE|ECHOK - |ECHONL|ISIG|IEXTEN); //|ECHOPRT + |ECHONL|ISIG|IEXTEN); //|ECHOPRT options.c_oflag &= (unsigned long) ~(OPOST); options.c_iflag &= (unsigned long) ~(INLCR|IGNCR|ICRNL|IGNBRK); @@ -242,8 +242,8 @@ Serial::SerialImpl::read (size_t size) { // Disconnected devices, at least on Linux, show the // behavior that they are always ready to read immediately // but reading returns nothing. - throw SerialExecption("device reports readiness to read but returned no " - "data (device disconnected?)"); + throw SerialExecption("device reports readiness to read but " + "returned no data (device disconnected?)"); } message.append(buf, (size_t)bytes_read); } @@ -342,19 +342,20 @@ Serial::SerialImpl::getFlowcontrol () const { return flowcontrol_; } - void Serial::SerialImpl::flush () { if (isOpen_ == false) { throw PortNotOpenedException("Serial::flush"); } tcdrain(fd_); } + void Serial::SerialImpl::flushInput () { if (isOpen_ == false) { throw PortNotOpenedException("Serial::flushInput"); } tcflush(fd_, TCIFLUSH); } + void Serial::SerialImpl::flushOutput () { if (isOpen_ == false) { throw PortNotOpenedException("Serial::flushOutput"); @@ -368,6 +369,7 @@ void Serial::SerialImpl::sendBreak(int duration) { } tcsendbreak(fd_, int(duration/4)); } + void Serial::SerialImpl::setBreak(bool level) { if (isOpen_ == false) { throw PortNotOpenedException("Serial::setBreak"); @@ -379,6 +381,7 @@ void Serial::SerialImpl::setBreak(bool level) { ioctl(fd_, TIOCCBRK); } } + void Serial::SerialImpl::setRTS(bool level) { if (isOpen_ == false) { throw PortNotOpenedException("Serial::setRTS"); @@ -390,6 +393,7 @@ void Serial::SerialImpl::setRTS(bool level) { ioctl(fd_, TIOCMBIC, TIOCM_RTS); } } + void Serial::SerialImpl::setDTR(bool level) { if (isOpen_ == false) { throw PortNotOpenedException("Serial::setDTR"); @@ -400,8 +404,8 @@ void Serial::SerialImpl::setDTR(bool level) { else { ioctl(fd_, TIOCMBIC, TIOCM_DTR); } - } + bool Serial::SerialImpl::getCTS() { if (isOpen_ == false) { throw PortNotOpenedException("Serial::getCTS"); @@ -409,6 +413,7 @@ bool Serial::SerialImpl::getCTS() { int s = ioctl(fd_, TIOCMGET, 0); return (s & TIOCM_CTS) != 0; } + bool Serial::SerialImpl::getDSR() { if (isOpen_ == false) { throw PortNotOpenedException("Serial::getDSR"); @@ -416,6 +421,7 @@ bool Serial::SerialImpl::getDSR() { int s = ioctl(fd_, TIOCMGET, 0); return (s & TIOCM_DSR) != 0; } + bool Serial::SerialImpl::getRI() { if (isOpen_ == false) { throw PortNotOpenedException("Serial::getRI"); @@ -423,6 +429,7 @@ bool Serial::SerialImpl::getRI() { int s = ioctl(fd_, TIOCMGET, 0); return (s & TIOCM_RI) != 0; } + bool Serial::SerialImpl::getCD() { if (isOpen_ == false) { throw PortNotOpenedException("Serial::getCD"); diff --git a/src/serial.cc b/src/serial.cc index 1ff386a..9a5def8 100644 --- a/src/serial.cc +++ b/src/serial.cc @@ -40,6 +40,7 @@ void Serial::close () { this->pimpl->close (); } + bool Serial::isOpen () const { return this->pimpl->isOpen (); @@ -63,9 +64,9 @@ Serial::readline(size_t size, string eol) { string c = pimpl->read(1); if (!c.empty()) { line.append(c); - if (line.length() > leneol && line.substr(line.length() - leneol, leneol) == eol) { + if (line.length() > leneol + && line.substr(line.length() - leneol, leneol) == eol) break; - } if (line.length() >= size) { break; } @@ -75,7 +76,6 @@ Serial::readline(size_t size, string eol) { break; } } - return line; } @@ -98,7 +98,6 @@ Serial::readlines(string eol) { break; } } - return lines; } @@ -183,33 +182,43 @@ Serial::getFlowcontrol () const { void Serial::flush() { this->pimpl->flush(); } + void Serial::flushInput() { this->pimpl->flushInput(); } + void Serial::flushOutput() { this->pimpl->flushOutput(); } + void Serial::sendBreak(int duration) { this->pimpl->sendBreak(duration); } + void Serial::setBreak(bool level) { this->pimpl->setBreak(level); } + void Serial::setRTS(bool level) { this->pimpl->setRTS(level); } + void Serial::setDTR(bool level) { this->pimpl->setDTR(level); } + bool Serial::getCTS() { return this->pimpl->getCTS(); } + bool Serial::getDSR() { return this->pimpl->getDSR(); } + bool Serial::getRI() { return this->pimpl->getRI(); } + bool Serial::getCD() { return this->pimpl->getCD(); } diff --git a/src/serial_listener.cc b/src/serial_listener.cc index b4a392e..6da8d76 100644 --- a/src/serial_listener.cc +++ b/src/serial_listener.cc @@ -135,7 +135,6 @@ SerialListener::readSomeData(std::string &temp, size_t this_many) { this->handle_exc(SerialListenerException("Serial port not open.")); } temp = this->serial_port->read(this_many); - std::cout << "Read(" << temp.length() << "): " << temp << std::endl; } void @@ -146,15 +145,19 @@ SerialListener::filterNewTokens (std::vector new_tokens) { for (it=filters.begin(); it!=filters.end(); it++) { this->filter((*it), new_tokens); } // for (it=filters.begin(); it!=filters.end(); it++) + // Put the last token back in the data buffer + this->data_buffer = (*new_tokens.back()); } -// void SerialListener::filter (FilterPtr filter, std::vector &tokens) { // Iterate through the token uuids and run each against the filter std::vector::iterator it; for (it=tokens.begin(); it!=tokens.end(); it++) { + // The last element goes back into the data_buffer, don't filter it + if (it == tokens.end()-1) + continue; TokenPtr token = (*it); if (filter->comparator((*token))) callback_queue.push(std::make_pair(filter,token));