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