1
0
mirror of https://github.com/wjwwood/serial.git synced 2026-01-23 04:04:54 +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:
string port_; // Path to the file descriptor
int fd_; // The current file descriptor.
int fd_; // The current file descriptor
int interCharTimeout_;
int writeTimeout_;

View File

@ -95,7 +95,7 @@ public:
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;
@ -107,7 +107,7 @@ public:
class PortNotOpenedException : public std::exception {
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;
@ -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();
@ -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

View File

@ -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

View File

@ -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");

View File

@ -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();
}

View File

@ -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<TokenPtr> 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());
}
// <filter_ptr,token_ptr>
void
SerialListener::filter (FilterPtr filter, std::vector<TokenPtr> &tokens)
{
// Iterate through the token uuids and run each against the filter
std::vector<TokenPtr>::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));