mirror of
https://github.com/wjwwood/serial.git
synced 2026-01-23 04:04:54 +08:00
fixing empty token bug, and adding some debugging stuff.
This commit is contained in:
parent
0d464469cc
commit
f610fb79ff
@ -36,9 +36,14 @@
|
|||||||
#ifndef SERIAL_LISTENER_H
|
#ifndef SERIAL_LISTENER_H
|
||||||
#define SERIAL_LISTENER_H
|
#define SERIAL_LISTENER_H
|
||||||
|
|
||||||
|
#ifndef SERIAL_LISTENER_DEBUG
|
||||||
|
#define SERIAL_LISTENER_DEBUG 0
|
||||||
|
#endif
|
||||||
|
|
||||||
// STL
|
// STL
|
||||||
#include <queue>
|
#include <queue>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
// Serial
|
// Serial
|
||||||
#include <serial/serial.h>
|
#include <serial/serial.h>
|
||||||
@ -49,6 +54,10 @@
|
|||||||
#include <boost/date_time/posix_time/posix_time.hpp>
|
#include <boost/date_time/posix_time/posix_time.hpp>
|
||||||
#include <boost/thread.hpp>
|
#include <boost/thread.hpp>
|
||||||
|
|
||||||
|
#if SERIAL_LISTENER_DEBUG
|
||||||
|
# warning SerialListener in debug mode
|
||||||
|
#endif
|
||||||
|
|
||||||
namespace serial {
|
namespace serial {
|
||||||
|
|
||||||
/*!
|
/*!
|
||||||
@ -643,16 +652,46 @@ 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) {
|
||||||
|
#if SERIAL_LISTENER_DEBUG
|
||||||
|
std::cerr << "In exactly callback(" << token.length() << "): ";
|
||||||
|
std::cerr << token << " == " << exact_str << ": ";
|
||||||
|
if (token == exact_str)
|
||||||
|
std::cerr << "True";
|
||||||
|
else
|
||||||
|
std::cerr << "False";
|
||||||
|
std::cerr << std::endl;
|
||||||
|
#endif
|
||||||
return token == exact_str;
|
return token == exact_str;
|
||||||
}
|
}
|
||||||
// startswith comparator function
|
// startswith comparator function
|
||||||
static bool
|
static bool
|
||||||
_startsWith (const std::string& token, std::string prefix) {
|
_startsWith (const std::string& token, std::string prefix) {
|
||||||
|
#if SERIAL_LISTENER_DEBUG
|
||||||
|
std::cerr << "In startsWith callback(" << token.length() << "): ";
|
||||||
|
std::cerr << token << " starts with " << prefix;
|
||||||
|
std::cerr << "?: ";
|
||||||
|
if (token.substr(0,prefix.length()) == prefix)
|
||||||
|
std::cerr << "True";
|
||||||
|
else
|
||||||
|
std::cerr << "False";
|
||||||
|
std::cerr << std::endl;
|
||||||
|
#endif
|
||||||
return token.substr(0,prefix.length()) == prefix;
|
return token.substr(0,prefix.length()) == prefix;
|
||||||
}
|
}
|
||||||
// endswith comparator function
|
// endswith comparator function
|
||||||
static bool
|
static bool
|
||||||
_endsWith (const std::string& token, std::string postfix) {
|
_endsWith (const std::string& token, std::string postfix) {
|
||||||
|
#if SERIAL_LISTENER_DEBUG
|
||||||
|
std::cerr << "In endsWith callback(";
|
||||||
|
std::cerr << token.length();
|
||||||
|
std::cerr << "): " << token;
|
||||||
|
std::cerr << " ends with " << postfix << "?: ";
|
||||||
|
if (token.substr(token.length()-postfix.length()) == postfix)
|
||||||
|
std::cerr << "True";
|
||||||
|
else
|
||||||
|
std::cerr << "False";
|
||||||
|
std::cerr << std::endl;
|
||||||
|
#endif
|
||||||
return token.substr(token.length()-postfix.length()) == postfix;
|
return token.substr(token.length()-postfix.length()) == postfix;
|
||||||
}
|
}
|
||||||
// contains comparator function
|
// contains comparator function
|
||||||
@ -662,7 +701,17 @@ private:
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Gets some data from the serial port
|
// Gets some data from the serial port
|
||||||
void readSomeData (std::string&, size_t);
|
void readSomeData (std::string &temp, size_t this_many) {
|
||||||
|
// Make sure there is a serial port
|
||||||
|
if (this->serial_port_ == NULL) {
|
||||||
|
this->handle_exc(SerialListenerException("Invalid serial port."));
|
||||||
|
}
|
||||||
|
// Make sure the serial port is open
|
||||||
|
if (!this->serial_port_->isOpen()) {
|
||||||
|
this->handle_exc(SerialListenerException("Serial port not open."));
|
||||||
|
}
|
||||||
|
temp = this->serial_port_->read(this_many);
|
||||||
|
}
|
||||||
// Runs the new_tokens through all the filters
|
// Runs the new_tokens through all the filters
|
||||||
void filter (std::vector<TokenPtr> &tokens);
|
void filter (std::vector<TokenPtr> &tokens);
|
||||||
// Function that loops while listening is true
|
// Function that loops while listening is true
|
||||||
@ -750,6 +799,10 @@ public:
|
|||||||
FilterPtr filter_ptr;
|
FilterPtr filter_ptr;
|
||||||
|
|
||||||
void callback(const std::string& token) {
|
void callback(const std::string& token) {
|
||||||
|
#if SERIAL_LISTENER_DEBUG
|
||||||
|
std::cerr << "In BlockingFilter callback(" << token.length() << "): ";
|
||||||
|
std::cerr << token << std::endl;
|
||||||
|
#endif
|
||||||
this->cond.notify_all();
|
this->cond.notify_all();
|
||||||
this->result = token;
|
this->result = token;
|
||||||
}
|
}
|
||||||
@ -844,6 +897,10 @@ public:
|
|||||||
FilterPtr filter_ptr;
|
FilterPtr filter_ptr;
|
||||||
|
|
||||||
void callback(const std::string &token) {
|
void callback(const std::string &token) {
|
||||||
|
#if SERIAL_LISTENER_DEBUG
|
||||||
|
std::cerr << "In BufferedFilter callback(" << token.length() << "): ";
|
||||||
|
std::cerr << token << std::endl;
|
||||||
|
#endif
|
||||||
std::string throw_away;
|
std::string throw_away;
|
||||||
if (this->queue.size() == this->buffer_size_) {
|
if (this->queue.size() == this->buffer_size_) {
|
||||||
this->queue.wait_and_pop(throw_away);
|
this->queue.wait_and_pop(throw_away);
|
||||||
|
|||||||
@ -105,19 +105,6 @@ SerialListener::determineAmountToRead() {
|
|||||||
return this->chunk_size_;
|
return this->chunk_size_;
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
|
||||||
SerialListener::readSomeData(std::string &temp, size_t this_many) {
|
|
||||||
// Make sure there is a serial port
|
|
||||||
if (this->serial_port_ == NULL) {
|
|
||||||
this->handle_exc(SerialListenerException("Invalid serial port."));
|
|
||||||
}
|
|
||||||
// Make sure the serial port is open
|
|
||||||
if (!this->serial_port_->isOpen()) {
|
|
||||||
this->handle_exc(SerialListenerException("Serial port not open."));
|
|
||||||
}
|
|
||||||
temp = this->serial_port_->read(this_many);
|
|
||||||
}
|
|
||||||
|
|
||||||
void
|
void
|
||||||
SerialListener::filter(std::vector<TokenPtr> &tokens) {
|
SerialListener::filter(std::vector<TokenPtr> &tokens) {
|
||||||
// Lock the filters while filtering
|
// Lock the filters while filtering
|
||||||
@ -126,6 +113,10 @@ SerialListener::filter(std::vector<TokenPtr> &tokens) {
|
|||||||
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++) {
|
||||||
TokenPtr token = (*it);
|
TokenPtr token = (*it);
|
||||||
|
// If it is empty then pass it
|
||||||
|
if (token->empty()) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
bool matched = false;
|
bool matched = false;
|
||||||
// Iterate through each filter
|
// Iterate through each filter
|
||||||
std::vector<FilterPtr>::iterator itt;
|
std::vector<FilterPtr>::iterator itt;
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user