Commands
View the activity
cat /proc/tty/driver/serial
Example output:
serinfo:1.0 driver revision:
0: uart:16550A port:00004310 irq:32 tx:70610 rx:70683 RTS|CTS|DTR|DSR|CD|RI
1: uart:16550A port:00004318 irq:32 tx:6352 rx:12704 RTS|CTS|DTR|DSR|CD|RI
2: uart:16550A port:00004320 irq:32 tx:16314 rx:0 RTS|DTR|DSR|CD|RI
3: uart:16550A port:00004328 irq:32 tx:132 rx:0 DSR|CD|RI
4: uart:16550A port:00004300 irq:32 tx:0 rx:0 DSR|CD|RI
5: uart:16550A port:00004308 irq:32 tx:0 rx:0 DSR|CD|RI
Get current port settings
stty -F /dev/ttyS0 -a
Example output
speed 9600 baud; rows 0; columns 0; line = 0;
intr = ^C; quit = ^; erase = ^?; kill = ^U; eof = ^D; eol = <undef>; eol2 = <undef>; swtch = <undef>;
start = ^Q; stop = ^S; susp = ^Z; rprnt = ^R; werase = ^W; lnext = ^V; discard = ^O; min = 1; time = 0;
-parenb -parodd -cmspar cs8 hupcl -cstopb cread -clocal -crtscts
-ignbrk -brkint ignpar -parmrk -inpck -istrip -inlcr -igncr -icrnl -ixon -ixoff -iuclc -ixany -imaxbel
-iutf8
-opost -olcuc -ocrnl -onlcr -onocr -onlret -ofill -ofdel nl0 cr0 tab0 bs0 vt0 ff0
-isig -icanon -iexten -echo -echoe -echok -echonl -noflsh -xcase -tostop -echoprt -echoctl -echoke -flusho
-extproc
Ownership
The c_cflag
member contains two options that should always be enabled, CLOCAL
and CREAD
. These will ensure that your program does not become the 'owner' of the port subject to sporatic job control and hangup signals, and also that the serial interface driver will read incoming data bytes.
#!/bin/bash
set -x
setup_com_port() {
id=$1
# set baud rate, disable modem control signals
stty -F "/dev/ttyS$id" 9600 eof ^D min 0 time 8 clocal -ignpar inpck parenb
}
if [ -n "$1" ]; then
setup_com_port $1
else
for i in $(seq 0 5); do
setup_com_port $i
done
fi
Magical touch with the screen utility
#!/bin/bash
# touch ports
screen -S com5 -d -m /dev/ttyS4
screen -S com7 -d -m /dev/ttyS6
screen -S com8 -d -m /dev/ttyS7
# kill screen sessions
screen -S com5 -X kill
screen -S com7 -X kill
screen -S com8 -X kill
Links:
Monitoring
I did a few silly commands to check the booting process and analyze when something is changed in the settings and how it affects the communication
while true; do sudo cat /proc/tty/driver/serial | grep port:00004310 | ts '[%Y-%m-%d %H:%M:%S]' >> /var/log/serial.log; sleep 1; done &
# example output:
# [2024-10-17 10:21:12] 6: uart:16550A port:00004310 irq:32 tx:0 rx:0 RTS|CTS|DTR|DSR|CD|RI
# sudo stty -F /dev/ttyS0 -a > working_serial_state
while true; do sudo stty -F /dev/ttyS0 -a | diff working_serial_state - | ts '[%Y-%m-%d %H:%M:%S]' >> /var/log/serial2.log; sleep 1; done &
# example output:
# [2024-10-17 10:21:11] 4,6c4,6
# [2024-10-17 10:21:11] < werase = ^W; lnext = ^V; discard = ^O; min = 0; time = 8;
# [2024-10-17 10:21:11] < parenb -parodd -cmspar cs8 -hupcl -cstopb cread clocal -crtscts
# [2024-10-17 10:21:11] < -ignbrk -brkint -ignpar -parmrk inpck -istrip -inlcr -igncr -icrnl -ixon -ixoff
# [2024-10-17 10:21:11] ---
# [2024-10-17 10:21:11] > werase = ^W; lnext = ^V; discard = ^O; min = 100; time = 2;
# [2024-10-17 10:21:11] > -parenb -parodd -cmspar cs8 -hupcl -cstopb cread clocal -crtscts
# [2024-10-17 10:21:11] > -ignbrk brkint ignpar -parmrk -inpck -istrip -inlcr -igncr -icrnl ixon -ixoff
# [2024-10-17 10:21:11] 9c9
# [2024-10-17 10:21:11] < -isig -icanon -iexten -echo echoe echok -echonl -noflsh -xcase -tostop -echoprt
# [2024-10-17 10:21:11] ---
# [2024-10-17 10:21:11] > -isig -icanon iexten -echo echoe echok -echonl -noflsh -xcase -tostop -echoprt
while true; do ls -1 /dev/ttyS* | xargs -I{} sudo lsof -X {} | ts '[%Y-%m-%d %H:%M:%S]' >> /var/log/serial3.log; sleep 3; done &
Reference
- Linux kernel boot parameters
- Remapping with udev rules:
ACTION=="add" KERNEL=="ttyS2" NAME="ttyS1"
- Rename
mv /dev/ttyS2 /dev/ttyS1
- ModemManager can screw you over (documentation)
- Inspect serial port on Linux
- Sniff serial port on Linux
- Monitor serial port on Linux