HOWTO: Test RS232 and RS485 adapters

bobmc

Member

Reaction score: 45
Messages: 90

Once upon a time, PCs needed RS232 serial ports for modems, printers, and terminals. These use-cases are mostly obsolete but now they can be used for factory PLCs, IoT devices, and asset management. RS485 can operate sensor hundreds of feet from the host (master) computer.

Many PCs omit serial DB9 connectors to make room for USB. My mini-ITX has no DB9 but the main board has a UART that FreeBSD 12.2 detects. I tried to test it with picocom, simpler than minicom, from the Ports package. It worked with the superuser account but my user account got "permission denied". An USB RS232 adapter gave the same result. It should have worked for users in the wheel group.

There is some forum chatter about RS485 devices. I also studied the system code without finding the official way to activate the serial device for applications. So I wrote the following script which must be run after power is applied or USB adapter is plugged in.
Code:
#!/bin/sh
echo
echo "----- fix_tty.sh:  set permissions for RS232 or RS485 -----"
uname -mrs
echo "check if RS232 or RS485 uart is present"
ls -lga  /dev/ttyu0
if  [ -e /dev/ttyu0 ]; then
    echo  "has serial port uart"
    chmod  g+rw  /dev/ttyu*
else
    echo  "no serial port 0"
fi
ls -lga /dev/ttyu*
echo  "--------------"

echo "check if USB RS232 or RS485 adapter is present"
ls -lga  /dev/ttyU0
if  [ -e /dev/ttyU0 ]; then
    echo  "has USB serial adapter"
    chmod  g+rw  /dev/ttyU*
else
    echo  "no USB serial adapter"
fi
ls -lga /dev/ttyU*
echo "======== bye ======="
echo
The script updates the group permission of the first detected motherboard UART and first USB UART (e.g. CH340). It must be run by the superuser from /root/fix_tty.sh. Then run
Code:
picocom /dev/ttyU0 # for example as regular user

LOOPBACK TEST: Join DB9 pins 2 and 3, run picocom, type something.
 

SirDice

Administrator
Staff member
Administrator
Moderator

Reaction score: 12,025
Messages: 38,480

Many PCs omit serial DB9 connectors to make room for USB
Interesting titbit, these are actually DE-9 connectors, not DB-9. But it's been wrongly named so many times they're now sold as DB-9.
Because personal computers first used DB-25 connectors for their serial and parallel ports, when the PC serial port began to use 9-pin connectors, they were often labeled as DB-9 instead of DE-9 connectors, due to an ignorance of the fact that B represented a shell size. It is now common to see DE-9 connectors sold as DB-9 connectors. DB-9 nearly always refers to a 9-pin connector with an E size shell.
 
Top