Channels ▼

Al Williams

Dr. Dobb's Bloggers

Simple Digital

July 29, 2014

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.

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.

Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

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.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.
 


Video