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;
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 }
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::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
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::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)
main
int main(int argc, char **argv)
Definition: serial.cpp:175
ecl::utils::Reader::serial
Serial * serial
Definition: hex.cpp:160
ecl::utils::Writer::run
void run()
Definition: hex.cpp:96
ecl
ecl::utils::Reader::hex
bool hex
Definition: serial.cpp:161
errors.hpp


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