serial_timeouts.cpp
Go to the documentation of this file.
00001 
00010 /*****************************************************************************
00011 ** Platform Check
00012 *****************************************************************************/
00013 
00014 #include <ecl/config.hpp>
00015 
00016 /*****************************************************************************
00017 ** Includes
00018 *****************************************************************************/
00019 
00020 #include <iostream>
00021 #include <string>
00022 #include <ecl/time/timestamp.hpp>
00023 #include "../../include/ecl/devices/serial.hpp"
00024 
00025 /*****************************************************************************
00026 ** Namespaces
00027 *****************************************************************************/
00028 
00029 using namespace ecl;
00030 
00031 /*****************************************************************************
00032 ** Functions
00033 *****************************************************************************/
00034 
00035 void print_usage() {
00036         std::cout << "Usage: demo_serial_timeouts [port]" << std::endl;
00037         std::cout << std::endl;
00038         std::cout << "  default value for port is /dev/ttyUSB0" << std::endl;
00039 }
00040 
00041 /*****************************************************************************
00042 ** Main
00043 *****************************************************************************/
00044 
00045 int main(int argc, char **argv) {
00046 
00047     std::string port("/dev/ttyUSB0");
00048     if ( argc > 1 ) {
00049         std::string arg(argv[1]);
00050         if ( arg == "--help" ) {
00051                 print_usage();
00052                 return 0;
00053         } else {
00054                 port = argv[1];
00055         }
00056     }
00057     std::cout << std::endl;
00058     std::cout << "***********************************************************" << std::endl;
00059     std::cout << "               Serial Timeouts" << std::endl;
00060     std::cout << "***********************************************************" << std::endl;
00061         std::cout << "* For demo'ing low latency read timeouts on posix systems." << std::endl;
00062         std::cout << "* Timeouts < 100ms use a custom loop, > 100ms use termios." << std::endl;
00063         std::cout << "* Hook this up to a serial cable to test (actual" << std::endl;
00064         std::cout << "* connection not important)." << std::endl;
00065     std::cout << "***********************************************************" << std::endl;
00066     std::cout << std::endl;
00067 
00068     Serial serial;
00069     try {
00070         serial.open(port,BaudRate_115200,DataBits_8,StopBits_1,NoParity);
00071     } catch (StandardException &e ) {
00072         std::cout << "[ERROR] : error opening " << port << std::endl;
00073         std::cout << std::endl;
00074         print_usage();
00075         return 0;
00076     }
00077     TimeStamp time, pre_read_time;
00078     char buffer[256];
00079 
00080     std::cout << std::endl;
00081     std::cout << "***********************************************************" << std::endl;
00082     std::cout << "                  100 ms timeouts" << std::endl;
00083     std::cout << "***********************************************************" << std::endl;
00084     std::cout << "* This will use termios to scan." << std::endl;
00085     std::cout << "***********************************************************" << std::endl;
00086     std::cout << std::endl;
00087 
00088     serial.block(100);
00089     for ( unsigned int i = 0; i < 10; ++i ) {
00090         pre_read_time.stamp();
00091         long result = serial.read(buffer,256);
00092         time.stamp();
00093         if ( result > 0 ) {
00094                 std::cout << "[INFO] : read " << result << " bytes." << std::endl;
00095         } else if ( result == 0 ) {
00096                 std::cout << "[INFO] : timed out [" << (time - pre_read_time) << "]." << std::endl;
00097         } else {
00098                 std::cout << "[INFO] : error " << result << "." << std::endl;
00099         }
00100     }
00101 
00102     std::cout << std::endl;
00103     std::cout << "***********************************************************" << std::endl;
00104     std::cout << "                  50 ms timeouts" << std::endl;
00105     std::cout << "***********************************************************" << std::endl;
00106     std::cout << "* This will internally scan with 5ms loops." << std::endl;
00107     std::cout << "***********************************************************" << std::endl;
00108     std::cout << std::endl;
00109 
00110     serial.block(50);
00111     // uncomment this to test reading without a timeout on a loopback connection.
00112     // buffer[0] = 'a';
00113     // buffer[1] = 'b';
00114     // serial.write(buffer,2);
00115     for ( unsigned int i = 0; i < 10; ++i ) {
00116         pre_read_time.stamp();
00117         long result = serial.read(buffer,256);
00118         time.stamp();
00119         if ( result > 0 ) {
00120                 std::cout << "[INFO] : read " << result << " bytes." << std::endl;
00121         } else if ( result == 0 ) {
00122                 std::cout << "[INFO] : timed out [" << (time - pre_read_time) << "]." << std::endl;
00123         } else {
00124                 std::cout << "[INFO] : error " << result << "." << std::endl;
00125         }
00126     }
00127     std::cout << std::endl;
00128     std::cout << "***********************************************************" << std::endl;
00129     std::cout << "                  20 ms timeouts" << std::endl;
00130     std::cout << "***********************************************************" << std::endl;
00131     std::cout << "* This will internally scan with 2ms loops." << std::endl;
00132     std::cout << "***********************************************************" << std::endl;
00133     std::cout << std::endl;
00134 
00135     serial.block(20);
00136     for ( unsigned int i = 0; i < 10; ++i ) {
00137         pre_read_time.stamp();
00138         long result = serial.read(buffer,256);
00139         time.stamp();
00140         if ( result > 0 ) {
00141                 std::cout << "[INFO] : read " << result << " bytes." << std::endl;
00142         } else if ( result == 0 ) {
00143                 std::cout << "[INFO] : timed out [" << (time - pre_read_time) << "]." << std::endl;
00144         } else {
00145                 std::cout << "[INFO] : error " << result << "." << std::endl;
00146         }
00147     }
00148     std::cout << std::endl;
00149     std::cout << "***********************************************************" << std::endl;
00150     std::cout << "                  5 ms timeouts" << std::endl;
00151     std::cout << "***********************************************************" << std::endl;
00152     std::cout << "* This will internally scan with 1ms loops." << std::endl;
00153     std::cout << "***********************************************************" << std::endl;
00154     std::cout << std::endl;
00155 
00156     serial.block(5);
00157     for ( unsigned int i = 0; i < 10; ++i ) {
00158         pre_read_time.stamp();
00159         long result = serial.read(buffer,256);
00160         time.stamp();
00161         if ( result > 0 ) {
00162                 std::cout << "[INFO] : read " << result << " bytes." << std::endl;
00163         } else if ( result == 0 ) {
00164                 std::cout << "[INFO] : timed out [" << (time - pre_read_time) << "]." << std::endl;
00165         } else {
00166                 std::cout << "[INFO] : error " << result << "." << std::endl;
00167         }
00168     }
00169     return 0;
00170 }


ecl_devices
Author(s): Daniel Stonier
autogenerated on Thu Jun 6 2019 21:18:03