5 using namespace boost::asio;
16 if( !
port.is_open() ) {
17 cerr <<
"Could not open serial port " << device << endl;
23 port.set_option(boost::asio::serial_port_base::baud_rate(baud));
24 port.set_option(boost::asio::serial_port_base::parity(boost::asio::serial_port_base::parity::none));
25 port.set_option(boost::asio::serial_port_base::stop_bits(boost::asio::serial_port_base::stop_bits::one));
27 cerr <<
"Could not set options on serial port " << device << endl;
38 return port.is_open();
44 boost::asio::write(
port, boost::asio::buffer(msg.c_str(),msg.length()));
49 boost::asio::write(
port, boost::asio::buffer(buffer, length));
55 boost::asio::write(
port, boost::asio::buffer(buffer, length));
60 if(!
port.is_open())
return -1;
65 boost::asio::read(
port, buffer(&in, 1));
66 }
catch (boost::exception_detail::clone_impl<boost::exception_detail::error_info_injector<boost::system::system_error> >& err) {
67 cerr <<
"Error reading serial port." << endl;
68 cerr << err.what() << endl;
78 char* bytes =
new char[numBytes];
79 for(
int i = 0; i < numBytes; i++)
char read()
Reads a single byte from the serial port.
boost::asio::serial_port port
std::string readln()
Reads bytes from the serial port until or is found.
SerialPort(std::string device, int baud)
The constructor takes in the path to the port (eg. "/dev/ttyUSB0") and a baud rate for the connection...
bool isOpen()
Returns true if the serial port is connected and open.
void write(std::string msg)
Writes the given string to the serial port.
std::string devicePath()
Returns the path to the device this port is connected to.