1
0
mirror of https://github.com/wjwwood/serial.git synced 2026-01-22 19:54:57 +08:00

Cleanup of code base

This commit is contained in:
William Woodall 2012-01-13 09:08:09 -06:00
parent 4cdb42987f
commit 0046f3f61f
6 changed files with 61 additions and 50 deletions

View File

@ -104,7 +104,7 @@ protected:
private: private:
string port_; // Path to the file descriptor string port_; // Path to the file descriptor
int fd_; // The current file descriptor. int fd_; // The current file descriptor
int interCharTimeout_; int interCharTimeout_;
int writeTimeout_; int writeTimeout_;
@ -118,7 +118,7 @@ private:
unsigned long baudrate_; // Baudrate unsigned long baudrate_; // Baudrate
parity_t parity_; // Parity parity_t parity_; // Parity
bytesize_t bytesize_; // Size of the bytes bytesize_t bytesize_; // Size of the bytes
stopbits_t stopbits_; // Stop Bits stopbits_t stopbits_; // Stop Bits
flowcontrol_t flowcontrol_; // Flow Control flowcontrol_t flowcontrol_; // Flow Control
}; };

View File

@ -82,21 +82,21 @@ typedef enum {
} flowcontrol_t; } flowcontrol_t;
class SerialExecption : public std::exception { class SerialExecption : public std::exception {
const char * e_what; const char * e_what;
public: public:
SerialExecption(const char *description) {e_what=description;}; SerialExecption(const char *description) {e_what=description;};
virtual const char* what() const throw() { virtual const char* what() const throw() {
std::stringstream ss; std::stringstream ss;
ss << "SerialException " << this->e_what << " failed."; ss << "SerialException " << this->e_what << " failed.";
return ss.str().c_str(); return ss.str().c_str();
} }
}; };
class IOException : public std::exception { class IOException : public std::exception {
const char * e_what; const char * e_what;
public: public:
IOException(const char * description) {this->e_what = description;} IOException(const char * description) {e_what = description;}
virtual const char* what() const throw() { virtual const char* what() const throw() {
std::stringstream ss; std::stringstream ss;
ss << "IO Exception " << this->e_what << " failed."; ss << "IO Exception " << this->e_what << " failed.";
@ -105,13 +105,13 @@ public:
}; };
class PortNotOpenedException : public std::exception { class PortNotOpenedException : public std::exception {
const char * e_what; const char * e_what;
public: public:
PortNotOpenedException(const char * description) {this->e_what = description;} PortNotOpenedException(const char * description) {e_what = description;}
virtual const char* what() const throw() { virtual const char* what() const throw() {
std::stringstream ss; std::stringstream ss;
ss << e_what << " called before port was opened."; ss << e_what << " called before port was opened.";
return ss.str().c_str(); return ss.str().c_str();
} }
}; };
@ -193,8 +193,7 @@ public:
void void
close (); close ();
/* Return the number of characters in the buffer. /*! Return the number of characters in the buffer. */
*/
size_t size_t
available(); available();
@ -295,8 +294,8 @@ public:
* timeout on reads occur. Setting this to zero (0) will cause reading to be * 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 * 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 * 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 * 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 * will block until either size bytes have been read or an exception has
* occured. * occured.
*/ */
void void
@ -337,8 +336,8 @@ public:
/*! Sets the bytesize for the serial port. /*! Sets the bytesize for the serial port.
* *
* \param bytesize Size of each byte in the serial transmission of data, * \param bytesize Size of each byte in the serial transmission of data,
* default is EIGHTBITS, possible values are: FIVEBITS, SIXBITS, SEVENBITS, * default is EIGHTBITS, possible values are: FIVEBITS, SIXBITS, SEVENBITS,
* EIGHTBITS * EIGHTBITS
* *
* \throw InvalidConfigurationException * \throw InvalidConfigurationException
@ -357,7 +356,7 @@ public:
/*! Sets the parity for the serial port. /*! 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 * are: PARITY_NONE, PARITY_ODD, PARITY_EVEN
* *
* \throw InvalidConfigurationException * \throw InvalidConfigurationException
@ -376,7 +375,7 @@ public:
/*! Sets the stopbits for the serial port. /*! 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 * possible values are: STOPBITS_ONE, STOPBITS_ONE_POINT_FIVE, STOPBITS_TWO
* *
* \throw InvalidConfigurationException * \throw InvalidConfigurationException
@ -395,8 +394,8 @@ public:
/*! Sets the flow control for the serial port. /*! Sets the flow control for the serial port.
* *
* \param flowcontrol Type of flowcontrol used, default is FLOWCONTROL_NONE, * \param flowcontrol Type of flowcontrol used, default is FLOWCONTROL_NONE,
* possible values are: FLOWCONTROL_NONE, FLOWCONTROL_SOFTWARE, * possible values are: FLOWCONTROL_NONE, FLOWCONTROL_SOFTWARE,
* FLOWCONTROL_HARDWARE * FLOWCONTROL_HARDWARE
* *
* \throw InvalidConfigurationException * \throw InvalidConfigurationException
@ -436,19 +435,6 @@ private:
SerialImpl *pimpl; 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 } // namespace serial
#endif #endif

View File

@ -731,6 +731,12 @@ private:
// exact comparator function // exact comparator function
static bool static bool
_exactly (const std::string& token, std::string exact_str) { _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; return token == exact_str;
} }
// startswith comparator function // startswith comparator function
@ -748,7 +754,7 @@ private:
_contains (const std::string& token, std::string substr) { _contains (const std::string& token, std::string substr) {
return token.find(substr) != std::string::npos; return token.find(substr) != std::string::npos;
} }
// Gets some data from the serial port // Gets some data from the serial port
void readSomeData (std::string&, size_t); void readSomeData (std::string&, size_t);
// Runs the new tokens through the filters // Runs the new tokens through the filters

View File

@ -74,12 +74,12 @@ Serial::SerialImpl::reconfigurePort () {
struct termios options; // The current options for the file descriptor struct termios options; // The current options for the file descriptor
struct termios originalTTYAttrs; // The orignal file descriptor options 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) { if (interCharTimeout_ == -1) {
vmin = 1; vmin = 1;
vtime = uint8_t(interCharTimeout_ * 10); vtime = uint8_t(interCharTimeout_ * 10);
} }
if (tcgetattr(fd_, &originalTTYAttrs) == -1) { if (tcgetattr(fd_, &originalTTYAttrs) == -1) {
throw IOException("::tcgetattr"); throw IOException("::tcgetattr");
} }
@ -89,7 +89,7 @@ Serial::SerialImpl::reconfigurePort () {
// set up raw mode / no echo / binary // set up raw mode / no echo / binary
options.c_cflag |= (unsigned long)(CLOCAL|CREAD); options.c_cflag |= (unsigned long)(CLOCAL|CREAD);
options.c_lflag &= (unsigned long) ~(ICANON|ECHO|ECHOE|ECHOK 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_oflag &= (unsigned long) ~(OPOST);
options.c_iflag &= (unsigned long) ~(INLCR|IGNCR|ICRNL|IGNBRK); 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 // Disconnected devices, at least on Linux, show the
// behavior that they are always ready to read immediately // behavior that they are always ready to read immediately
// but reading returns nothing. // but reading returns nothing.
throw SerialExecption("device reports readiness to read but returned no " throw SerialExecption("device reports readiness to read but "
"data (device disconnected?)"); "returned no data (device disconnected?)");
} }
message.append(buf, (size_t)bytes_read); message.append(buf, (size_t)bytes_read);
} }
@ -342,19 +342,20 @@ Serial::SerialImpl::getFlowcontrol () const {
return flowcontrol_; return flowcontrol_;
} }
void Serial::SerialImpl::flush () { void Serial::SerialImpl::flush () {
if (isOpen_ == false) { if (isOpen_ == false) {
throw PortNotOpenedException("Serial::flush"); throw PortNotOpenedException("Serial::flush");
} }
tcdrain(fd_); tcdrain(fd_);
} }
void Serial::SerialImpl::flushInput () { void Serial::SerialImpl::flushInput () {
if (isOpen_ == false) { if (isOpen_ == false) {
throw PortNotOpenedException("Serial::flushInput"); throw PortNotOpenedException("Serial::flushInput");
} }
tcflush(fd_, TCIFLUSH); tcflush(fd_, TCIFLUSH);
} }
void Serial::SerialImpl::flushOutput () { void Serial::SerialImpl::flushOutput () {
if (isOpen_ == false) { if (isOpen_ == false) {
throw PortNotOpenedException("Serial::flushOutput"); throw PortNotOpenedException("Serial::flushOutput");
@ -368,6 +369,7 @@ void Serial::SerialImpl::sendBreak(int duration) {
} }
tcsendbreak(fd_, int(duration/4)); tcsendbreak(fd_, int(duration/4));
} }
void Serial::SerialImpl::setBreak(bool level) { void Serial::SerialImpl::setBreak(bool level) {
if (isOpen_ == false) { if (isOpen_ == false) {
throw PortNotOpenedException("Serial::setBreak"); throw PortNotOpenedException("Serial::setBreak");
@ -379,6 +381,7 @@ void Serial::SerialImpl::setBreak(bool level) {
ioctl(fd_, TIOCCBRK); ioctl(fd_, TIOCCBRK);
} }
} }
void Serial::SerialImpl::setRTS(bool level) { void Serial::SerialImpl::setRTS(bool level) {
if (isOpen_ == false) { if (isOpen_ == false) {
throw PortNotOpenedException("Serial::setRTS"); throw PortNotOpenedException("Serial::setRTS");
@ -390,6 +393,7 @@ void Serial::SerialImpl::setRTS(bool level) {
ioctl(fd_, TIOCMBIC, TIOCM_RTS); ioctl(fd_, TIOCMBIC, TIOCM_RTS);
} }
} }
void Serial::SerialImpl::setDTR(bool level) { void Serial::SerialImpl::setDTR(bool level) {
if (isOpen_ == false) { if (isOpen_ == false) {
throw PortNotOpenedException("Serial::setDTR"); throw PortNotOpenedException("Serial::setDTR");
@ -400,8 +404,8 @@ void Serial::SerialImpl::setDTR(bool level) {
else { else {
ioctl(fd_, TIOCMBIC, TIOCM_DTR); ioctl(fd_, TIOCMBIC, TIOCM_DTR);
} }
} }
bool Serial::SerialImpl::getCTS() { bool Serial::SerialImpl::getCTS() {
if (isOpen_ == false) { if (isOpen_ == false) {
throw PortNotOpenedException("Serial::getCTS"); throw PortNotOpenedException("Serial::getCTS");
@ -409,6 +413,7 @@ bool Serial::SerialImpl::getCTS() {
int s = ioctl(fd_, TIOCMGET, 0); int s = ioctl(fd_, TIOCMGET, 0);
return (s & TIOCM_CTS) != 0; return (s & TIOCM_CTS) != 0;
} }
bool Serial::SerialImpl::getDSR() { bool Serial::SerialImpl::getDSR() {
if (isOpen_ == false) { if (isOpen_ == false) {
throw PortNotOpenedException("Serial::getDSR"); throw PortNotOpenedException("Serial::getDSR");
@ -416,6 +421,7 @@ bool Serial::SerialImpl::getDSR() {
int s = ioctl(fd_, TIOCMGET, 0); int s = ioctl(fd_, TIOCMGET, 0);
return (s & TIOCM_DSR) != 0; return (s & TIOCM_DSR) != 0;
} }
bool Serial::SerialImpl::getRI() { bool Serial::SerialImpl::getRI() {
if (isOpen_ == false) { if (isOpen_ == false) {
throw PortNotOpenedException("Serial::getRI"); throw PortNotOpenedException("Serial::getRI");
@ -423,6 +429,7 @@ bool Serial::SerialImpl::getRI() {
int s = ioctl(fd_, TIOCMGET, 0); int s = ioctl(fd_, TIOCMGET, 0);
return (s & TIOCM_RI) != 0; return (s & TIOCM_RI) != 0;
} }
bool Serial::SerialImpl::getCD() { bool Serial::SerialImpl::getCD() {
if (isOpen_ == false) { if (isOpen_ == false) {
throw PortNotOpenedException("Serial::getCD"); throw PortNotOpenedException("Serial::getCD");

View File

@ -40,6 +40,7 @@ void
Serial::close () { Serial::close () {
this->pimpl->close (); this->pimpl->close ();
} }
bool bool
Serial::isOpen () const { Serial::isOpen () const {
return this->pimpl->isOpen (); return this->pimpl->isOpen ();
@ -63,9 +64,9 @@ Serial::readline(size_t size, string eol) {
string c = pimpl->read(1); string c = pimpl->read(1);
if (!c.empty()) { if (!c.empty()) {
line.append(c); 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; break;
}
if (line.length() >= size) { if (line.length() >= size) {
break; break;
} }
@ -75,7 +76,6 @@ Serial::readline(size_t size, string eol) {
break; break;
} }
} }
return line; return line;
} }
@ -98,7 +98,6 @@ Serial::readlines(string eol) {
break; break;
} }
} }
return lines; return lines;
} }
@ -183,33 +182,43 @@ Serial::getFlowcontrol () const {
void Serial::flush() { void Serial::flush() {
this->pimpl->flush(); this->pimpl->flush();
} }
void Serial::flushInput() { void Serial::flushInput() {
this->pimpl->flushInput(); this->pimpl->flushInput();
} }
void Serial::flushOutput() { void Serial::flushOutput() {
this->pimpl->flushOutput(); this->pimpl->flushOutput();
} }
void Serial::sendBreak(int duration) { void Serial::sendBreak(int duration) {
this->pimpl->sendBreak(duration); this->pimpl->sendBreak(duration);
} }
void Serial::setBreak(bool level) { void Serial::setBreak(bool level) {
this->pimpl->setBreak(level); this->pimpl->setBreak(level);
} }
void Serial::setRTS(bool level) { void Serial::setRTS(bool level) {
this->pimpl->setRTS(level); this->pimpl->setRTS(level);
} }
void Serial::setDTR(bool level) { void Serial::setDTR(bool level) {
this->pimpl->setDTR(level); this->pimpl->setDTR(level);
} }
bool Serial::getCTS() { bool Serial::getCTS() {
return this->pimpl->getCTS(); return this->pimpl->getCTS();
} }
bool Serial::getDSR() { bool Serial::getDSR() {
return this->pimpl->getDSR(); return this->pimpl->getDSR();
} }
bool Serial::getRI() { bool Serial::getRI() {
return this->pimpl->getRI(); return this->pimpl->getRI();
} }
bool Serial::getCD() { bool Serial::getCD() {
return this->pimpl->getCD(); return this->pimpl->getCD();
} }

View File

@ -135,7 +135,6 @@ SerialListener::readSomeData(std::string &temp, size_t this_many) {
this->handle_exc(SerialListenerException("Serial port not open.")); this->handle_exc(SerialListenerException("Serial port not open."));
} }
temp = this->serial_port->read(this_many); temp = this->serial_port->read(this_many);
std::cout << "Read(" << temp.length() << "): " << temp << std::endl;
} }
void void
@ -146,15 +145,19 @@ SerialListener::filterNewTokens (std::vector<TokenPtr> new_tokens) {
for (it=filters.begin(); it!=filters.end(); it++) { for (it=filters.begin(); it!=filters.end(); it++) {
this->filter((*it), new_tokens); this->filter((*it), new_tokens);
} // for (it=filters.begin(); it!=filters.end(); it++) } // for (it=filters.begin(); it!=filters.end(); it++)
// Put the last token back in the data buffer
this->data_buffer = (*new_tokens.back());
} }
// <filter_ptr,token_ptr>
void void
SerialListener::filter (FilterPtr filter, std::vector<TokenPtr> &tokens) SerialListener::filter (FilterPtr filter, std::vector<TokenPtr> &tokens)
{ {
// Iterate through the token uuids and run each against the filter // Iterate through the token uuids and run each against the filter
std::vector<TokenPtr>::iterator it; std::vector<TokenPtr>::iterator it;
for (it=tokens.begin(); it!=tokens.end(); 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); TokenPtr token = (*it);
if (filter->comparator((*token))) if (filter->comparator((*token)))
callback_queue.push(std::make_pair(filter,token)); callback_queue.push(std::make_pair(filter,token));