hex.cpp
Go to the documentation of this file.
00001 
00009 /*****************************************************************************
00010 ** Includes
00011 *****************************************************************************/
00012 
00013 #include <iostream>
00014 #include <string>
00015 #include <vector>
00016 #include <ecl/command_line.hpp>
00017 #include <ecl/converters.hpp>
00018 #include <ecl/errors.hpp>
00019 #include <ecl/ipc.hpp>
00020 #include <ecl/time.hpp>
00021 #include <ecl/threads.hpp>
00022 #include <ecl/devices/serial.hpp>
00023 #include <ecl/formatters.hpp>
00024 
00025 
00026 /*****************************************************************************
00027 ** Namespaces
00028 *****************************************************************************/
00029 
00030 namespace ecl {
00031 namespace utils {
00032 
00033 /*****************************************************************************
00034  * Using
00035  ****************************************************************************/
00036 
00037 using std::string;
00038 using std::vector;
00039 using ecl::ArgException;
00040 using ecl::CmdLine;
00041 using ecl::SwitchArg;
00042 using ecl::ValueArg;
00043 using ecl::Serial;
00044 using ecl::BaudRate;
00045 using ecl::BaudRate_9600;
00046 using ecl::BaudRate_38400;
00047 using ecl::BaudRate_115200;
00048 using ecl::DataBits_8;
00049 using ecl::StopBits_1;
00050 using ecl::NoParity;
00051 using ecl::StandardException;
00052 using ecl::Format;
00053 using ecl::Dec;
00054 using ecl::Hex;
00055 using ecl::NoAlign;
00056 using ecl::RightAlign;
00057 using ecl::Thread;
00058 using ecl::TimeStamp;
00059 using ecl::MilliSleep;
00060 using ecl::Converter;
00061 
00062 /*****************************************************************************
00063  * Classes
00064  ****************************************************************************/
00065 
00066 class Writer
00067 {
00068 public:
00069         Writer(Serial *serial_device, bool timestamps = false) :
00070                 display_timestamps(timestamps),
00071                 serial(serial_device),
00072                 hex_format(-1,NoAlign,Hex),
00073                 thread(&Writer::run,*this)
00074                 {}
00075         void wait() { thread.join(); }
00076 
00077 private:
00078         bool display_timestamps;
00079         long current_time;
00080         TimeStamp timestamp;
00081         Serial *serial;
00082         Format<char> hex_format;
00083         Thread thread;
00084         Converter< vector<char> > toByteArray;
00085 
00086         void run() {
00087                 MilliSleep sleep;
00088                 string s;
00089                 char buffer[80];
00090 
00091                 /******************************************
00092                 ** Read
00093                 *******************************************/
00094                 while (1) {
00095                         // Read a line from standard input
00096                         if ( fgets(buffer,80,stdin) == NULL ) {
00097                                 break;
00098                         }
00099                         // fgets always terminates with a null character, even if it manages
00100                         // to read n-1 chars
00101                         vector<char> hex_values = toByteArray(buffer);
00102 //                      std::cout << "Sending: ";
00103 //                      for ( unsigned int i = 0; i < hex_values.size(); ++i ) {
00104 //                              std::cout << hex_format(hex_values[i]) << " ";
00105 //                      }
00106                         std::cout << std::endl;
00107                         serial->write(&hex_values[0],hex_values.size());
00108                         if ( display_timestamps ) {
00109                                 timestamp.stamp();
00110                                 std::cout << "[" << timestamp << "]"  << std::endl;
00111                         }
00112                         sleep(5);
00113                 }
00114         }
00115 };
00116 
00117 class Reader
00118 {
00119 public:
00120         Reader(Serial *serial_device, bool timestamps = false) :
00121                 serial(serial_device),
00122                 display_timestamps(timestamps),
00123                 format(6,RightAlign,Dec),
00124                 hex_format(-1,NoAlign,Hex),
00125                 thread(&Reader::run,*this)
00126                 {}
00127 
00128         void wait() { thread.join(); }
00129 
00130 private:
00131         void run() {
00132                 char s[255];
00133                 unsigned long ch_read;
00134 
00135                 while (1) {
00136                         ch_read = serial->read(s,1);
00137 //            ch_read = serial->read(s,255);
00138                         if ( ch_read > 0 ) {
00139                                 if ( display_timestamps ) {
00140                                         time.stamp();
00141                                         std::cout << "[" << time << "] ";
00142                                 }
00143                                 for (unsigned int i = 0; i < ch_read; ++i) {
00144                                         std::cout << hex_format(s[i]) << " ";
00145                                 }
00146                                 std::cout << std::endl;
00147                         } else {
00148 //                              std::cout << "Timed out." << std::endl;
00149                         }
00150                 }
00151         }
00152         Serial *serial;
00153         bool display_timestamps;
00154         long current_time;
00155         TimeStamp time;
00156         Format<long> format;
00157         Format<unsigned char> hex_format;
00158         Thread thread;
00159 
00160 };
00161 
00162 } // namespace utils
00163 } // namespace ecl
00164 
00165 /*****************************************************************************
00166 ** Using
00167 *****************************************************************************/
00168 
00169 using namespace ecl::utils;
00170 
00171 /*****************************************************************************
00172 ** Main program
00173 *****************************************************************************/
00174 int main(int argc, char** argv) {
00175 
00176     /******************************************
00177      * Parse for the port name
00178      ******************************************/
00179     string port;
00180     BaudRate baud_rate;
00181     bool timestamps(false);
00182 
00183     try {
00184         CmdLine cmd("This is a simple interface for reading/echoing from a serial port [115.2k,8N1].",' ',"0.1");
00185         ValueArg<string> arg_port("p","port","Port to connect to",false,"/dev/ttyS0","string");
00186         ValueArg<string> arg_baud("b","baud","Baud rate (9600,38400,115200) [115200]",false,"115200","string");
00187         SwitchArg switch_timestamps("t","timestamps","Print timestamps.",false);
00188 
00189         cmd.add(arg_port);
00190         cmd.add(arg_baud);
00191         cmd.add(switch_timestamps);
00192         cmd.parse(argc,argv);
00193 
00194         port = arg_port.getValue();
00195         string baud = arg_baud.getValue();
00196         timestamps = switch_timestamps.getValue();
00197 
00198         if ( baud == "9600" ) {
00199                 baud_rate = BaudRate_9600;
00200         } else if ( baud == "38400" ) {
00201                 baud_rate = BaudRate_38400;
00202         } else {
00203                 baud_rate = BaudRate_115200;
00204         }
00205 
00206     } catch ( ArgException &e ) {
00207         std::cerr << "error: " << e.error() << " for arg " << e.argId() << std::endl;
00208     }
00209     try {
00210         /******************************************
00211         ** Open
00212         *******************************************/
00213         Serial serial(port,baud_rate,DataBits_8,StopBits_1,NoParity);
00214         serial.block(5000);
00215         Reader reader(&serial,timestamps);
00216         Writer writer(&serial,timestamps);
00217 
00218         reader.wait();
00219                 writer.wait();
00220     } catch ( StandardException &e ) {
00221         std::cout << e.what() << std::endl;
00222     }
00223 
00224     return 0;
00225 }


ecl_core_apps
Author(s): Daniel Stonier
autogenerated on Sun Oct 5 2014 23:36:25