***Title - Mystery Black Box ***Date - 2010-01-30 ***Author - Richard Marshall A friend of mine at work discovered a mysterious black box attached to one of our internal systems a few months ago. It was a simple little box with a red LED and a push button connected via a RS-232 cable to the server. The light was not on, nor did it turn on when the button was pushed. Additionally nothing seemed to happen on the system when the button was pushed. While not quite as "magical" this whole thing reminded us of the "Magic/More Magic" switch story (http://catb.org/jargon/html/magic-story.html). At that point we just let it be a mystery and went about our business. A couple of weeks ago the box came up in conversation and it was decided to figure out what exactly was in this box so we promptly walked over the datacenter and retrieved said box. Photo of box connected to box in datacenter: http://www.flickr.com/photos/thejof/3888283503/ Turns out both the box and the rs-232 cable connecting it to the computer were purchased from radioshack which is somewhat amusing. Inside is a mess of wire pulled from a cat-5 utp cable soldered to the switch, LED, and DE-9 connector (which is hot glued to the case), obviously a quick one off construction. The switch it self is a MOM-ON SPDT with pins 1 and 4 of the DE-9 connected to the input. The ON pin connected to the LED which is in turn connected to pin 5. Finally the MOM pin is connected to pin 8. From this I gathered that pin 5 is probably the ground and the LED would be in an on state with the button not pushed and when pushed pin 1 and 4 would be jumped to pin 8. A picture is worth a thousand words so here is a diagram: http://dropbox.easytospell.net/image/365/365.png Since I don't know the RS-232 DE-9 pinout from memory the wonder that is wikipedia prevailed: http://en.wikipedia.org/wiki/RS-232#Pinouts The relevant pins are as follows: 1 - DCD (Data Carrier Detect) 4 - DTR (Data Terminal Ready) 5 - GND (Common Ground) 8 - CTS (Clear to Send) In the default state the device is jumping pins 1 and 4 which would let the serial port driver think there is a device to talk to. Additionally pins 1/4 are shorted to ground which powers the LED. When the button is pushed pins 1/4 are jumped to pin 8 which pulls CTS high. Obviously to continue to figure out how this thing works I had to connect it to my computer and see what happened. At first the same thing happened as when it was connected to the system in the datacenter; absolutely nothing. Tried connecting to the serial port with cu (cu -l/dev/ttyS0) and voila the LED turned on. No surprise it turned off when pressing the button, but nothing else was detected with cu. So how does one detect a high state on CTS? A quick google search found a possible method: http://www.unix.com/high-level-programming/12235-how-read-cts-dsr-rs232-unix-using-c-language.html#post302302765 Photo of box connected to my laptop: http://dropbox.easytospell.net/image/366/366.jpg A few lines of c later and I had a simple program that would output "PUSHED!!!" when the button was pressed. Turns out when the button is in it's default state the DSR pin is high and the CTS pin is low which makes sense. When the button is pushed the two pins swap CTS is high and DSR is low. So we have a easy way of detecting the state of the button that can be used to trigger and action on the system. Since I haven't been able to come up with a really neat thing to trigger with the button I decided to just write a little app that would execute another program specified on the command line. For example: [~]: button /dev/ttyS0 "echo Hi!" The source for this app can be found here: http://paste.easytospell.net/view/#22