hex.cpp
Go to the documentation of this file.
1 
9 /*****************************************************************************
10 ** Includes
11 *****************************************************************************/
12 
13 #include <iostream>
14 #include <string>
15 #include <vector>
16 #include <ecl/command_line.hpp>
17 #include <ecl/converters.hpp>
18 #include <ecl/errors.hpp>
19 #include <ecl/ipc.hpp>
20 #include <ecl/time.hpp>
21 #include <ecl/threads.hpp>
22 #include <ecl/devices/serial.hpp>
23 #include <ecl/formatters.hpp>
24 
25 
26 /*****************************************************************************
27 ** Namespaces
28 *****************************************************************************/
29 
30 namespace ecl {
31 namespace utils {
32 
33 /*****************************************************************************
34  * Using
35  ****************************************************************************/
36 
37 using std::string;
38 using std::vector;
39 using ecl::ArgException;
40 using ecl::CmdLine;
41 using ecl::SwitchArg;
42 using ecl::ValueArg;
43 using ecl::Serial;
44 using ecl::BaudRate;
45 using ecl::BaudRate_9600;
48 using ecl::DataBits_8;
49 using ecl::StopBits_1;
50 using ecl::NoParity;
52 using ecl::Format;
53 using ecl::Dec;
54 using ecl::Hex;
55 using ecl::NoAlign;
56 using ecl::RightAlign;
57 using ecl::Thread;
58 using ecl::TimeStamp;
59 using ecl::MilliSleep;
60 using ecl::Converter;
61 
62 /*****************************************************************************
63  * Classes
64  ****************************************************************************/
65 
66 class Writer
67 {
68 public:
69  Writer(Serial *serial_device, bool timestamps = false) :
70  display_timestamps(timestamps),
71  serial(serial_device),
73  thread(&Writer::run,*this)
74  {}
75  void wait() { thread.join(); }
76 
77 private:
80  TimeStamp timestamp;
81  Serial *serial;
83  Thread thread;
85 
86  void run() {
87  MilliSleep sleep;
88  string s;
89  char buffer[80];
90 
91  /******************************************
92  ** Read
93  *******************************************/
94  while (1) {
95  // Read a line from standard input
96  if ( fgets(buffer,80,stdin) == NULL ) {
97  break;
98  }
99  // fgets always terminates with a null character, even if it manages
100  // to read n-1 chars
101  vector<char> hex_values = toByteArray(buffer);
102 // std::cout << "Sending: ";
103 // for ( unsigned int i = 0; i < hex_values.size(); ++i ) {
104 // std::cout << hex_format(hex_values[i]) << " ";
105 // }
106  std::cout << std::endl;
107  serial->write(&hex_values[0],hex_values.size());
108  if ( display_timestamps ) {
109  timestamp.stamp();
110  std::cout << "[" << timestamp << "]" << std::endl;
111  }
112  sleep(5);
113  }
114  }
115 };
116 
117 class Reader
118 {
119 public:
120  Reader(Serial *serial_device, bool timestamps = false) :
121  serial(serial_device),
122  display_timestamps(timestamps),
123  format(6,RightAlign,Dec),
124  hex_format(-1,NoAlign,Hex),
125  thread(&Reader::run,*this)
126  {}
127 
128  void wait() { thread.join(); }
129 
130 private:
131  void run() {
132  char s[255];
133  unsigned long ch_read;
134 
135  while (1) {
136  ch_read = serial->read(s,1);
137 // ch_read = serial->read(s,255);
138  if ( ch_read > 0 ) {
139  if ( display_timestamps ) {
140  time.stamp();
141  std::cout << "[" << time << "] ";
142  }
143  for (unsigned int i = 0; i < ch_read; ++i) {
144  std::cout << hex_format(s[i]) << " ";
145  }
146  std::cout << std::endl;
147  } else {
148 // std::cout << "Timed out." << std::endl;
149  }
150  }
151  }
152  Serial *serial;
155  TimeStamp time;
158  Thread thread;
159 
160 };
161 
162 } // namespace utils
163 } // namespace ecl
164 
165 /*****************************************************************************
166 ** Using
167 *****************************************************************************/
168 
169 using namespace ecl::utils;
170 
171 /*****************************************************************************
172 ** Main program
173 *****************************************************************************/
174 int main(int argc, char** argv) {
175 
176  /******************************************
177  * Parse for the port name
178  ******************************************/
179  string port;
180  BaudRate baud_rate;
181  bool timestamps(false);
182 
183  try {
184  CmdLine cmd("This is a simple interface for reading/echoing from a serial port [115.2k,8N1].",' ',"0.1");
185  ValueArg<string> arg_port("p","port","Port to connect to",false,"/dev/ttyS0","string");
186  ValueArg<string> arg_baud("b","baud","Baud rate (9600,38400,115200) [115200]",false,"115200","string");
187  SwitchArg switch_timestamps("t","timestamps","Print timestamps.",false);
188 
189  cmd.add(arg_port);
190  cmd.add(arg_baud);
191  cmd.add(switch_timestamps);
192  cmd.parse(argc,argv);
193 
194  port = arg_port.getValue();
195  string baud = arg_baud.getValue();
196  timestamps = switch_timestamps.getValue();
197 
198  if ( baud == "9600" ) {
199  baud_rate = BaudRate_9600;
200  } else if ( baud == "38400" ) {
201  baud_rate = BaudRate_38400;
202  } else {
203  baud_rate = BaudRate_115200;
204  }
205 
206  } catch ( ArgException &e ) {
207  std::cerr << "error: " << e.error() << " for arg " << e.argId() << std::endl;
208  }
209  try {
210  /******************************************
211  ** Open
212  *******************************************/
213  Serial serial(port,baud_rate,DataBits_8,StopBits_1,NoParity);
214  serial.block(5000);
215  Reader reader(&serial,timestamps);
216  Writer writer(&serial,timestamps);
217 
218  reader.wait();
219  writer.wait();
220  } catch ( StandardException &e ) {
221  std::cout << e.what() << std::endl;
222  }
223 
224  return 0;
225 }
Writer(Serial *serial_device, bool timestamps=false)
Definition: hex.cpp:69
Thread thread
Definition: hex.cpp:83
int main(int argc, char **argv)
Definition: hex.cpp:174
const char * what() const
Format< char > hex_format
Definition: hex.cpp:82
Format< unsigned char > hex_format
Definition: hex.cpp:157
bool display_timestamps
Definition: hex.cpp:78
Serial * serial
Definition: hex.cpp:152
BaudRate_9600
long current_time
Definition: hex.cpp:79
StopBits_1
void add(Arg &a)
Thread thread
Definition: hex.cpp:158
bool display_timestamps
Definition: hex.cpp:153
DataBits_8
long current_time
Definition: hex.cpp:154
Reader(Serial *serial_device, bool timestamps=false)
Definition: hex.cpp:120
TimeStamp time
Definition: hex.cpp:155
void run()
Definition: hex.cpp:86
Serial * serial
Definition: hex.cpp:81
RightAlign
std::string argId() const
TimeStamp timestamp
Definition: hex.cpp:80
BaudRate_115200
Utility programs.
Converter< vector< char > > toByteArray
Definition: hex.cpp:84
std::string error() const
void parse(int argc, char **argv)
BaudRate_38400
Format< long > format
Definition: hex.cpp:156
void wait()
Definition: hex.cpp:75


ecl_core_apps
Author(s): Daniel Stonier
autogenerated on Mon Jun 10 2019 13:08:55