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:
parent
4cdb42987f
commit
0046f3f61f
@ -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_;
|
||||||
|
|||||||
@ -95,7 +95,7 @@ public:
|
|||||||
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;
|
||||||
@ -107,7 +107,7 @@ 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;
|
||||||
@ -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();
|
||||||
|
|
||||||
@ -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
|
||||||
|
|||||||
@ -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
|
||||||
|
|||||||
@ -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");
|
||||||
|
|||||||
@ -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();
|
||||||
}
|
}
|
||||||
|
|||||||
@ -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));
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user