mavros icon indicating copy to clipboard operation
mavros copied to clipboard

Serial port baud rates are dependent and restricted by the version of asio

Open slovak194 opened this issue 1 year ago • 5 comments

Issue details

Serial port baud rates are dependent and restricted by the version of asio. Particularly, in the serial_port_base.ipp some baud rates might be missing but still supported by the OS with termios/termios2/ioctl. In my case, it is the 1500000 baud rate which throws the Device exceptio within MAVROS but works fine if set manually. In the latest asio master it is still missing. https://github.com/chriskohlhoff/asio/blob/62481a25be6cf78cbe714419a4462fd89bd84ab9/asio/include/asio/impl/serial_port_base.ipp#L93C8-L101C40

# ifdef B1000000
  case 1000000: baud = B1000000; break;
# endif
# ifdef B1152000
  case 1152000: baud = B1152000; break;
# endif
# ifdef B2000000
  case 2000000: baud = B2000000; break;

A proposed solution might be to split the try-catch in the serial device constructor into sections and try one more time with serial_dev.native_handle() and ioctl if the baud rate option causes an exception.

MAVROS version and platform

Mavros: N/A ROS: N/A Ubuntu: 22.04

Best regards, Alex

slovak194 avatar Jan 02 '25 14:01 slovak194

I'd rather not to reinvent the wheel with that termios (as it were here at early days...). Also are you sure, your UART works fine on such high rates?

vooon avatar Jan 02 '25 15:01 vooon

I'd rather not to reinvent the wheel with that termios (as it were here at early days...).

That is true, but what is the alternative if we have to deal with hardcoded baud rates in ASIO? It worked well for me until now.

Also are you sure, your UART works fine on such high rates?

Ardupilot specifies literaly "Most stm32-based boards can support rates of up to 1500". Hardware-wise serial ports on modern controllers might work at 12.5 MBaud/s in PPP mode. On my end 1500000 bps and 2000000 bps work fine even with finicky cables.

slovak194 avatar Jan 03 '25 10:01 slovak194

Well, you can just package up fresh version of the ASIO, or make a patched version.

vooon avatar Jan 03 '25 12:01 vooon

I would agree with @vooon that it's better not to re-write this all from scratch. The asio libraries are well optimised, it would be disservice to start from scratch. I would propose three options:

  • Create an upstream ticket to get the baud rates functional in asio, then run a custom build until they're mainlined
  • Patch the software yourself to get the functionality, then run with it from there
  • Workaround with a software like socat to get the serial port working at the baud you want, and interface with UDP on the mavros side. I would imagine this work-around would be the least effort, if not a little clunky.

pryre avatar Feb 13 '25 00:02 pryre

The asio libraries are well optimised, it would be disservice to start from scratch.

I feel there is a misunderstanding. No reason to start anything from scratch. ASIO does not aim to handle every aspect of serial communication, which is why it provides the native_handle() method for such things.

BR, Alex

slovak194 avatar Feb 24 '25 13:02 slovak194