Question

Does anybody know of a good serial communication library that is a decent alternative to boost::asio::serialport?

Boost::Asio::SerialPort does not seem to support non-standard baud rates - like 31250 (it definitely does not seem to work on Mac OS 10.6 with boost 1.48.0)

I need the library to work on a minimum of Mac OS X and Windows

Was it helpful?

Solution

The easiest workaround is to create the asio::serial_port instance for the appropriate device, and then call serial_port.native_native() and use the returned handle to call ioctl() or SetCommState() to set the speed.

That way you can use asio for everything except setting the line speed, and you can get the line speed you want with minimal special code.

You will need this code to be special for each operating system, but at least everything else is common.

However: The bug you reference is for a specific workaround on a specific version of Linux/glibc. Are you having this problem on Windows and OSX as well?

OTHER TIPS

That's the MIDI baudrate. Always struck me as a rate optimized to ensure you're forced to buy hardware from the music instrument vendors. Shop for the hardware first, the software usually comes with it. So you don't end up doing this to your nice Apple machine:

enter image description here

You can use ioctl and termios to directly control the baud rate and serial port parameters. Its not terribly difficult.

If you only need a simple serial port and do not need the asynchronous IO components you can try: https://github.com/wjwwood/serial its a library a friend of mine wrote for some of his projects. Be sure to use the boostless branch. I know the OS X drivers support custom baud rates, I have tested those, and custom baud rates should work in Linux as well. It doesn't currently support Windows, so if you need Windows support, its won't be much help at the moment.

If you only want to use ioctl and termios you can do:

#define IOSSIOSPEED _IOW('T', 2, speed_t)
int new_baud = static_cast<int> (baudrate_);
ioctl (fd_, IOSSIOSPEED, &new_baud, 1);

And it will let you set the baud rate to any value in OS X, but that is OS specific. for Linux you need to do:

struct serial_struct ser;
ioctl (fd_, TIOCGSERIAL, &ser);
// set custom divisor
ser.custom_divisor = ser.baud_base / baudrate_;
// update flags
ser.flags &= ~ASYNC_SPD_MASK;
ser.flags |= ASYNC_SPD_CUST;

if (ioctl (fd_, TIOCSSERIAL, ser) < 0)
{
  // error
}

For any other OS your gonna have to go read some man pages or it might not even work at all, its very OS dependent.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top