serial_example.cc
Go to the documentation of this file.
00001 /***
00002  * This example expects the serial port has a loopback on it.
00003  *
00004  * Alternatively, you could use an Arduino:
00005  *
00006  * <pre>
00007  *  void setup() {
00008  *    Serial.begin(<insert your baudrate here>);
00009  *  }
00010  *
00011  *  void loop() {
00012  *    if (Serial.available()) {
00013  *      Serial.write(Serial.read());
00014  *    }
00015  *  }
00016  * </pre>
00017  */
00018 
00019 #include <string>
00020 #include <iostream>
00021 #include <cstdio>
00022 
00023 // OS Specific sleep
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); // 100 ms
00042 #else
00043       usleep(milliseconds*1000); // 100 ms
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   // Argument 1 is the serial port or enumerate flag
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   // Argument 2 is the baudrate
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   // port, baudrate, timeout in milliseconds
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   // Get the Test string
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   // Test the timeout, there should be 1 second between prints
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   // Test the timeout at 250ms
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   // Test the timeout at 250ms, but asking exactly for what was written
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   // Test the timeout at 250ms, but asking for 1 less than what was written
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 }


serial
Author(s): William Woodall , John Harrison
autogenerated on Thu Mar 28 2019 03:29:52