Kaliko Systems Limited

Make a Low-Power Media Server

Writing Relay Board Software

If you need to write your own software for a relay board the picture is complicated. In general, writing software to work to parallel or serial interfaces is simpler, with USB being harder. I have not looked at many USB relay boards, but I suspect that most are Human Interface Devices (HIDs) and these devices are the easiest USB devices to program. A particularly good site for USB programming is Jan Axelson's USB Central and I understand his books are good.

To discuss writing your own software, I have grouped the PC operating systems into 4 groups:

1.
Windows95/98/ME (DOS)
For these older operating systems, it is easy to directly access the parallel and serial ports by using an old 16-bit compiler (e.g. TurboC). However, it is always much harder to write USB applications. There are USB libraries for Windows 98, and now that a USB library is available for DOS then
DOS USB applications should also be practical.

2.
Windows NT/2000/XP/Vista/Windows 7
For 32-bit versions only, you can use the 16-bit Windows subsystem to directly access parallel and serial ports using an old 16-bit compiler. However, the 64-bit versions do not include a
16-bit Windows subsystem, so you have to use the Windows drivers (harder). USB applications are seriously hard, but doable using C# or VB.net and the Microsoft.net USB drivers. I have recently used a third party library, to write a USB command-line application using Microsoft's C# Express compiler and it works!

There are Windows USB applications that do use Microsoft.net, and I need to investigate how these are made, as
Microsoft.net is a large download with frequent security updates.

3. Mac OS
I have no information here, but Mac OS10 is Unix based at a low level.

4. Linux/Unix
It is always hard to write USB applications, but there are some Linux libraries available. I tried to use the libhid.c library with the
GNU C++ compiler, but had problems with the example and gave up. However, a reliable Linux driver has been written for the JBS relay boards using the libusb.c library and is available from the Relay Boards page at http://www.loggytronic.com.

I prefer to write relay board software as Windows batch files or Linux scripts that call a simple command-line application to change the relay state. However there are some tricks to writing
Windows batch files or Linux scripts for relay boards that are worth knowing.

Windows Batch Files

The new Windows command line interpreter ("cmd.exe") has some usefull set command extensions for relay boards. There are 3 new operators allow bit-twiddling (technical term for manipulating bits in a byte, word, etc) on enviroment variables:

Example 1: Toggle bit 1 of the relaystate environment variable.

set /a relaysstate="%relaysstate% ^ 1"

Similarly to the "bit-wise exclusive or" operator  "^" shown above, both the "bit-wise and" operator "&" and the bit-wise "inclusive or" operator "|" can also be used.


Example 2: When controlling hard disk drives "f:" and "h:", the following code will set bit 1 of the relaysstate variable if drive "f:" is on-line and will set bit 2 of the relaysstate variable if drive "h:" is on-line.

set relaysstate=0
if exist f: set /a relaysstate="%relaysstate% | 1"
if exist h: set /a relaysstate="%relaysstate% | 2"

Linux Script Files

Under construction.



Back to home.