Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Possible to use a 9 Pin Serial port as "GPIO" using ioctl()?

Tags:

c

linux

ioctl

Is it possible to use the COM port on a linux box to read the value of a switch? I think that it should be possible with ioctl(), but i have struggled a little to find a comparable example. I only need one input, and only need it to read in a HIGH/LOW value. I thought I could just use ioctl to set one of the pins high, and wire a switch between that and another pin, again using ioctl to read the value of the second pin()

The rationale is that I have a trusty old server running in the garage, I'd like to make something that would bleep and tell if the garage door is open, Since the server sits next to the door, I thought it would be simple to just wire a switch into the back of the com port (which is currently unused)

Granted, I could spend a few pounds, save myself a headache and use an arduino or an ESP8266, but this has now got me curious!

like image 258
phatmanace Avatar asked Jan 05 '15 22:01

phatmanace


1 Answers

Yes, sure you can do this. There are a number of lines on the serial port that can be used as GPIO. One of them (on pin9 I think) is RING which is used on old serial modes to signal incoming calls.

You can read it like this:

unsigned mask = TIOCM_RNG;
unsigned status;
int fd; // your serial port file descriptor.

/* Get current status of the control lines in mask */
if (ioctl(fd, TIOCMGET, &status) == -1) {
    perror("ioctl(TIOCMGET)");
}

/* now evaluate status */

You also need to generate a voltage that you can apply to the ring line. One cheap way to do this is to transmit some stream of alternating bits. E.g. send 0x55 at any baud-rate.

You can then tap the voltage from the TX-pin. Split the positive and negative voltages using two diodes and buffer them using two capacitors. That will give you positive and negative voltages compatible with the ring-line.

Here is a schematic that should work. Adjust diodes and capacitors to taste, but don't go crazy with the capacitor. 10µF should be the maximum.

Generating signal voltages from UART tx-pin

like image 192
Nils Pipenbrinck Avatar answered Sep 24 '22 16:09

Nils Pipenbrinck