serial_timeouts.cpp
Go to the documentation of this file.
1 
10 /*****************************************************************************
11 ** Platform Check
12 *****************************************************************************/
13 
14 #include <ecl/config.hpp>
15 
16 /*****************************************************************************
17 ** Includes
18 *****************************************************************************/
19 
20 #include <iostream>
21 #include <string>
22 #include <ecl/time/timestamp.hpp>
23 #include "../../include/ecl/devices/serial.hpp"
24 
25 /*****************************************************************************
26 ** Namespaces
27 *****************************************************************************/
28 
29 using namespace ecl;
30 
31 /*****************************************************************************
32 ** Functions
33 *****************************************************************************/
34 
35 void print_usage() {
36  std::cout << "Usage: demo_serial_timeouts [port]" << std::endl;
37  std::cout << std::endl;
38  std::cout << " default value for port is /dev/ttyUSB0" << std::endl;
39 }
40 
41 /*****************************************************************************
42 ** Main
43 *****************************************************************************/
44 
45 int main(int argc, char **argv) {
46 
47  std::string port("/dev/ttyUSB0");
48  if ( argc > 1 ) {
49  std::string arg(argv[1]);
50  if ( arg == "--help" ) {
51  print_usage();
52  return 0;
53  } else {
54  port = argv[1];
55  }
56  }
57  std::cout << std::endl;
58  std::cout << "***********************************************************" << std::endl;
59  std::cout << " Serial Timeouts" << std::endl;
60  std::cout << "***********************************************************" << std::endl;
61  std::cout << "* For demo'ing low latency read timeouts on posix systems." << std::endl;
62  std::cout << "* Timeouts < 100ms use a custom loop, > 100ms use termios." << std::endl;
63  std::cout << "* Hook this up to a serial cable to test (actual" << std::endl;
64  std::cout << "* connection not important)." << std::endl;
65  std::cout << "***********************************************************" << std::endl;
66  std::cout << std::endl;
67 
68  Serial serial;
69  try {
71  } catch (StandardException &e ) {
72  std::cout << "[ERROR] : error opening " << port << std::endl;
73  std::cout << std::endl;
74  print_usage();
75  return 0;
76  }
77  TimeStamp time, pre_read_time;
78  char buffer[256];
79 
80  std::cout << std::endl;
81  std::cout << "***********************************************************" << std::endl;
82  std::cout << " 100 ms timeouts" << std::endl;
83  std::cout << "***********************************************************" << std::endl;
84  std::cout << "* This will use termios to scan." << std::endl;
85  std::cout << "***********************************************************" << std::endl;
86  std::cout << std::endl;
87 
88  serial.block(100);
89  for ( unsigned int i = 0; i < 10; ++i ) {
90  pre_read_time.stamp();
91  long result = serial.read(buffer,256);
92  time.stamp();
93  if ( result > 0 ) {
94  std::cout << "[INFO] : read " << result << " bytes." << std::endl;
95  } else if ( result == 0 ) {
96  std::cout << "[INFO] : timed out [" << (time - pre_read_time) << "]." << std::endl;
97  } else {
98  std::cout << "[INFO] : error " << result << "." << std::endl;
99  }
100  }
101 
102  std::cout << std::endl;
103  std::cout << "***********************************************************" << std::endl;
104  std::cout << " 50 ms timeouts" << std::endl;
105  std::cout << "***********************************************************" << std::endl;
106  std::cout << "* This will internally scan with 5ms loops." << std::endl;
107  std::cout << "***********************************************************" << std::endl;
108  std::cout << std::endl;
109 
110  serial.block(50);
111  // uncomment this to test reading without a timeout on a loopback connection.
112  // buffer[0] = 'a';
113  // buffer[1] = 'b';
114  // serial.write(buffer,2);
115  for ( unsigned int i = 0; i < 10; ++i ) {
116  pre_read_time.stamp();
117  long result = serial.read(buffer,256);
118  time.stamp();
119  if ( result > 0 ) {
120  std::cout << "[INFO] : read " << result << " bytes." << std::endl;
121  } else if ( result == 0 ) {
122  std::cout << "[INFO] : timed out [" << (time - pre_read_time) << "]." << std::endl;
123  } else {
124  std::cout << "[INFO] : error " << result << "." << std::endl;
125  }
126  }
127  std::cout << std::endl;
128  std::cout << "***********************************************************" << std::endl;
129  std::cout << " 20 ms timeouts" << std::endl;
130  std::cout << "***********************************************************" << std::endl;
131  std::cout << "* This will internally scan with 2ms loops." << std::endl;
132  std::cout << "***********************************************************" << std::endl;
133  std::cout << std::endl;
134 
135  serial.block(20);
136  for ( unsigned int i = 0; i < 10; ++i ) {
137  pre_read_time.stamp();
138  long result = serial.read(buffer,256);
139  time.stamp();
140  if ( result > 0 ) {
141  std::cout << "[INFO] : read " << result << " bytes." << std::endl;
142  } else if ( result == 0 ) {
143  std::cout << "[INFO] : timed out [" << (time - pre_read_time) << "]." << std::endl;
144  } else {
145  std::cout << "[INFO] : error " << result << "." << std::endl;
146  }
147  }
148  std::cout << std::endl;
149  std::cout << "***********************************************************" << std::endl;
150  std::cout << " 5 ms timeouts" << std::endl;
151  std::cout << "***********************************************************" << std::endl;
152  std::cout << "* This will internally scan with 1ms loops." << std::endl;
153  std::cout << "***********************************************************" << std::endl;
154  std::cout << std::endl;
155 
156  serial.block(5);
157  for ( unsigned int i = 0; i < 10; ++i ) {
158  pre_read_time.stamp();
159  long result = serial.read(buffer,256);
160  time.stamp();
161  if ( result > 0 ) {
162  std::cout << "[INFO] : read " << result << " bytes." << std::endl;
163  } else if ( result == 0 ) {
164  std::cout << "[INFO] : timed out [" << (time - pre_read_time) << "]." << std::endl;
165  } else {
166  std::cout << "[INFO] : error " << result << "." << std::endl;
167  }
168  }
169  return 0;
170 }
void print_usage()
Embedded control libraries.
int main(int argc, char **argv)


ecl_devices
Author(s): Daniel Stonier
autogenerated on Mon Jun 10 2019 13:08:45