1
votes

I am using Boost's ASIO class for serial communication. I would like to run it at a 1M baud rate on an OS X system. Whenever I try to set the baud rate to a standard value above 115200, I get an exception.

boost::asio::serial_port m_port;
baudrate = 1000000;
try {
  m_port.open(serial_port_path);
} catch (boost::system::system_error::exception e) {
  // ...
}

try {
  m_port.set_option(boost::asio::serial_port_base::baud_rate(baudrate));
} catch (boost::system::system_error::exception e) {
  // Tripped here on OS X
}

I am running OS X (Yosemite) and I installed Boost through home brew. I tested my code on a linux virtual machine (running on top of my OS X system) and it works fine.

Is there a way to make Boost support higher baud rates in OS X?

1

1 Answers

3
votes

You can get the file descriptor for the port using the native_handle() method, and then use the tcsetattr() function to set an arbitrary speed. I suspect Linux defines B1000000 as a preprocessor definition for speed where OS X does not.

Update:

A quick example of how to do this (just typed in, not tested):

#include <termios.h>

void set_speed(boost::asio::serial_port& p, unsigned speed)
{
    termios t;
    int fd = p.native_handle();

    if (tcgetattr(fd, &t) < 0) { /* handle error */ }
    if (cfsetspeed(&t, speed) < 0) { /* handle error */ }
    if (tcsetattr(fd, &t) < 0) { /* handle error */ }

    // Speed changed
}