]> Git Repo - serial.git/blame - examples/serial_example.cc
Cmake: Add BUILD_SHARED_LIBS option
[serial.git] / examples / serial_example.cc
CommitLineData
6853f8c0
WW
1/***
2 * This example expects the serial port has a loopback on it.
3 *
4 * Alternatively, you could use an Arduino:
9f89596e 5 *
6853f8c0 6 * <pre>
c5a2d7b4 7 * void setup() {
6853f8c0
WW
8 * Serial.begin(<insert your baudrate here>);
9 * }
9f89596e 10 *
c5a2d7b4 11 * void loop() {
6853f8c0
WW
12 * if (Serial.available()) {
13 * Serial.write(Serial.read());
14 * }
15 * }
16 * </pre>
17 */
18
7587b16f
WW
19#include <string>
20#include <iostream>
29783866 21#include <cstdio>
f7cee5e1 22
c429b0ee 23// OS Specific sleep
e9999c9c 24#ifdef _WIN32
c429b0ee
WW
25#include <windows.h>
26#else
27#include <unistd.h>
f7cee5e1 28#endif
7587b16f 29
93b8dfe2 30#include "serial/serial.h"
7587b16f 31
f7cee5e1 32using std::string;
c429b0ee 33using std::exception;
f7cee5e1 34using std::cout;
c429b0ee 35using std::cerr;
f7cee5e1 36using std::endl;
72604cec 37using std::vector;
f7cee5e1 38
c429b0ee 39void my_sleep(unsigned long milliseconds) {
e9999c9c 40#ifdef _WIN32
c429b0ee
WW
41 Sleep(milliseconds); // 100 ms
42#else
43 usleep(milliseconds*1000); // 100 ms
44#endif
45}
46
72604cec
CL
47void enumerate_ports()
48{
301a3d4b 49 vector<serial::PortInfo> devices_found = serial::list_ports();
72604cec 50
301a3d4b 51 vector<serial::PortInfo>::iterator iter = devices_found.begin();
72604cec
CL
52
53 while( iter != devices_found.end() )
54 {
301a3d4b 55 serial::PortInfo device = *iter++;
72604cec 56
301a3d4b 57 printf( "(%s, %s, %s)\n", device.port.c_str(), device.description.c_str(),
b8479822 58 device.hardware_id.c_str() );
72604cec
CL
59 }
60}
61
62void print_usage()
7587b16f 63{
72604cec 64 cerr << "Usage: test_serial {-e|<serial port address>} ";
6853f8c0 65 cerr << "<baudrate> [test string]" << endl;
72604cec
CL
66}
67
68int run(int argc, char **argv)
69{
70 if(argc < 2) {
71 print_usage();
7587b16f 72 return 0;
6853f8c0 73 }
72604cec
CL
74
75 // Argument 1 is the serial port or enumerate flag
6853f8c0
WW
76 string port(argv[1]);
77
72604cec
CL
78 if( port == "-e" ) {
79 enumerate_ports();
80 return 0;
81 }
82 else if( argc < 3 ) {
83 print_usage();
84 return 1;
85 }
86
6853f8c0
WW
87 // Argument 2 is the baudrate
88 unsigned long baud = 0;
780f76c4 89#if defined(WIN32) && !defined(__MINGW32__)
e11abb04
WW
90 sscanf_s(argv[2], "%lu", &baud);
91#else
6853f8c0 92 sscanf(argv[2], "%lu", &baud);
e11abb04 93#endif
6853f8c0
WW
94
95 // port, baudrate, timeout in milliseconds
e9999c9c 96 serial::Serial my_serial(port, baud, serial::Timeout::simpleTimeout(1000));
6853f8c0
WW
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
e9999c9c 128 my_serial.setTimeout(serial::Timeout::max(), 250, 0, 250, 0);
6853f8c0
WW
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;
0f75942f 174}
241daf30
WW
175
176int main(int argc, char **argv) {
c429b0ee 177 try {
241daf30 178 return run(argc, argv);
c429b0ee
WW
179 } catch (exception &e) {
180 cerr << "Unhandled Exception: " << e.what() << endl;
181 }
241daf30 182}
This page took 0.072594 seconds and 5 git commands to generate.