screen the most useful program for serial communication since I use it for other things anyway. It’s usually just
screen /dev/ttyS0 <speed>, although the default settings may be different for your device. It also allows you to pipe anything into the session by entering command mode and doing
exec !! <run some program that generates output>.
The main reason why you need any program like
minicom to communicate over a serial port is that the port needs to be set up prior to initiating a connection. If it weren’t set up appropriately, the
echo commands would not do for you what you might have expected. Notice that once you run a program like
minicom, the port is left with the settings that
minicom used. You can query the communication settings using the
stty program like this:
stty < /dev/ttyS0
If you have done it right; after booting the computer and before running any other program like
minicom, the communication settings will be at their default settings. These are probably different than what you will need to make your connection. In this situation, sending the commands
echo to the port will either produce garbage or not work at all.
stty again after using
minicom, and you’ll notice the settings are set to what the program was using.
Minimal serial communication
Basically, two things are needed to have two-way communication through a serial port: 1) configuring the serial port, and 2) opening the pseudo-tty read-write.
The most basic program that I know that does this is
picocom. You can also use a tool like
setserial to set up the port and then interact with it directly from the shell.
I found a way using a shell script here that put
cat as a background process and a while loop that read the user input and
echo it out to the port. I modified it to be more general and it fitted my purpose perfectly.
#!/bin/sh # connect.sh # Usage: # $ connect.sh <device> <port speed> # Example: connect.sh /dev/ttyS0 9600 # Set up device stty -F $1 $2 # Let cat read the device $1 in the background cat $1 & # Capture PID of background process so it is possible to terminate it when done bgPid=$! # Read commands from user, send them to device $1 while read cmd do echo "$cmd" done > $1 # Terminate background read process kill $bgPid