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


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