Serial Port Jiggery-Pokery
How to enable and disable serial ports on Ubuntu
Today, I had to fix a bug in some software I’m working on. The details of the bug aren’t that important, apart from the fact that the bug only became apparent when the software was run on a system with one or less serial ports.
Since I’m debugging on Ubuntu, and I have two serial ports (/dev/ttyS0 and /dev/ttyS1), I needed to find a way to temporarily disable one. This is how I achieved it.
In order to configure serial ports on Linux, you’ll need the setserial command. This wasn’t installed when I went looking for it, but a quick call to apt-get (as root) had it installed in no time.
apt-get install setserial
As part of the install it kindly created a configuration file which backed up the known serial ports at the time to /var/lib/setserial/autoserial.conf which came in handy later. Alternatively, you can use setserial to show how ports are configured at any time by using:
setserial -gG /dev/ttyS*
Disabling the Port
To disable a port, set its uart to none as in (as root):
setserial /dev/ttyS1 uart none
If you enumerate your ports now, you should not see /dev/ttyS1 as a valid serial port. I could now proceed to debug my software as I could now reproduce the original bug.
Enabling the port
To re-enable the port, I took the configuration information which the setserial -gG … exported as in (as root):
setserial /dev/ttyS1 uart 16550A port 0x02f8 irq 3 baud_base 115200 spd_normal skip_test
That’s it, job done. Everything back to normal.