serial  1.2.1
Cross-platform, serial port library written in C++
Functions
serial_example.cc File Reference
#include <string>
#include <iostream>
#include <cstdio>
#include <unistd.h>
#include "serial/serial.h"
Include dependency graph for serial_example.cc:

Functions

void my_sleep (unsigned long milliseconds)
 
void enumerate_ports ()
 
void print_usage ()
 
int run (int argc, char **argv)
 
int main (int argc, char **argv)
 

Function Documentation

void enumerate_ports ( )
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 }
std::vector< PortInfo > list_ports()
std::string hardware_id
Definition: serial.h:759
std::string description
Definition: serial.h:756
Definition: serial.h:750
std::string port
Definition: serial.h:753
int main ( int  argc,
char **  argv 
)
176  {
177  try {
178  return run(argc, argv);
179  } catch (exception &e) {
180  cerr << "Unhandled Exception: " << e.what() << endl;
181  }
182 }
int run(int argc, char **argv)
Definition: serial_example.cc:68
void my_sleep ( unsigned long  milliseconds)
39  {
40 #ifdef _WIN32
41  Sleep(milliseconds); // 100 ms
42 #else
43  usleep(milliseconds*1000); // 100 ms
44 #endif
45 }
void print_usage ( )
63 {
64  cerr << "Usage: test_serial {-e|<serial port address>} ";
65  cerr << "<baudrate> [test string]" << endl;
66 }
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 }
void print_usage()
Definition: serial_example.cc:62
void enumerate_ports()
Definition: serial_example.cc:47
static uint32_t max()
Definition: serial.h:102
Definition: serial.h:147
static Timeout simpleTimeout(uint32_t timeout)
Definition: serial.h:112