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