Skip to content

Instantly share code, notes, and snippets.

@kennethryerson
Created May 29, 2014 14:32
Show Gist options
  • Save kennethryerson/f7d1abcf2633b7c03cf0 to your computer and use it in GitHub Desktop.
Save kennethryerson/f7d1abcf2633b7c03cf0 to your computer and use it in GitHub Desktop.
Set custom baud rate in Linux
#include <termios.h>
#include <sys/ioctl.h>
#define BAUD_RATE 128000 /* custom baud rate */
/* extended termios struct for custom baud rate */
struct termios2 {
tcflag_t c_iflag; /* input mode flags */
tcflag_t c_oflag; /* output mode flags */
tcflag_t c_cflag; /* control mode flags */
tcflag_t c_lflag; /* local mode flags */
cc_t c_line; /* line discipline */
cc_t c_cc[NCCS]; /* control characters */
speed_t c_ispeed; /* input speed */
speed_t c_ospeed; /* output speed */
};
struct termios2 tio;
ioctl(fd, TCGETS2, &tio);
tio.c_cflag &= ~CBAUD;
tio.c_cflag |= CBAUDEX;
tio.c_ispeed = BAUD_RATE;
tio.c_ospeed = BAUD_RATE;
/* do other miscellaneous setup options with the flags here */
ioctl(fd, TCSETS2, &tio);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment