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:
fp=fopen(cfg.port,"r"); if (!fp) return 0; // huh? ioctl(fileno(fp),TIOCMGET,&modemstatus); bit=1; if (modemstatus & TIOCM_CAR) bit=0; if (bit ^ cfg.comsense) // Switch is off else // Switch is on
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.
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.