VB - FireCracker Interface


By: Jared Hoylman

This article will give you a simple solution to interface the FireCracker available from http://www.x10.com/homepage.htm, to a PC using Microsoft’s Visual Basic 5 Professional Edition or higher. You can order the FireCracker kit which comes with the FireCracker , an X-10 Transceiver , an X-10 Lamp Module, and a RF Remote.

X-10 technology is a new wave of home automation or control. You can turn any appliance or lamp that plugs into a standard wall socket on or off. If you use a lamp module you can even dim or brighten a lamp to the brightness that you desire. And you can do all of this remotely, and without modifying your home's electrical system.

The way that X-10 works is what makes it so cool. There is no extra wiring to run through your home to control these devices. The X10 Transceiver will receive RF signals from either the FireCracker or the hand-held remote, and transmit data over the existing power lines in your house. The X10 modules will pick this data off of the power lines and turn the connected device on or off, or adjust brightness accordingly. Pretty cool, eh..?

 

About The FireCracker :

The FireCracker is a small DB9 device that plugs into your PC’s serial port. It has an odd protocol for communicating with the PC. The FireCracker uses the Data Terminal Ready (DTR) and Ready To Send (RTS) lines to both power itself and read data from the PC. This allows for pass-through capabilities. Since it does not use the standard port lines you can still connect another device to the port as long as it does not use the DTR or RTS lines. (However software may limit the number of programs using a single port.) Since the FireCracker uses either the DTR or RTS lines for power you must keep one of these lines high at all times to make sure that power is not lost.

 

The Protocol:

As mentioned earlier, the FireCracker uses an odd protocol to communicate with the PC. There are four different states that can be depicted from the two port lines. When both the DTR and RTS lines are high the FireCracker is in StandBy mode, and when both lines are low the FireCracker is in Reset mode. To send a logic 1 to the FireCracker you must toggle the DTR line low, and to send a logic 0 you must toggle the RTS line low. (See the table below.)

  RTS DTR
StandBy 1 1
Logic 1 1 0
Logic 0 0 1
Reset 0 0

 

Timing:

There is no set timing for sending commands, however it is recommended to maintain a state for at least .5 ms.

Each transmission is made up of 40 bits of data. First you must send a 16-bit header, then 16 bits of data, and then an 8-bit footer. The header and footer always have the same value, but the data changes as to which command you are sending to the FireCracker.

Header = 11010101 10101010 = D5 AA Hex

Footer = 10101101 = AD Hex

 

The VB Side

Step 1: (Start New Project)

Run Microsoft Visual Basic 5 Professional Edition or better. When the program starts you should see a screen similar to the one below…

We will be making a standard executable so click on "Standard EXE" and then on the "Open" button.

 

Step 2: (Load Comm Control)

Now that you have selected to make a standard executable you should see a blank form on the screen. Since we need to use the serial port to communicate with the FireCracker, we need to add the Microsoft Comm Control.

  1. On the menu at the top of the screen click on "Project".
  2. Now scroll down and click on "Components…"
  3. On the next screen scroll down until you see the "Microsoft Comm Control"
  4. Click in the check box next to this item.
  5. Click "OK"

Now you should see a little yellow phone in the VB toolbar.

Double click on the yellow phone and you should see an icon with the yellow phone pop up on your blank form, as shown below…

 

 

Step 3: (Understanding the Comm Control DTR and RTS Enable Commands)

Usually when using the MSComm control you need to set the baud rate, parity, and number of start and stop bits. However since the FireCracker does not use the standard connections this is not necessary.

The MSComm control has two properties called DTREnable and RTSEnable. These are Boolean properties and therefore only have two states, True and False. It so happens that setting the DTREnable property to True sets the DTR line on the serial port to a high state. And vice-versa, setting the DTREnable property to False sets the DTR line on the serial port to a low state. The same goes for the RTSEnable property and the RTS line.

 

Step 4: (Form_Load)

Above I mentioned that StandBy mode was when both the DTR and RTS lines are high. So, when executing the project, the first thing that we want to do is set up the MSComm control to use a specific port, enable the RTS and DTR lines, and finally open the port. In Form1’s Load event you should have something like this….

Private Sub Form_Load()
MSComm1.CommPort = 1      ' Use COM1
MSComm1.DTREnable = True  ' Set DTR Line High
MSComm1.RTSEnable = True  ' Set RTS Line High
MSComm1.PortOpen = True   ' Finally Open the Com Port
Pause 50                  ' Wait for port to open
Reset                     ' Reset FireCracker
End Sub

Note: The Pause and Reset commands are explained below.

 

Step 5: (Helper Routines)

Now we are going to make a couple small routines that will be the basis of how we send logic 1 and logic 0 to the FireCracker (refer to the Timing diagram above). Below is the code…

Public Sub Send_1()
MSComm1.DTREnable = False       ' Toggle DTR Line Low
Pause 1                         ' Pause for Wait State
MSComm1.DTREnable = True        ' Toggle DTR Line Back to High
Pause 1
End Sub

Public Sub Send_0()
MSComm1.RTSEnable = False       ' Toggle RTS Line Low
Pause 1                         ' Pause for Wait State
MSComm1.RTSEnable = True        ' Toggle RTS Line Back to High
Pause 1
End Sub

