Al Williams

Dr. Dobb's Bloggers

Simple Digital

July 29, 2014

It seemed a waste to put an I/O board in the system just for one switch closure reading.

For some reason, I’ve worked on a number of exhibits at museums and theme parks. I never went out looking for those tasks, but they somehow seem to find me. These usually aren’t very technically challenging, but they do usually have beautiful custom cases and even without their electronics are works of art.

A few years ago, I did a museum system that was essentially a self-contained PC, but needed one digital input. It seemed a waste to put an I/O board in the system just for one switch closure reading. I’ve seen people wire across a keyboard pin to do this, but that seems like such an ugly hack and it is easy to imagine keyboard driver behavior getting in the way (auto repeat, for example).

My solution was simple: Get a normal serial-to-TTL adapter (either USB or one for a conventional serial port) and use the carrier detect input. In fact, you can probably squeeze the input information out of several serial port pins, but in this case I just needed one.

The system used Windows (a stipulation from the user), but to make my life easier, I developed under Linux and depended on using Cygwin or MinGW to provide the POSIX-like API so I didn’t have to deal with the Windows communications stack directly.

Switching the input to ground should be sufficient, but you can test your specific hardware. Most devices will output plus and minus 5V or more, and if you prefer you can force one of the output pins to a given state and use it as your switch closure.

The system reads its configuration from a CSV file and populates a global structure named cfg. Inside are two items of interest to the serial port code: the port name (cfg.port) and whether a DCD active means the switch is closed or open (cfg.comsense). Here’s the code:

Of course, later, there is an fclose(fp). The code is really pretty simple (just make sure to include sys/ioctl.h). The fileno() macro converts the C library FILE pointer to the underlying file number that ioctl expects. The ioctl() call populates modemstatus, and the TIOCM_CAR bit corresponds to carrier detect.

By using bit^cfg.comsense, I can “flip” the sense of the if/else statement without recompiling. A change in the configuration file controls which way the program measures the switch.

Sometimes simple solutions are the best. TTL adapters are cheap (well under $5 if you shop) and plentiful. Many of them are tiny and can be easily built into the equipment. I wouldn’t want to read dozens of inputs like this, but for a quick and dirty switch connection to a PC, it really does the trick.

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task.
However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

Video

This month's Dr. Dobb's Journal

This month,
Dr. Dobb's Journal is devoted to mobile programming. We introduce you to Apple's new Swift programming language, discuss the perils of being the third-most-popular mobile platform, revisit SQLite on Android
, and much more!