[ home ]

Setting a custom serial baudrate on Linux

This page describes steps necessary to set a custom baudrate (bitrate) for serial ports on Linux.

Tested with FTDI FT230X USB/serial bridge, Arch Linux kernel 4.12.13-1-ARCH on AMD64.

DISCLAIMER: I didn't figure this out or anything. Text here just serves as a reminder for myself.

What to want, and how to get it

You want a custom baudrate, say B

Install setserial (package setserial) and stty (package coreutils)

Find out which base-baudrate your serial device uses:

    # setserial -a /dev/ttyUSB0
    /dev/ttyUSB0, Line 0, UART: unknown, Port: 0x0000, IRQ: 0
            Baud_base: 24000000, ...

(So, in this example, the base-baudrate is 24000000.)

Calculate required divisor:

divisor = baud_base / B

(Example: for a desired baudrate of 16000, the divisor would be 1500, since 24000000 / 16000 = 1500.)

Set divisor, and configure the kernel to use this custom divisor/baudrate whenever a baudrate of 38400 (B38400) is selected:

    setserial /dev/ttyUSB0 divisor 1500 spd_cust

Use 38400 as pseudo-baudrate in your application. For example:

    minicom -b 38400


    stty -F /dev/ttyUSB0 38400

Note that you can alter the baudrate on-the-fly while Minicom has the serial port open, by issueing both the setserial and stty commands.

I use the following 'setbps' script to set a desired baudrate:

    #!/usr/bin/env bash
    [ "$2" = "" ]  &&  { echo 'use args <dev> <bps>'; exit; }
    setserial  $dev  divisor  $[ $BASEBPS / $bps ]  spd_cust
    stty -F $dev 38400  2> /dev/null