We have not made the Pause routine yet but what it does is pause for the number of milliseconds that is passed to it. The FireCracker only needs .5 ms between states, but in VB the smallest amount of time that we can pause is 1 ms, so this will have to do.

So now say that we want to send the header which has a value of D5AA Hex. We would have to write 16 Send_1 and Send_0 commands, one for each bit. This is too complicated..! So to ease the process how about making a routine that will send a whole byte..! See the code and comments below…

Public Sub SendByte(iByte As Long)
Dim i As Integer
For i = 0 To 7                     ' Loop through all 8 bits
If (iByte And &H80) = &H80 Then    ' If b8 is a 1 then
Send_1                             ' Send a 1
Else                               ' If b8 is a 0 then
Send_0                             ' Send a 0
End If
iByte = iByte * 2                  ' Shift the bits one place to the left
Next i                             ' Loop for next bit
End Sub

So now the process of sending data is even easier. How about a few more simple routines that will make your coding a little easier to read and follow…

Public Sub Reset()
MSComm1.RTSEnable = False      ' Toggle both RTS and DTR lines
MSComm1.DTREnable = False      ' Low to specifiy Reset
Pause 50                       ' Allow time for Reset
MSComm1.RTSEnable = True       ' Toggle both lines high to
MSComm1.DTREnable = True       ' put in StandBy Mode
Pause 50                       ' Pause again
End Sub

Public Sub SendHeader()
SendByte &HD5                  ' Send D5
SendByte &HAA                  ' Send AA
End Sub

Public Sub SendFooter()
SendByte &HAD                  ' Send AD
End Sub

The above subroutines do exactly what they say. The first one resets the FireCracker, the second one sends the header bytes, and the third one sends the footer byte.

In the General Declarations of your Form add this line…

Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)

And add a subroutine called Pause…

Public Sub Pause(milli As Long)
Sleep (milli)
End Sub

 

Step 6: (Writing A Simple Application)

For this article I am not going to go too in depth and write the whole application for you. I am just providing the tools for you to make your own FireCracker interface. However I will show you how to control the two X10 Modules that came with the FireCracker kit.

Set the Transceiver (the one with the antenna) to House Code "A". By default this module has a Unit Number of "1". Now set the lamp module that came with the kit to House Code "A" and Unit Number "2".

After looking at the FireCracker datasheet (can download below) you will see the following commands and corresponding values.

Command Binary Value Hex Value
A1 ON 01100000 00000000 6000
A1 OFF 01100000 00100000 6020
A2 ON 01100000 00010000 6010
A2 OFF 01100000 00110000 6030

So now that we have that out of the way. Let’s build our program. On your form add four command buttons as shown in the picture below…

Now set each command buttons properties as shown below…

Command1

Name – "A1_ON"
Caption – "A1 ON"

Command2

Name – "A2_ON"
Caption – "A2 ON"

Command3

Name – "A3_ON"
Caption – "A3 ON"

Command4

Name – "A4_ON"
Caption – "A4 ON"

Now your form should look like this…

Now you need to add the code to each command button to send the header, then the data, and finally the footer. Sow add something like this…

Private Sub A1_OFF_Click()
SendHeader
SendByte &H60
SendByte &H20
SendFooter
End Sub

Prvate Sub A1_ON_Click()
SendHeader
SendByte &H6
SendByte &H0
SendFooter
End Sub

Private Sub A2_OFF_Click()
SendHeader
SendByte &H60
SendByte &H30
SendFooter
End Sub

Private Sub A2_ON_Click()
SendHeader
SendByte &H60
SendByte &H10
SendFooter
End Sub

Now all of the coding is complete..! Run your app and see if it works..!

Want more..?

Since this article only showed you how to control just two modules, I have written a class for use in your applications. This class is complete with the entire House and Unit codes built in. You just set the House Code, Unit Code, and execute an ON, OFF, DIM, or BRIGHTEN command. This class comes complete with full source code and an example project to get you started on your way to your home control and automation dreams..!

See below to download it.

What can you do with this class..? With a little imagination you can control your whole house. Turn on the coffee pot and lights in the morning, turn them off when it is time for you to leave, have toast ready for you when you get out of bed, ...whatever you can imagine. The possibilities are a endless..!

About The Author:

My name is Jared Hoylman and am a EET student at DeVry in Columbus, OH scheduled to graduate in June 2000. I have been programming in VB for about 3 years and studying electronics for about the same. This is one of my first articles ever written to be placed on the web. If you have questions, comments, suggestions, or just want to say hi, you can email me at electrolinx@yahoo.com. (I am not affiliated with X-10 or Microsoft in any way.)

Thanks for reading, and I hope you enjoyed it..!

Jared Hoylman - electrolinx@yahoo.com

 Click HERE to download the Visual Basic files for the FireCracker project.

Distibution:
You are free to use this class for both personal use and commercially. If you do make something quite interesting with the class, please keep me in mind. Email me and tell me what your imagination inspired you to create. And if you are generous enough, you can send me a copy. ;-)

Visit our new remote-control store link below. We have the TWS-434 & RWS-434 RF transmitters/receivers in-stock with plenty of Holtek remote control ICs, and infrared components to get you started on other Remote Control projects

Click HERE For
Remote Control Store
We have Holtek remote control ICs, RF and Infrared parts in stock.