Take the 2-minute tour ×

I want to communicate over my serial port on Linux to a device with a non-standard-baud rate that is not defined in termios.h.

I tried the "baud rate aliasing"-method from this post but when I execute my C-program (I’ve named it "testprogram"), Linux says "testprogram sets custom speed on ttyS0. This is deprecated."

I did some search on google and it seems that there is another (newer?) method to change the baud rate to a non-standard-value: On http://sourceware.org/ml/libc-help/2009-06/msg00016.html the author says that the c_flag of struct termios must be OR’d with BOTHER (=CBAUDEX | B0). With this method the baud rates are set directly in the c_ispeed and c_ospeed-members of the struct termios. However, I don’t know how I use this method in my C-program. Like the author said, there is no BOTHER defined/available when I include termios.h, so what should be done to set the baud rate this way?

How can I set the baud rate to a non-standard-value without changing the kernel?

Thanks in advance!

share|improve this question
The answer is going to be hardware-dependent. What kind of serial hardware are you using? The PC 16550 UART works by dividing an input clock by an integer divisor. I doubt that has the precision to hit 125k given that it already has to hit 115.2k per the standard. –  Andy Ross Sep 28 '12 at 19:47
add comment

7 Answers

You can set custom baud rate using stty command on linux. For example, to set custom baud rate of 567890 on your serial port /dev/ttyX0 use the command

stty -F /dev/ttyX0 567890
share|improve this answer
add comment

there is an serial IO chip on your mother board's CPU (UART 16650). This chip uses 8-bit port as control and data bus, thus you can issue command to it through writing to this chip through control /data bus.

Usually, an application did the following steps on serial port

  1. set baud rate/ parity/ encoding/ flow control/ staring / ending sequence length during program start. This setup can be done via ioctl to serial device or 'stty' command. In fact, the stty command uses ioctl to that serial device.
  2. write characters of data to the serail device and the driver will writing data charaters to UART chip through its 8bit data bus.

In short, you can specify baud rate only in STTY command, then all other options would be kept as default and it should enough to connect to ohter devices.

share|improve this answer
add comment

BOTHER appears to be available from <asm/termios.h> on Linux. Pulling the definition from there is going to be wildly non-portable, but I assume this API is non-portable anyway, so it's probably no big loss.

share|improve this answer
add comment

I noticed the same thing about BOTHER not being defined. Like Jamey Sharp said, you can find it in <asm/termios.h>. Just a forewarning, I think I ran into problems including both it and the regular <termios.h> file at the same time.

Aside from that, I found with the glibc I have, it still didn't work because glibc's tcsetattr was doing the ioctl for the old-style version of struct termios which doesn't pay attention to the speed setting. I was able to set a custom speed by manually doing an ioctl with the new style termios2 struct, which should also be available by including <asm/termios.h>:

struct termios2 tio;
ioctl(fd, TCGETS2, &tio);
tio.c_cflag &= ~CBAUD;
tio.c_cflag |= BOTHER;
tio.c_ispeed = 12345;
tio.c_ospeed = 12345;
ioctl(fd, TCSETS2, &tio);
share|improve this answer
add comment

For Mac users (possibly also for some Linux distros)

stty ospeed 999999

stty ispeed 999999

share|improve this answer
add comment

dougg3 has this pretty much (I can't comment there). The main additional thing you need to know is the headers which don't conflict with each other but do provide the correct prototypes. The answer is

#include <stropts.h>
#include <asm/termios.h>

After that you can use dougg3's code, preferably with error checking round the ioctl() calls. You will probably need to put this in a separate .c file to the rest of your serial port code which uses the normal termios to set other parameters. Doing POSIX manipulations first, then this to set the custom speed, works fine on the built-in UART of the Raspberry Pi to get a 250k baud rate.

share|improve this answer
add comment

Well, if you have a way to do it in the terminal, and don't need to actually use this often (it sounds like a very specific situation) then system("[command]") acts just like typing [command] in the terminal.

share|improve this answer
add comment

Your Answer


