serial_example.cc
Go to the documentation of this file.
1 /***
2  * This example expects the serial port has a loopback on it.
3  *
4  * Alternatively, you could use an Arduino:
5  *
6  * <pre>
7  * void setup() {
8  * Serial.begin(<insert your baudrate here>);
9  * }
10  *
11  * void loop() {
12  * if (Serial.available()) {
13  * Serial.write(Serial.read());
14  * }
15  * }
16  * </pre>
17  */
18 
19 #include <string>
20 #include <iostream>
21 #include <cstdio>
22 
23 // OS Specific sleep
24 #ifdef _WIN32
25 #include <windows.h>
26 #else
27 #include <unistd.h>
28 #endif
29 
30 #include "serial/serial.h"
31 
32 using std::string;
33 using std::exception;
34 using std::cout;
35 using std::cerr;
36 using std::endl;
37 using std::vector;
38 
39 void my_sleep(unsigned long milliseconds) {
40 #ifdef _WIN32
41  Sleep(milliseconds); // 100 ms
42 #else
43  usleep(milliseconds*1000); // 100 ms
44 #endif
45 }
46 
48 {
49  vector<serial::PortInfo> devices_found = serial::list_ports();
50 
51  vector<serial::PortInfo>::iterator iter = devices_found.begin();
52 
53  while( iter != devices_found.end() )
54  {
55  serial::PortInfo device = *iter++;
56 
57  printf( "(%s, %s, %s)\n", device.port.c_str(), device.description.c_str(),
58  device.hardware_id.c_str() );
59  }
60 }
61 
63 {
64  cerr << "Usage: test_serial {-e|<serial port address>} ";
65  cerr << "<baudrate> [test string]" << endl;
66 }
67 
68 int run(int argc, char **argv)
69 {
70  if(argc < 2) {
71  print_usage();
72  return 0;
73  }
74 
75  // Argument 1 is the serial port or enumerate flag
76  string port(argv[1]);
77 
78  if( port == "-e" ) {
80  return 0;
81  }
82  else if( argc < 3 ) {
83  print_usage();
84  return 1;
85  }
86 
87  // Argument 2 is the baudrate
88  unsigned long baud = 0;
89 #if defined(WIN32) && !defined(__MINGW32__)
90  sscanf_s(argv[2], "%lu", &baud);
91 #else
92  sscanf(argv[2], "%lu", &baud);
93 #endif
94 
95  // port, baudrate, timeout in milliseconds
96  serial::Serial my_serial(port, baud, serial::Timeout::simpleTimeout(1000));
97 
98  cout << "Is the serial port open?";
99  if(my_serial.isOpen())
100  cout << " Yes." << endl;
101  else
102  cout << " No." << endl;
103 
104  // Get the Test string
105  int count = 0;
106  string test_string;
107  if (argc == 4) {
108  test_string = argv[3];
109  } else {
110  test_string = "Testing.";
111  }
112 
113  // Test the timeout, there should be 1 second between prints
114  cout << "Timeout == 1000ms, asking for 1 more byte than written." << endl;
115  while (count < 10) {
116  size_t bytes_wrote = my_serial.write(test_string);
117 
118  string result = my_serial.read(test_string.length()+1);
119 
120  cout << "Iteration: " << count << ", Bytes written: ";
121  cout << bytes_wrote << ", Bytes read: ";
122  cout << result.length() << ", String read: " << result << endl;
123 
124  count += 1;
125  }
126 
127  // Test the timeout at 250ms
128  my_serial.setTimeout(serial::Timeout::max(), 250, 0, 250, 0);
129  count = 0;
130  cout << "Timeout == 250ms, asking for 1 more byte than written." << endl;
131  while (count < 10) {
132  size_t bytes_wrote = my_serial.write(test_string);
133 
134  string result = my_serial.read(test_string.length()+1);
135 
136  cout << "Iteration: " << count << ", Bytes written: ";
137  cout << bytes_wrote << ", Bytes read: ";
138  cout << result.length() << ", String read: " << result << endl;
139 
140  count += 1;
141  }
142 
143  // Test the timeout at 250ms, but asking exactly for what was written
144  count = 0;
145  cout << "Timeout == 250ms, asking for exactly what was written." << endl;
146  while (count < 10) {
147  size_t bytes_wrote = my_serial.write(test_string);
148 
149  string result = my_serial.read(test_string.length());
150 
151  cout << "Iteration: " << count << ", Bytes written: ";
152  cout << bytes_wrote << ", Bytes read: ";
153  cout << result.length() << ", String read: " << result << endl;
154 
155  count += 1;
156  }
157 
158  // Test the timeout at 250ms, but asking for 1 less than what was written
159  count = 0;
160  cout << "Timeout == 250ms, asking for 1 less than was written." << endl;
161  while (count < 10) {
162  size_t bytes_wrote = my_serial.write(test_string);
163 
164  string result = my_serial.read(test_string.length()-1);
165 
166  cout << "Iteration: " << count << ", Bytes written: ";
167  cout << bytes_wrote << ", Bytes read: ";
168  cout << result.length() << ", String read: " << result << endl;
169 
170  count += 1;
171  }
172 
173  return 0;
174 }
175 
176 int main(int argc, char **argv) {
177  try {
178  return run(argc, argv);
179  } catch (exception &e) {
180  cerr << "Unhandled Exception: " << e.what() << endl;
181  }
182 }
std::vector< PortInfo > list_ports()
void print_usage()
void enumerate_ports()
static uint32_t max()
Definition: serial.h:102
void setTimeout(Timeout &timeout)
Definition: serial.cc:298
int main(int argc, char **argv)
std::string hardware_id
Definition: serial.h:759
static Timeout simpleTimeout(uint32_t timeout)
Definition: serial.h:112
bool isOpen() const
Definition: serial.cc:93
size_t read(uint8_t *buffer, size_t size)
Definition: serial.cc:124
std::string description
Definition: serial.h:756
void my_sleep(unsigned long milliseconds)
int run(int argc, char **argv)
std::string port
Definition: serial.h:753
size_t write(const uint8_t *data, size_t size)
Definition: serial.cc:268


serial
Author(s): William Woodall , John Harrison
autogenerated on Thu Jan 9 2020 07:18:58