Sample Visual Basic code for CM17A

Platform: Windows 95/98/NT

Overview

This is some of the code at the core of my FireworX-10 program. It is meant only as a starting point for you efforts. This code will control only one unit (1) on House Code (J). It is up to you to figure out how to send the commands for other units/housecodes. The protocol is fully defined on the x10.com website. See the References section of the main FireworX-10 page for a link.

The Code

In VB, use the Microsoft Comm Control to send signals to a COM port. I have stripped out some functionality from my application to help you get started. The only thing I ask for in return is that if you come up with anything cool, please share it with others and me. The following will create a VB application that will have two buttons, one to send ON to J1 and another to send OFF to J1. To do this:

1. Create a new Form
2. Add two buttons. One named 'cmdJ1On' with a label 'J1 On' and another named 'cmdJ1Off' with a label 'J1 Off'.
3. Add an MSComm control.

Drop in the following code:

Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Private Sub cmdJ1On_Click()
     'Send Header
     Call SendBits("1101010110101010")
     'Send Data
     Call SendBits("1111000000000000")
     'Send Footer
     Call SendBits("10101101")
End Sub

Private Sub cmdJ1Off_Click()
     'Send Header
     Call SendBits("1101010110101010")
     'Send Data
     Call SendBits("1111000000100000")
     'Send Footer
     Call SendBits("10101101")
End Sub

Private Sub Form_Load()
     'COM1
     MSComm1.CommPort = 1
     ' 9600 baud, no parity, 8 data, and 1 stop bit.
     MSComm1.Settings = "9600,N,8,1"
     MSComm1.PortOpen = True
End Sub

Public Function SendBits(Transmission As String)
     Dim i, j As Long
     Dim SendBit As Integer
     For i = 1 To Len(Transmission)
         SendBit = Val(Mid(Transmission, i, 1))
         If SendBit = 1 Then
             MSComm1.RTSEnable = True
             MSComm1.DTREnable = False
         Else
             MSComm1.RTSEnable = False
             MSComm1.DTREnable = True
         End If

         'Pause at least .5 ms
         Sleep (0.5)

         'Set back to standby
         MSComm1.RTSEnable = True
         MSComm1.DTREnable = True

         'Pause again
         Sleep (0.5)
     Next i
End Function

Private Sub Form_Unload(Cancel As Integer)
     MSComm1.PortOpen = False
End Sub

Related Information

1. Need a way to calculate sunrise and sunset in your VB program?  Check out Scott's Sample Code page.   Once there click on Visual Basic and then Dates and you will see the link to SunRiseSet.