Connect with us

Simple PC Serial Port Help

Discussion in 'Electronic Basics' started by [email protected], Nov 16, 2006.

Scroll to continue with content
  1. Guest

    Please point me toward any applicable FAQs or other groups if I am
    breaking protocol or in the wrong place, thanks. :)

    That said, I am trying to find some information on building a very
    simple circuit that combines a momentary contact switch and my PC's
    serial port. I have a very basic understanding of electronics,
    generally speaking, but I am definitely a "software guy." ;) Once
    connected, I'd like to be able to open the serial port and query (or
    receive) the current state of the switch. The rest of my project can
    be handled in software (for now) so I'd like to keep everything on the
    hardware side as simple as possible.

    Googling, I find a good number of circuits involving using the PC
    serial port to control the state of LEDs/relays/etc. but not much in
    the way of simple receiving of data. I'm not opposed to using the
    parallel port, I'd just prefer to use serial for this project, unless
    there is a significant advantage (lower cost, simpler design). I'm
    also not against using a USB port, but I am assuming that involves more
    complexity on the hardware side as well as the software side.

    Anyway, thanks for any links or information you can provide. I'd be
    glad to provide the final design and example C code for my project when
    I get it going, if that is useful to anyone. :)

  2. Chris

    Chris Guest

    Hi, Oz. The serial port is the easiest place to accomplish your task,
    and s.e.b. is the right place to ask (There are no questions too simple
    or basic for s.e.b. -- just no homework problems unless you've at least
    made a good effort, please ;-)

    Having said that, your best bet would be to use a SPDT switch, with the
    common going to one of the inputs (like RxD), and the N.O. and N.C.
    contacts going to two outputs (say, TxD and CTS). Make one output high
    and the other low. Then, when you switch, the logic state at the input
    will change. Try to use a switch rated for dry contact (<5mA), also
    called a logic level switch. View your switch hookup here in fixed
    font or M$ Notepad):
    | Serial Port
    | .-----------< TxD
    | |
    | | SW1
    | '--o
    | __--o----> RxD
    | .--o
    | |
    | |
    | '-----------< CTS
    (created by AACircuit v1.28.6 beta 04/19/05

    This is trivial in DOS -- just input and output ports in BASIC, C, or
    any other language that can access hardware directly. The Lakeview
    Research site has helpful information on Windows .dlls and drivers you
    might require if you're using a Windows programming language like
    Visual C++.

    Good luck
  3. Randy Day

    Randy Day Guest

    Build a loopback tester with the switch in the loop.
    If you transmit some data, and you receive the same
    data back, your switch is closed.

  4. jasen

    jasen Guest

    put the switch between the DTR and DSR pins... you should be able to tell
    the state of the switch by turning DTR on and reading DSR.

    in linux you open the port and use an IOCTL, to get the DSR state, in DOS
    it's a BIOS call, or you can just read the io port, dunno about windows.
  5. Bob Masta

    Bob Masta Guest

    In addition to all the good info from the other responses, one general
    source of port info is Lakeview Research at

    I personally have always preferred the parallel port, but that is
    no longer easy to use on any system later than Win9x, without
    a special Ring 0 driver. I expect that even the serial port's days
    are numbered as well. Ahh, progress!!!!

    Best regards,

    Bob Masta

    D A Q A R T A
    Data AcQuisition And Real-Time Analysis
    Home of DaqGen, the FREEWARE signal generator
  6. Chris

    Chris Guest

    Chris wrote:
    Forgot to provide the link:

    "Serial Port Complete" by Jan Axelson of LVR is a great resource on
    hacking the serial port. It has a CD which includes numerous
    programming examples in many languages, as well as the Windows files
    you'll need. You can purchase the book from the website or many
    hobbyist resources such as Jameco. It's also available at many

    Good luck
  7. Rich Grise

    Rich Grise Guest

    I'd do almost this, except I wouldn't use TxD and RxD - they go through
    the UART and are expected to be a serial data stream, with start bits
    and all that.

    Just use, lessee...

    So, you'd use your control register to output, say, a high on DTR, and
    a low on RTS, and come in on your choice of DSR or CTS:
    Then, you just poll the status byte when you want to check the

    (on that webpage, the computer's on the left, so you really should
    look at a mirror image of the ASCII art above.)

    Have Fun!
  8. Chris

    Chris Guest

    Yup. Sorry, Rich -- I first did this more than a quarter century ago
    with a footswitch on a tank Kaypro II with Z80 assembler -- I forgot
    the details. Ah, the joys of that monster 9" green screen!

    You're right, the OP should use control signals, they're immediately
    addressable from most UARTs including the PC. As I remember, I'd also
    have occasional problems with resistive switch contacts. Much better
    to just assert a "1" on one output control bit, and a "0" on the other,
    and use a SPDT switch to signal.


  9. You need some basic understanding of the serial port. All you need for that
    you can find in
    chapters on RS-232 interfacing. You need to know which (two) lines are meant
    to transmit and receive data and what modem controll- and sense lines you
    have available. The original use of the latter lines is not important, but
    you must distingguish inputs from outputs.

    Next, you did not tell what OS you're using. In plain old DOS, you can
    access the UART directly. In W95, W98 and WME you can do so as well. For
    WNT, W2000 and WXP you'd better find a driver or use a language that
    supports serial data communication. For Linux, you need to handle the TTY
    controls. Some others told you already how you can connect the switch.
    Beware! An open input line has no defined logical value so make sure it is
    connected to either a high- or a low output line.

    Now you need to decide what you really want to achive. So do you want to
    look at the switch after asking the user a question or do you decide for
    yourself when you want to read out the switch or do you want to catch every
    change the switch makes? The first two cases are simple. You poll whenever
    you need to. I'd go for the single pole, single throw switch that can
    (dis)connect TxD and RxD. Send a byte, for instance 0x55, and check whether
    the byte has been received or not. If you want to catch every change, you
    need to poll continuously or use an interrupt. I'd go for the interrupt, but
    being a programmer, you will know what to do.

    petrus bitbyter
  10. Si Ballenger

    Si Ballenger Guest

    For the serial port, connect the tx to the rx thru the switch.
    Then send some bytes and if the switch is closed, the bytes will
    be received. For the parallel port, below is a simple setup.
Ask a Question
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.
Electronics Point Logo
Continue to site
Quote of the day