Using serial port to realize switch input of common computer

Source: Internet
Author: User
Tags bool switches

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.