The project is based around a generic USB-Serial converter and a small number of discrete components likely to be found in any well-stocked junk box. Code examples are given for control of the interface and simple to construct examples are given throughout. I originally developed this interface to build an office control system , but have since used it on many projects where I wanted quick results and didn't need the complexity of full USB control.
Preview of the steps involved:
1. Overview - how does it work?
3. Output examples: LED control and motorised flashing beacon notification
5. Input example: signature hotkey
An arbitrary number of interfaces can be controlled by the same host machine via hubs etc., which combined with the low cost of USB-Serial converters makes this a great starting point for many projects.
Step 1: Overview
These levels mean they can't be interfaced directly with the TTL logic levels of HIGH +5V and LOW 0V. The signals also have a high source impedance. While this means that they can stand being short circuited it also means that you really can't power anything with a significant load. So in order to be useful, our interface needs to convert these signals to TTL levels and reduce the source resistance to allow higher power devices to be controlled.
In this example we'll just be controlling one input (CTS) and one output (RTS), but the same method can be applied to use the other available lines for control. For instance, by using both the RTS and DSR lines we could control external I²C devices by bit-banging the SDA and SCL lines.
So let's start by enabling output...
Step 2: Output
Step 3: Output Examples: LED and Beacon
However it's much more rewarding to use an external supply and drive higher power devices. For example, a rotating beacon. I found these battery powered beacons recently and converted one for USB control. The circuit in the second image uses two transistors in Darlington configuration, with U2 a higher power device than U1, enough to supply a few hundred milliamps to the beacon. The beacon is simply a motor in parallel with an incandescent bulb, so I've added a small capacitor to suppress noise from the motor and a diode to prevent the back EMF from the motor damaging the transistor.
The attached file 'serialcontrol.c' can be used to control both these circuits. It allows the level on the RTS line to be switched from the command line, for example:
serialcontrol /dev/ttyUSB0 on
This makes it simple to control the beacon from a script, allowing you to make an incoming email indicator or a server monitor indicator etc. Note that the circuits in both of these examples invert the input, so you'll have to turn the port 'off' to switch the device on.
Now we just need to capture some input and we can really begin to interact with our environment!
Step 4: Input
The value of R3 needs to be chosen based on the serial converter used as it's sensitive to the converter's input impedance. I use a value of 1K, but you may need to experiment.
Step 5: Input Example: Signature Hotkey
The attached file 'hotkey.c' gives an example of a daemon process that monitors the external hotkey. When the hotkey is pressed, the daemon sends the phrase to the currently active window as a series of keypresses. This saves me considerable time when filling in online forms or writing letters. The daemon uses TIOCMIWAIT to sleep while waiting for a keypress and then sleeps for a few milliseconds afterwards to debounce the key before sending the phrase.