Can I use a 9-pin serial port as "GPIO" using ioctl ()?

Is it possible to use COM port in linux window to read switch value? I think this is possible with ioctl (), but I tried a bit to find a comparable example. I need only one input, and it needs to be read only in the HIGH / LOW value. I thought I could just use ioctl to set one of the pins high, and connect the switch between this and the other pin, again using ioctl to read the value of the second pin ()

The rationale is that I have a reliable old server running in the garage, I would like to do something that will print, and say if the garage door is open. Since the server is sitting next to the door, I thought it would be easy just to connect the switch to the back of the COM port (which is not currently in use)

Of course, I could spend a few pounds, save myself from a headache and use an arduino or ESP8266, but now I'm curious!

+5
source share
1 answer

Yes of course you can do it. There are several lines on the serial port that can be used as GPIOs. One of them (on pin9, I think) is RING, which is used in old serial modes to signal incoming calls.

You can read it as follows:

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 create a voltage that you can apply to the ring line. One cheap way to do this is to transfer some stream of alternating bits. For instance. send 0x55 at any baud rate.

Then you can click on the voltage from the TX pin. Separate the positive and negative voltages using two diodes, and be careful when using two capacitors. This will give you positive and negative voltages compatible with the ring line.

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

Generating signal voltages from UART tx-pin

+4
source

Source: https://habr.com/ru/post/1210426/


All Articles