1
0
mirror of https://github.com/wjwwood/serial.git synced 2026-01-22 19:54:57 +08:00
serial/examples/serial_example.cc

50 lines
1.2 KiB
C++
Raw Normal View History

2011-03-16 08:03:54 -05:00
#include <string>
#include <iostream>
2012-01-23 14:23:01 -06:00
#include <stdio.h>
2011-03-16 08:03:54 -05:00
2012-01-23 09:54:31 -06:00
#include <boost/thread.hpp>
#include "serial/serial.h"
2011-03-16 08:03:54 -05:00
2012-01-23 09:54:31 -06:00
int run(int argc, char **argv)
2011-03-16 08:03:54 -05:00
{
if(argc < 3) {
std::cerr << "Usage: test_serial <serial port address> <baudrate>" << std::endl;
return 0;
}
std::string port(argv[1]);
unsigned long baud = 0;
sscanf(argv[2], "%lu", &baud);
2012-01-23 09:54:31 -06:00
// port, baudrate, timeout in milliseconds
serial::Serial serial(port, baud, 250);
2011-03-16 08:03:54 -05:00
std::cout << "Is the serial port open?";
if(serial.isOpen())
std::cout << " Yes." << std::endl;
else
std::cout << " No." << std::endl;
int count = 0;
while (count >= 0) {
2012-01-23 14:28:14 -06:00
size_t bytes_wrote = serial.write("Testing.");
std::string result = serial.read(8);
2012-01-23 09:54:31 -06:00
std::cout << ">" << count << ">" << bytes_wrote << ">";
std::cout << result.length() << "<" << result << std::endl;
count += 1;
2012-01-23 09:54:31 -06:00
boost::this_thread::sleep(boost::posix_time::milliseconds(100));
2011-03-16 08:03:54 -05:00
}
return 0;
2011-03-18 19:02:32 -05:00
}
2012-01-23 09:54:31 -06:00
int main(int argc, char **argv) {
try {
return run(argc, argv);
} catch (std::exception &e) {
std::cerr << "Unhandled Exception: " << e.what() << std::endl;
}
}