2
\$\begingroup\$

Using a PC and writing some software I want to detect if a user toggles a small electric switch with as little external hardware as possible. I was thinking that maybe I could use a USB to serial cable?
Here are the pins of the serial cable:
enter image description here

Before I open the COM port in my software I measure 0 V from pin 4 to ground.
After I open it I measure -13 V from pin 4 to ground.
I read that: "On many operating systems, including Windows, the DTR line is held low while the serial port is unused and not being controlled by any applications."
So it seems that 0 V is low and -13 V is high, at least on the pins sending info out from the PC.

I was thinking that I could connect this -13 V via the toggle switch to an input pin and thereby detect if the switch has been toggled.

Should this work? Which pin can I use?
I was thinking of maybe using pin 6 Data Set Ready. This sounds like a pin used by e.g. a modem to signal to the PC (that it has data that can be read).
What voltage do I need to apply to pin 6 to turn it high?
-13 V or +13 V? Do I need to place a resistor in between?

\$\endgroup\$
3
  • \$\begingroup\$ Andy, do you have direct access to the serial port registers on the PC? As in, are you operating under DOS or something similar? Or are you running as a "user mode" application under 32-bit or 64-bit Windows? \$\endgroup\$
    – jonk
    Jun 14, 2022 at 9:28
  • \$\begingroup\$ @jonk: User mode. \$\endgroup\$
    – Andy
    Jun 14, 2022 at 10:34
  • 1
    \$\begingroup\$ Okay. Well you then have some limitations due to protected mode behaviors. Only kernel mote driver code gets to deal with the hardware, directly. You should do some research on this, if in Winfows 32-bit or 64-bit. It's been years for me. But I'd be a little bit worried. \$\endgroup\$
    – jonk
    Jun 14, 2022 at 10:40

1 Answer 1

6
\$\begingroup\$

The basic idea is correct even though the details how you think it works are not entirely correct.

Yes, you can connect DTR to DSR via a switch and be able to detect the switch state by reading the status.

It only requires that the DTR is set to "active" state so that DSR pin can either detect active state or default to inactive state. Also remember that what "active" means depends on context, as sometimes signals are active low, and with RS232 the logic level 0 means positive voltage and logic level 1 means negative voltage. The DTR output should never read 0V, unless the USB adapter implemets some kind of power saving mode which shuts down the charge pumps if the port is unused.

Using the handshake pins is not the only way though, you can use the data pins too, if you send a character, you either receive back a transmitted character or not, depending on switch position.

Depending on the programming language and the libraries you use to access the serial port you can choose how you want to do it.

\$\endgroup\$
5
  • 1
    \$\begingroup\$ So if I wire together the transmit pin and the receive pin and write one character I should read one character? Without any handshaking haven taken part? Is pin 3 transmit and pin 2 receive? \$\endgroup\$
    – Andy
    Jun 14, 2022 at 8:23
  • 3
    \$\begingroup\$ @Andy Yes that's correct. And a neat idea. \$\endgroup\$
    – user253751
    Jun 14, 2022 at 8:46
  • 1
    \$\begingroup\$ It will work but you still need to configure the serial port after opening it, and one of the things might be to not expect any handshaking. Your diagram says pin 3 is transmit output and pin 2 is receive input, which would be true to a DTE device. For a DCE pin 3 would be transmit input and pin 2 would be receive output. \$\endgroup\$
    – Justme
    Jun 14, 2022 at 8:46
  • \$\begingroup\$ Wiring together pin 2 and 3 this worked great :-). \$\endgroup\$
    – Andy
    Jun 14, 2022 at 11:06
  • \$\begingroup\$ If using a simple switch, one does not even need to know which is Tx and which is Rx. \$\endgroup\$
    – gbarry
    Jun 14, 2022 at 14:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.