How to Set Serial Port Parameters in Ubuntu: A Quick Guide
Why Serial Port Settings Matter
Even in a world dominated by USB and wireless connections, serial ports still power a surprising number of devices—industrial controllers, Arduino boards, and legacy equipment. If those gadgets speak the language of ttyS0 or ttyUSB0, you’ll quickly discover that the default settings (9600 bps, 8‑N‑1) may not match what the hardware expects. Mismatched baud rates, parity bits, or stop bits can turn a perfectly good connection into a stream of gibberish.
Basic Terminology at a Glance
- Baud rate: bits per second, e.g., 115200.
- Data bits: usually 7 or 8.
- Parity: none, even, odd—used for simple error checking.
- Stop bits: 1 or 2, indicating the end of a byte.
- Flow control: RTS/CTS or XON/XOFF, managing data flow.
Getting Your Device Visible
First things first: make sure Ubuntu actually sees the serial device. Plug it in, then run:
ls -l /dev/tty*If you spot something like /dev/ttyUSB0 (USB‑to‑serial adapter) or /dev/ttyS0 (built‑in COM port), you’re ready to proceed. No entry? Try dmesg | tail to check whether the kernel recognized the hardware.
Choosing a Tool
Ubuntu ships with a few command‑line utilities that can tweak serial parameters:
- stty – classic, works on any POSIX system.
- setserial – more focused on legacy built‑in ports.
- minicom or screen – interactive programs that also let you test communication.
For a quick change, stty is usually enough.
Setting Parameters with stty
Assume the device appears as /dev/ttyUSB0. To configure it to 115200 bps, 8 data bits, no parity, 1 stop bit, run:
sudo stty -F /dev/ttyUSB0 115200 cs8 -cstopb -parenbBreak that command down:
-F /dev/ttyUSB0tellssttywhich device to act on.115200sets the baud rate.cs8selects 8 data bits.-cstopbforces a single stop bit (omit the dash for two).-parenbdisables parity; useparenbfor even parity.
Adding Flow Control
If your hardware requires RTS/CTS hardware flow control, simply tack on crtscts:
sudo stty -F /dev/ttyUSB0 crtsctsSoftware flow control (XON/XOFF) is enabled with ixon ixoff instead.
Saving Settings Across Reboots
Changes made with stty are volatile—they disappear after a power cycle. To make them stick, create a small systemd service or place the command in /etc/rc.local. Here’s a minimal service file:
[Unit]Description=Configure Serial Port /dev/ttyUSB0
[Service]
Type=oneshot
ExecStart=/bin/stty -F /dev/ttyUSB0 115200 cs8 -cstopb -parenb crtscts
[Install]
WantedBy=multi-user.target
Save this as /etc/systemd/system/serial-config.service, then enable it:
sudo systemctl daemon-reloadsudo systemctl enable --now serial-config.service
Testing the Connection
Once the parameters are set, open a quick test session with screen:
screen /dev/ttyUSB0 115200If you see readable output, you’ve hit the sweet spot. Press Ctrl‑A then K to exit; confirm with y.
Common Pitfalls and How to Fix Them
- Permission denied – add your user to the
dialoutgroup:sudo usermod -a -G dialout $USER, then log out and back in. - Garbage characters – double‑check the baud rate and parity; a mismatch is the usual culprit.
- Device disappears after reboot – ensure the USB‑to‑serial adapter’s firmware isn’t being reset by a BIOS setting; sometimes disabling “Fast Boot” helps.
When stty Isn’t Enough
Some embedded boards expose additional control lines that stty can’t manipulate. In those cases, the setserial utility may be required, especially for built‑in COM ports. A typical invocation looks like:
sudo setserial /dev/ttyS0 uart 16550A port 0x3F8 irq 4Refer to the device’s datasheet for the correct uart, port, and irq values.
Wrapping Up
Configuring serial port parameters in Ubuntu doesn’t have to be a black‑box ordeal. With a handful of commands—stty for most cases, setserial for the stubborn ones—you can match any device’s expectations, persist the settings, and get back to productive work. The next time a piece of hardware throws “framing error” warnings, you’ll know exactly where to look.