Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #include <string>
00020 #include <iostream>
00021 #include <cstdio>
00022
00023
00024 #ifdef _WIN32
00025 #include <windows.h>
00026 #else
00027 #include <unistd.h>
00028 #endif
00029
00030 #include "serial/serial.h"
00031
00032 using std::string;
00033 using std::exception;
00034 using std::cout;
00035 using std::cerr;
00036 using std::endl;
00037 using std::vector;
00038
00039 void my_sleep(unsigned long milliseconds) {
00040 #ifdef _WIN32
00041 Sleep(milliseconds);
00042 #else
00043 usleep(milliseconds*1000);
00044 #endif
00045 }
00046
00047 void enumerate_ports()
00048 {
00049 vector<serial::PortInfo> devices_found = serial::list_ports();
00050
00051 vector<serial::PortInfo>::iterator iter = devices_found.begin();
00052
00053 while( iter != devices_found.end() )
00054 {
00055 serial::PortInfo device = *iter++;
00056
00057 printf( "(%s, %s, %s)\n", device.port.c_str(), device.description.c_str(),
00058 device.hardware_id.c_str() );
00059 }
00060 }
00061
00062 void print_usage()
00063 {
00064 cerr << "Usage: test_serial {-e|<serial port address>} ";
00065 cerr << "<baudrate> [test string]" << endl;
00066 }
00067
00068 int run(int argc, char **argv)
00069 {
00070 if(argc < 2) {
00071 print_usage();
00072 return 0;
00073 }
00074
00075
00076 string port(argv[1]);
00077
00078 if( port == "-e" ) {
00079 enumerate_ports();
00080 return 0;
00081 }
00082 else if( argc < 3 ) {
00083 print_usage();
00084 return 1;
00085 }
00086
00087
00088 unsigned long baud = 0;
00089 #if defined(WIN32) && !defined(__MINGW32__)
00090 sscanf_s(argv[2], "%lu", &baud);
00091 #else
00092 sscanf(argv[2], "%lu", &baud);
00093 #endif
00094
00095
00096 serial::Serial my_serial(port, baud, serial::Timeout::simpleTimeout(1000));
00097
00098 cout << "Is the serial port open?";
00099 if(my_serial.isOpen())
00100 cout << " Yes." << endl;
00101 else
00102 cout << " No." << endl;
00103
00104
00105 int count = 0;
00106 string test_string;
00107 if (argc == 4) {
00108 test_string = argv[3];
00109 } else {
00110 test_string = "Testing.";
00111 }
00112
00113
00114 cout << "Timeout == 1000ms, asking for 1 more byte than written." << endl;
00115 while (count < 10) {
00116 size_t bytes_wrote = my_serial.write(test_string);
00117
00118 string result = my_serial.read(test_string.length()+1);
00119
00120 cout << "Iteration: " << count << ", Bytes written: ";
00121 cout << bytes_wrote << ", Bytes read: ";
00122 cout << result.length() << ", String read: " << result << endl;
00123
00124 count += 1;
00125 }
00126
00127
00128 my_serial.setTimeout(serial::Timeout::max(), 250, 0, 250, 0);
00129 count = 0;
00130 cout << "Timeout == 250ms, asking for 1 more byte than written." << endl;
00131 while (count < 10) {
00132 size_t bytes_wrote = my_serial.write(test_string);
00133
00134 string result = my_serial.read(test_string.length()+1);
00135
00136 cout << "Iteration: " << count << ", Bytes written: ";
00137 cout << bytes_wrote << ", Bytes read: ";
00138 cout << result.length() << ", String read: " << result << endl;
00139
00140 count += 1;
00141 }
00142
00143
00144 count = 0;
00145 cout << "Timeout == 250ms, asking for exactly what was written." << endl;
00146 while (count < 10) {
00147 size_t bytes_wrote = my_serial.write(test_string);
00148
00149 string result = my_serial.read(test_string.length());
00150
00151 cout << "Iteration: " << count << ", Bytes written: ";
00152 cout << bytes_wrote << ", Bytes read: ";
00153 cout << result.length() << ", String read: " << result << endl;
00154
00155 count += 1;
00156 }
00157
00158
00159 count = 0;
00160 cout << "Timeout == 250ms, asking for 1 less than was written." << endl;
00161 while (count < 10) {
00162 size_t bytes_wrote = my_serial.write(test_string);
00163
00164 string result = my_serial.read(test_string.length()-1);
00165
00166 cout << "Iteration: " << count << ", Bytes written: ";
00167 cout << bytes_wrote << ", Bytes read: ";
00168 cout << result.length() << ", String read: " << result << endl;
00169
00170 count += 1;
00171 }
00172
00173 return 0;
00174 }
00175
00176 int main(int argc, char **argv) {
00177 try {
00178 return run(argc, argv);
00179 } catch (exception &e) {
00180 cerr << "Unhandled Exception: " << e.what() << endl;
00181 }
00182 }