Ordinary computers do not have a generic input outlet (GPIO), but sometimes I would like to enter a switch quantity.
For example, let the user pull the pull rope switch to start an application, such as a contact switch to determine whether the door is open or closed, such as ....
Demand is so simple, you are not willing to spend dozens of yuan to buy a single chip, not to mention PCI expansion cards, PLC and so on. What do you do?
There's a way! The simplest use of the serial port can be achieved.
Principle:
Serial PIN4[DTR] and Pin7[rts] can output +6v (OK, your computer is not necessarily a +6v, but certainly greater than +3v on it), the output is connected to PIN1[DCD, PIN6[DSR], pin8[cts], On the PC can be detected, so as to achieve the switch input.
Core code:
Output voltage to pin
serialport.dtrenable = true;
Serialport.rtsenable = true;
Determine if PIN has voltage input
bool cd = serialport.cdholding;
BOOL DSR = serialport.dsrholding;
bool cts = serialport.ctsholding;
Know the principle, the rest is good to do.
First is the wiring: (You need a 9-pin serial mother, a number of switches, wires, electric soldering iron)
As shown in figure, I have 3 switches, 4 as a common pin, 1, 6, 8, respectively, with a switch for input signal. Of course, you can only take one switch.
(Computer motherboard with the needle is the male head, wiring to use the mother or not plug into the computer, if not can go to the electronic city to buy a very cheap, the above number is very small to look carefully
There is no serial port notebook can taobao buy a USB to the serial line can also be