node-serialport-bindings-cpp/src/serialport_linux.cpp
Jade Bilkey 910438c2ca feat(linux): Custom baud rates for linux (eg 250k baudrate) (#1464)
* 250k baudrate now works
* Generic linux non-standard baudrate setting using termios2
* Tested successfully with an FTDI usb chip with the tx and rx pins bridged. Previous solution using the B38400 hack returned gibberish in same setup.
* Test for linux and win32 baudrate setting. Darwin stubbed out.

The arbitrary baud interface has been working on darwin up to this point so it's
likely that it works properly. The GetBaudRate extension should still get
written to be thorough but for now, this will let the CI tests pass properly and
still throw a warning about the check being disabled.

That is, of course, if anyone's testing with the arduino hardware on darwin,
otherwise, these tests never run anyway.
2018-02-05 09:33:11 -05:00

38 lines
707 B
C++
Executable file

#if defined(__linux__)
#include <sys/ioctl.h>
#include <asm/termbits.h>
// Uses the termios2 interface to set nonstandard baud rates
int linuxSetCustomBaudRate(const int fd, const unsigned int baudrate) {
struct termios2 t;
if(ioctl(fd, TCGETS2, &t)) {
return -1;
}
t.c_cflag &= ~CBAUD;
t.c_cflag |= BOTHER;
t.c_ospeed = t.c_ispeed = baudrate;
if(ioctl(fd, TCSETS2, &t)) {
return -2;
}
return 0;
}
// Uses termios2 interface to retrieve system reported baud rate
int linuxGetSystemBaudRate(const int fd, int* const outbaud) {
struct termios2 t;
if(ioctl(fd, TCGETS2, &t)) {
return -1;
}
*outbaud = (int)t.c_ospeed;
return 0;
}
#endif