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:
78  bool display_timestamps;
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 ) {
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;
153  bool display_timestamps;
154  long current_time;
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 }
ecl::utils::Reader::Reader
Reader(Serial *serial_device, bool timestamps=false)
Definition: hex.cpp:128
ecl::Hex
Hex
threads.hpp
ecl::utils::Reader::thread
Thread thread
Definition: hex.cpp:166
ecl::utils::Reader
Definition: hex.cpp:125
ecl::DataBits_8
DataBits_8
ecl::NoAlign
NoAlign
ecl::utils::Reader::format
Format< long > format
Definition: hex.cpp:164
command_line.hpp
ecl::utils::Reader::run
void run()
Definition: hex.cpp:139
ecl::utils::Writer::toByteArray
Converter< vector< char > > toByteArray
Definition: hex.cpp:94
ecl::utils::Reader::current_time
long current_time
Definition: hex.cpp:162
ecl::utils::Writer::serial
Serial * serial
Definition: hex.cpp:91
formatters.hpp
ecl::CmdLine
ecl::utils::Writer::timestamp
TimeStamp timestamp
Definition: hex.cpp:90
ecl::utils::Reader::wait
void wait()
Definition: hex.cpp:136
ecl::BaudRate_115200
BaudRate_115200
ipc.hpp
ecl::RightAlign
RightAlign
ecl::utils::Reader::time
TimeStamp time
Definition: hex.cpp:163
ecl::utils::Reader::hex_format
Format< unsigned char > hex_format
Definition: hex.cpp:165
ecl::utils::Writer::wait
void wait()
Definition: hex.cpp:85
ecl::utils::Writer
Definition: hex.cpp:74
ecl::utils::Writer::Writer
Writer(Serial *serial_device, bool timestamps=false)
Definition: hex.cpp:79
ecl::SwitchArg::getValue
bool getValue()
ecl::ValueArg::getValue
T & getValue()
ecl::ArgException
main
int main(int argc, char **argv)
Definition: hex.cpp:174
ecl::BaudRate_38400
BaudRate_38400
ecl::Dec
Dec
ecl::CmdLine::add
void add(Arg &a)
ecl::NoParity
NoParity
ecl::utils::Writer::display_timestamps
bool display_timestamps
Definition: hex.cpp:88
ecl::SwitchArg
ecl::utils
Utility programs.
serial.hpp
ecl::StandardException
ecl::utils::Writer::thread
Thread thread
Definition: hex.cpp:93
ecl::BaudRate_9600
BaudRate_9600
ecl::utils::Reader::display_timestamps
bool display_timestamps
Definition: hex.cpp:161
ecl::Format
ecl::ValueArg
ecl::BaudRate
BaudRate
ecl::StandardException::what
const char * what() const
ecl::utils::Writer::hex_format
Format< char > hex_format
Definition: hex.cpp:92
ecl::utils::Writer::current_time
long current_time
Definition: hex.cpp:89
time.hpp
ecl::StopBits_1
StopBits_1
ecl::ArgException::argId
std::string argId() const
ecl::ArgException::error
std::string error() const
ecl::CmdLine::parse
void parse(int argc, char **argv)
ecl::Format< char >
converters.hpp
ecl::utils::Reader::serial
Serial * serial
Definition: hex.cpp:160
ecl::utils::Writer::run
void run()
Definition: hex.cpp:96
ecl
ecl::Converter
errors.hpp


ecl_core_apps
Author(s): Daniel Stonier
autogenerated on Wed Mar 2 2022 00:16:52