Open Article in New Tab
Three ways to send and receive data from Serial devices in Linux
Identify the serial device
There are a few ways to find the serial adapter connected to the computer.
For USB serial devices like our PL2303-DB9 USB Serial Adapter, connect the adapter to the computer, then open a terminal and run:
dmesg | tail
Look for output like:
[54221.132226] usb 1-2.2: pl2303 converter now attached to ttyUSB0
This indicates the serial adpater can be accessed at /dev/ttyUSB0
Alternatively, you can run the following command to print out the newest created serial adapter:
ls -tr1 /dev/tty* | tail -n 1
This will output something similar to:
/dev/ttyUSB0
Screen
Screen is a full-screen window manager capable of connecting to serial devices.
To connect to the serial device at 9600 baud (one of the more common buad rates) run the following command in the terminal:
screen /dev/ttyUSB0 9600
From here you can type text into the terminal to transmit to the connected device, or read output from the device.
Screen commands consist of "C-a" or ctrl+a (holding down the control key, and then pressing the 'a' key, then releasing both), to enter the command input mode, then commands may be entered with another key stroke.
To disconnect from the session ( while leaving it open ): Ctrl+a, Ctrl+d, you can then use the following to reattach to the session.
screen -r
To quit screen and close connection: ctrl+a, k. Then 'y' when prompted to kill the session.
Minicom is a serial communication program that has a more user-friendly interface than screen, however with more options comes more choices.
To connect to the serial device at 9600 baud with minicom, from the terminal:
minicom -b 9600 -D /dev/ttyUSB0
Minicom will provide more output when connected, in my case:
Welcome to minicom 2.7.1
OPTIONS: I18n
Compiled on Jul 22 2021, 00:00:00.
Port /dev/ttyUSB0, 10:42:24
Press CTRL-A Z for help on special keys
From here you can type into the terminal to send to connected serial device.
To quit, similar to Screen, the commands are accepted through key commands. Ctrl+A, z, x will disconnect and close the application.
Minicom may store presets and can be configured using the setup mode, but it is not necessary for basic useage.
minicom -s
Redirecting echo
We can directly write to the serial device from bash by redirecting the output of the 'echo' command to the serial device. First we need to set the serial port speed and then send the text "helloworld" to the serial device:
stty -F /dev/ttyUSB0 speed 9600
echo helloworld >/dev/ttyUSB0
This method will not print responses from the serial device, and is most useful for one-way communication with a device, for example sending a string of text to an LED matrix display.
Troubleshooting Connectivity
Permission denied
Most permission denied error messages can be resolved by adding your user account to the 'dialout' group or the group with control of the serial port. We can check the group ownership of the port with the following:
ls -l /dev/ttyUSB0
crw-rw----. 1 root dialout 188, 0 Jan 21 11:05 /dev/ttyUSB0
In this case I would need to add my user account to the 'dialout' group:
sudo usermod -a $(whoami) -G dialout
`$(whoami)` can be replaced with your user name. Once added to the group, close and reopen the terminal, log out and back in, or restart the computer for the changes to take effect.
Scrambled Text
This is most often caused by incorrect baud rate settings on either the computer or device. Most serial devices like network routers and switches will have either a sticker with the serial port settings, or these can be found in the user manual for the device. For device with additional settings minicom may prove to be the easiest to configure.