2012-02-04 21:56:10 -06:00
|
|
|
/***
|
|
|
|
|
* This example expects the serial port has a loopback on it.
|
|
|
|
|
*
|
|
|
|
|
* Alternatively, you could use an Arduino:
|
|
|
|
|
*
|
|
|
|
|
* <pre>
|
2012-07-09 15:51:38 -05:00
|
|
|
* void setup() {
|
2012-02-04 21:56:10 -06:00
|
|
|
* Serial.begin(<insert your baudrate here>);
|
|
|
|
|
* }
|
|
|
|
|
*
|
2012-07-09 15:51:38 -05:00
|
|
|
* void loop() {
|
2012-02-04 21:56:10 -06:00
|
|
|
* if (Serial.available()) {
|
|
|
|
|
* Serial.write(Serial.read());
|
|
|
|
|
* }
|
|
|
|
|
* }
|
|
|
|
|
* </pre>
|
|
|
|
|
*/
|
|
|
|
|
|
2011-03-16 08:03:54 -05:00
|
|
|
#include <string>
|
|
|
|
|
#include <iostream>
|
2012-02-04 21:14:22 -06:00
|
|
|
#include <cstdio>
|
2012-01-30 16:33:58 -06:00
|
|
|
|
2012-02-03 01:43:42 -06:00
|
|
|
// OS Specific sleep
|
2012-06-07 19:26:55 -05:00
|
|
|
#ifdef _WIN32
|
2012-02-03 01:43:42 -06:00
|
|
|
#include <windows.h>
|
|
|
|
|
#else
|
|
|
|
|
#include <unistd.h>
|
2012-01-30 16:33:58 -06:00
|
|
|
#endif
|
2011-03-16 08:03:54 -05:00
|
|
|
|
2012-01-01 01:12:00 -06:00
|
|
|
#include "serial/serial.h"
|
2011-03-16 08:03:54 -05:00
|
|
|
|
2012-01-30 16:33:58 -06:00
|
|
|
using std::string;
|
2012-02-03 01:43:42 -06:00
|
|
|
using std::exception;
|
2012-01-30 16:33:58 -06:00
|
|
|
using std::cout;
|
2012-02-03 01:43:42 -06:00
|
|
|
using std::cerr;
|
2012-01-30 16:33:58 -06:00
|
|
|
using std::endl;
|
|
|
|
|
|
2012-02-03 01:43:42 -06:00
|
|
|
void my_sleep(unsigned long milliseconds) {
|
2012-06-07 19:26:55 -05:00
|
|
|
#ifdef _WIN32
|
2012-02-03 01:43:42 -06:00
|
|
|
Sleep(milliseconds); // 100 ms
|
|
|
|
|
#else
|
|
|
|
|
usleep(milliseconds*1000); // 100 ms
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
2012-01-23 09:54:31 -06:00
|
|
|
int run(int argc, char **argv)
|
2011-03-16 08:03:54 -05:00
|
|
|
{
|
2012-02-04 21:56:10 -06:00
|
|
|
if(argc < 3) {
|
|
|
|
|
cerr << "Usage: test_serial <serial port address> ";
|
|
|
|
|
cerr << "<baudrate> [test string]" << endl;
|
2011-03-16 08:03:54 -05:00
|
|
|
return 0;
|
2012-02-04 21:56:10 -06:00
|
|
|
}
|
|
|
|
|
// Argument 1 is the serial port
|
|
|
|
|
string port(argv[1]);
|
|
|
|
|
|
|
|
|
|
// Argument 2 is the baudrate
|
|
|
|
|
unsigned long baud = 0;
|
2013-08-02 21:20:16 -07:00
|
|
|
#ifdef WIN32
|
|
|
|
|
sscanf_s(argv[2], "%lu", &baud);
|
|
|
|
|
#else
|
2012-02-04 21:56:10 -06:00
|
|
|
sscanf(argv[2], "%lu", &baud);
|
2013-08-02 21:20:16 -07:00
|
|
|
#endif
|
2012-02-04 21:56:10 -06:00
|
|
|
|
|
|
|
|
// port, baudrate, timeout in milliseconds
|
2012-06-07 19:26:55 -05:00
|
|
|
serial::Serial my_serial(port, baud, serial::Timeout::simpleTimeout(1000));
|
2012-02-04 21:56:10 -06:00
|
|
|
|
|
|
|
|
cout << "Is the serial port open?";
|
|
|
|
|
if(my_serial.isOpen())
|
|
|
|
|
cout << " Yes." << endl;
|
|
|
|
|
else
|
|
|
|
|
cout << " No." << endl;
|
|
|
|
|
|
|
|
|
|
// Get the Test string
|
|
|
|
|
int count = 0;
|
|
|
|
|
string test_string;
|
|
|
|
|
if (argc == 4) {
|
|
|
|
|
test_string = argv[3];
|
|
|
|
|
} else {
|
|
|
|
|
test_string = "Testing.";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Test the timeout, there should be 1 second between prints
|
|
|
|
|
cout << "Timeout == 1000ms, asking for 1 more byte than written." << endl;
|
|
|
|
|
while (count < 10) {
|
|
|
|
|
size_t bytes_wrote = my_serial.write(test_string);
|
|
|
|
|
|
|
|
|
|
string result = my_serial.read(test_string.length()+1);
|
|
|
|
|
|
|
|
|
|
cout << "Iteration: " << count << ", Bytes written: ";
|
|
|
|
|
cout << bytes_wrote << ", Bytes read: ";
|
|
|
|
|
cout << result.length() << ", String read: " << result << endl;
|
|
|
|
|
|
|
|
|
|
count += 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Test the timeout at 250ms
|
2012-06-07 19:26:55 -05:00
|
|
|
my_serial.setTimeout(serial::Timeout::max(), 250, 0, 250, 0);
|
2012-02-04 21:56:10 -06:00
|
|
|
count = 0;
|
|
|
|
|
cout << "Timeout == 250ms, asking for 1 more byte than written." << endl;
|
|
|
|
|
while (count < 10) {
|
|
|
|
|
size_t bytes_wrote = my_serial.write(test_string);
|
|
|
|
|
|
|
|
|
|
string result = my_serial.read(test_string.length()+1);
|
|
|
|
|
|
|
|
|
|
cout << "Iteration: " << count << ", Bytes written: ";
|
|
|
|
|
cout << bytes_wrote << ", Bytes read: ";
|
|
|
|
|
cout << result.length() << ", String read: " << result << endl;
|
|
|
|
|
|
|
|
|
|
count += 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Test the timeout at 250ms, but asking exactly for what was written
|
|
|
|
|
count = 0;
|
|
|
|
|
cout << "Timeout == 250ms, asking for exactly what was written." << endl;
|
|
|
|
|
while (count < 10) {
|
|
|
|
|
size_t bytes_wrote = my_serial.write(test_string);
|
|
|
|
|
|
|
|
|
|
string result = my_serial.read(test_string.length());
|
|
|
|
|
|
|
|
|
|
cout << "Iteration: " << count << ", Bytes written: ";
|
|
|
|
|
cout << bytes_wrote << ", Bytes read: ";
|
|
|
|
|
cout << result.length() << ", String read: " << result << endl;
|
|
|
|
|
|
|
|
|
|
count += 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Test the timeout at 250ms, but asking for 1 less than what was written
|
|
|
|
|
count = 0;
|
|
|
|
|
cout << "Timeout == 250ms, asking for 1 less than was written." << endl;
|
|
|
|
|
while (count < 10) {
|
|
|
|
|
size_t bytes_wrote = my_serial.write(test_string);
|
|
|
|
|
|
|
|
|
|
string result = my_serial.read(test_string.length()-1);
|
|
|
|
|
|
|
|
|
|
cout << "Iteration: " << count << ", Bytes written: ";
|
|
|
|
|
cout << bytes_wrote << ", Bytes read: ";
|
|
|
|
|
cout << result.length() << ", String read: " << result << endl;
|
|
|
|
|
|
|
|
|
|
count += 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 0;
|
2011-03-18 19:02:32 -05:00
|
|
|
}
|
2012-01-23 09:54:31 -06:00
|
|
|
|
|
|
|
|
int main(int argc, char **argv) {
|
2012-02-03 01:43:42 -06:00
|
|
|
try {
|
2012-01-23 09:54:31 -06:00
|
|
|
return run(argc, argv);
|
2012-02-03 01:43:42 -06:00
|
|
|
} catch (exception &e) {
|
|
|
|
|
cerr << "Unhandled Exception: " << e.what() << endl;
|
|
|
|
|
}
|
2012-01-23 09:54:31 -06:00
|
|
|
}
|