Skip to content

Instantly share code, notes, and snippets.

@wjwwood
Created January 12, 2012 02:56
Show Gist options
  • Select an option

  • Save wjwwood/1598274 to your computer and use it in GitHub Desktop.

Select an option

Save wjwwood/1598274 to your computer and use it in GitHub Desktop.
serial_listener_example.cc
#include <iostream>
#include <serial/serial.h>
#include <serial/serial_listener.h>
using namespace serial;
void default_handler(std::string token) {
std::cout << "default_handler got a: " << token << std::endl;
}
void callback(std::string token) {
std::cout << "callback got a: " << token << std::endl;
}
int main(void) {
// Assuming this device prints the string 'pre-substr-post\r' at 100Hz
Serial serial("/dev/tty.usbmodemfd1231", 115200);
SerialListener listener;
listener.startListening(serial);
// Set the tokenizer
// This is the same as the default delimeter, so an explicit call to
// setTokenizer is not necessary if your data is \r delimited.
// You can create your own Tokenizer as well.
listener.setTokenizer(SerialListener::delimeter_tokenizer("\r"));
// Method #1:
// comparator, callback - async
FilterPtr f1 =
listener.createFilter(SerialListener::startsWith("pre"), callback);
SerialListener::sleep(15); // Sleep 15ms, to let the data come in
listener.removeFilter(f1); // Not scoped, must be removed explicity
// Method #2:
// comparator - blocking
{
BlockingFilter f2 =
listener.createFilter(SerialListener::endsWith("post"));
for (size_t i = 0; i < 3; i++) {
std::string token = f2.wait(100); // Wait for 100 ms or a matched token
if (token == "")
std::cout << "Did not find something ending in 'post'" << std::endl;
}
}
// BlockingFilter is scoped and will remove itself, so no removeFilter
// required, but a call like `listener.removeFilter(BlockingFilter) will
// remove it from the filter list so wait will always timeout.
// Method #3:
// comparator, token buffer size - blocking
{
// Give it a comparator, then a buffer size of 10
BufferedBlockingFilter f3 =
listener.createFilter(SerialListener::contains("substr"), 10);
SerialListener::sleep(75); // Sleep 75ms, should have about 7
std::cout << "Caught " << f3.count();
std::cout << " tokens containing 'substr'" << std::endl;
for(size_t i = 0; i < 20; ++i) {
std::string token = f3.wait(5); // Pull message from the buffer
if (token == "") // If an empty string is returned, a timeout occured
break;
}
f3.clear(); // Empties the buffer
if (f3.wait(0) == "") // Non-blocking wait
std::cout << "We won the race condition!" << std::endl;
// The buffer is circular, so the oldest matches will be dropped first
}
// BufferedBlockingFilter is scoped and will remove itself just like
// BlockingFilter.
// Method #4:
// callback - async
// Gets called if a token doesn't match a filter
listener.setDefaultHandler(default_handler);
SerialListener::sleep(25); // Sleep 25 ms, so some default callbacks occur
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment