[ltp] Serial port switch
Richard Neill
linux-thinkpad@linux-thinkpad.org
Wed, 12 Mar 2008 02:17:38 +0000
Dear All,
Just thought the following might be useful to someone wanting to do some
really trivial hardware hacking. I'm using it to connect a reed-switch
to an old ThinkPad, to make a networked burglar alarm.
The old-fashioned Serial port (or a USB-serial adaptor) has a couple of
status lines which are really easy to monitor and interface with. It's
also reasonable electrically "tough", being designed to work with large
voltage-swings, and long cables.
statserial /dev/ttyS0 gives the following listing, for a
non-connected port:
-------------------
Device: /dev/ttyS0
Signal Pin Pin Direction Status Full
Name (25) (9) (computer) Name
----- --- --- --------- ------ -----
FG 1 - - - Frame Ground
TxD 2 3 out - Transmit Data
RxD 3 2 in - Receive Data
RTS 4 7 out 1 Request To Send
CTS 5 8 in 0 Clear To Send
DSR 6 6 in 0 Data Set Ready
GND 7 5 - - Signal Ground
DCD 8 1 in 0 Data Carrier Detect
DTR 20 4 out 1 Data Terminal Ready
RI 22 9 in 0 Ring Indicator
--------------------
Normal data flows through the TxD and RxD lines, which we don't use
here. However, the status lines are essentially logic levels(*).
So, if we connect a switch between pin 4 (RTS,out,logic 1), and pin 9
(RI,in,floats at 0), then we can very easily read the state of the
switch. The circuit really is this simple:
[. .]
[. 9] -----------------------O--/ O----|
[. .] |
[. 4] ---------------------------------|
[. .]
Female switch
DB9 connector
When the switch is closed, RI == 1; when it is open, RI == 0.
This makes it really easy to hook up a switch to a laptop, and monitor
all sorts of things. I'm using it to monitor something else, with a reed
switch, and a piece of thread connected to the magnet.
In practice, we have up to 4 inputs and 1 outputs available, as well as
a small amount of unregulated power.
[For more advanced or faster I/O, try the parallel port, or an
FTDI-USB245M USB device, which is about $15]
Scripting statserial is a bit of a pain, since it sends ANSI-escape
characters to the terminal. Here's a kludge that works, and prints
either 0 or 1.
statserial -n /dev/ttyS0 | cat -vT | \
grep "Ring Indicator" | cut -b 29
Hope this is of interest.
Richard
(*)Serial ports use reverse logic: 1 = negative, 0 = positive. Also, the
voltage swing can be anything in the range +/-5V to +/- 30V.