How to Read Serial Port Data on Linux Using the Terminal
When a microcontroller, GPS dongle, or legacy modem spits out bytes, the first place most engineers look is the Linux terminal. You don’t need a heavyweight GUI or exotic libraries – a handful of built‑in tools can pull raw data from /dev/ttyS0, /dev/ttyUSB0, or any other serial node. This guide walks through the steps, from wiring the cable to saving a log file, so you can start watching those streams in real time.
Why the Terminal Is Often Enough
Serial communication is inherently line‑oriented, and the Linux kernel already presents each port as a character device. That means any program that can read a file can also read a serial stream. The terminal’s simplicity reduces latency, avoids extra layers that might corrupt timing, and keeps you in control of every flag – baud rate, parity, stop bits, and flow control.
Moreover, the tools you’ll use (screen, minicom, cat, stty, dd) are part of most distributions. No extra packages, no X‑server required, and you can script them for automated testing or data capture.
Preparing Your Serial Device
Before you type a command, make sure the kernel knows about the device. Plug in a USB‑to‑UART adapter and run:
dmesg | grep tty– you’ll typically seettyUSB0orttyACM0appear.ls -l /dev/tty*– confirms the node exists and shows its permissions.
If you get “Permission denied”, add your user to the dialout group (or uucp on some distros) and re‑login:
sudo usermod -a -G dialout $USERMost modern kernels assign the correct driver automatically, but double‑check that the device isn’t claimed by a modem manager or NetworkManager – those services can hijack the port and drop incoming data.
Basic Commands: screen, minicom, cat
The quickest way to peek at a stream is screen. It opens a full‑screen terminal attached to the port and handles line wrapping for you.
screen /dev/ttyUSB0 115200If the connection looks garbled, press Ctrl‑A then K to kill the session and retry with different parameters. The syntax is screen DEVICE BAUD, where you can replace 115200 with any supported speed.
For a more feature‑rich experience, minicom offers menu‑driven configuration, logging, and even a built‑in script interpreter. After installing (sudo apt install minicom on Debian‑based systems), start it with:
minicom -D /dev/ttyUSB0 -b 9600Press Ctrl‑A Z to open the configuration menu, then navigate to “Serial port setup” to tweak parity, stop bits, and flow control.
If you only need to dump raw bytes, cat works like a charm:
cat /dev/ttyS0Pipe the output into grep or awk for on‑the‑fly filtering, or redirect it to a file for later analysis.
Reading Data with Stty and DD
Before any read operation, the port’s settings must match the device’s expectations. stty lets you script those parameters without launching an interactive program.
# Set 8N1, 115200 bps, no flow controlstty -F /dev/ttyUSB0 115200 cs8 -cstopb -parenb -ixon -ixoff
After configuring, you can use dd to capture a precise number of bytes, which is handy when dealing with binary protocols:
dd if=/dev/ttyUSB0 of=log.bin bs=1 count=1024 status=noneThe bs=1 tells dd to read one byte at a time, while count limits the transfer. Replace 1024 with the exact payload length you expect, or drop count to read until you interrupt with Ctrl‑C.
Tips for Troubleshooting
- Check voltage levels. Most PC serial ports speak RS‑232 (±12 V), whereas USB‑UART adapters use TTL (0–3.3 V or 0–5 V). A level‑shifter prevents corrupted characters.
- Verify flow control. Hardware (RTS/CTS) or software (XON/XOFF) can pause transmission if mismatched. Disable both on the host side with
-crtscts -ixon -ixoffinsttyif the remote device doesn’t support them. - Watch for line‑ending conversion. Some devices send
\r\nwhile others use just\n. Usetr -d '\r'in a pipe to normalize. - Use
odfor hex dumps. If you suspect non‑printable data, pipe the stream throughod -t x1to view each byte in hexadecimal. - Log timestamps. Prefix each line with a time stamp using
awk '{print strftime("%F %T"), $0}'when you need a chronological record.
FAQ
How can I list all serial ports recognized by the kernel?
Run ls /dev/ttyS* /dev/ttyUSB* /dev/ttyACM*. The output shows every built‑in (ttyS) and USB‑based (ttyUSB, ttyACM) node currently available.
Why do I see “Permission denied” when I try to read /dev/ttyUSB0?
The most common cause is that your user isn’t in the group that owns the device (usually dialout). Adding yourself to that group and logging out/in resolves the error.
Can I save the serial output directly to a file while still seeing it on screen?
Yes. Use tee to duplicate the stream: cat /dev/ttyUSB0 | tee capture.log. This prints to the terminal and writes everything to capture.log simultaneously.
What’s the practical difference between screen and minicom?
screen is lightweight, ideal for quick checks, and requires only the device name and baud rate. minicom offers a menu‑driven setup, logging, and can handle multiple connections, making it better for long‑running sessions or when you need to tweak parity and flow control on the fly.