serial_listener_example.cc
Go to the documentation of this file.
00001 #include <iostream>
00002 
00003 #include <serial/serial.h>
00004 #include <serial/utils/serial_listener.h>
00005 
00006 using namespace serial;
00007 using namespace serial::utils;
00008 
00009 void default_handler(std::string token) {
00010   std::cout << "default_handler got a: " << token << std::endl;
00011 }
00012 
00013 void callback(std::string token) {
00014   std::cout << "callback got a: " << token << std::endl;
00015 }
00016 
00017 int run() {
00018   // Assuming this device prints the string 'pre-substr-post\r' at 100Hz
00019   Serial serial("/dev/tty.usbserial-A900cfJA", 115200);
00020 
00021   SerialListener listener;
00022   listener.startListening(serial);
00023 
00024   // Set the tokenizer
00025   //  This is the same as the default delimeter, so an explicit call to
00026   //  setTokenizer is not necessary if your data is \r delimited.
00027   //  You can create your own Tokenizer as well.
00028   listener.setTokenizer(SerialListener::delimeter_tokenizer("\r"));
00029 
00030   // Method #1:
00031   //  comparator, callback - async
00032   FilterPtr f1 =
00033     listener.createFilter(SerialListener::startsWith("pre"), callback);
00034   SerialListener::sleep(15); // Sleep 15ms, to let the data come in
00035   listener.removeFilter(f1); // Not scoped, must be removed explicity
00036 
00037   // Method #2:
00038   //  comparator - blocking
00039   {
00040     BlockingFilterPtr f2 =
00041       listener.createBlockingFilter(SerialListener::endsWith("post"));
00042     for (size_t i = 0; i < 3; i++) {
00043       std::string token = f2->wait(100); // Wait for 100 ms or a matched token
00044       if (token != "")
00045         std::cout << "Found something ending with 'post'" << std::endl;
00046       else
00047         std::cout << "Did not find something ending with 'post'" << std::endl;
00048     }
00049   }
00050   // BlockingFilter is scoped and will remove itself, so no removeFilter
00051   // required, but a call like `listener.removeFilter(BlockingFilter) will
00052   // remove it from the filter list so wait will always timeout.
00053 
00054   // Method #3:
00055   //  comparator, token buffer size - blocking
00056   {
00057     // Give it a comparator, then a buffer size of 10
00058     BufferedFilterPtr f3 =
00059       listener.createBufferedFilter(SerialListener::contains("substr"), 10);
00060     SerialListener::sleep(75); // Sleep 75ms, should have about 7
00061     std::cout << "Caught " << f3->count();
00062     std::cout << " tokens containing 'substr'" << std::endl;
00063     for(size_t i = 0; i < 20; ++i) {
00064       std::string token = f3->wait(5); // Pull message from the buffer
00065       if (token == "") // If an empty string is returned, a timeout occured
00066         break;
00067     }
00068     f3->clear(); // Empties the buffer
00069     if (f3->wait(0) == "") // Non-blocking wait
00070       std::cout << "We won the race condition!" << std::endl;
00071     else
00072       std::cout << "We lost the race condition..." << std::endl;
00073     // The buffer is circular, so the oldest matches will be dropped first
00074   }
00075   // BufferedFilter is scoped and will remove itself just like BlockingFilter.
00076 
00077   // Method #4:
00078   //  callback - async
00079   // Gets called if a token doesn't match a filter
00080   listener.setDefaultHandler(default_handler);
00081   SerialListener::sleep(25); // Sleep 25 ms, so some default callbacks occur
00082 
00083   return 0;
00084 
00085 }
00086 
00087 int main(void) {
00088   try {
00089     return run();
00090   } catch (std::exception &e) {
00091     std::cerr << e.what() << std::endl;
00092     return 1;
00093   }
00094 }


serial_utils
Author(s): William Woodall , John Harrison
autogenerated on Thu Jun 6 2019 19:02:26