Posts Tagged ‘vb code’

Like many programs there is always more ways to do one thing.

Here is another way I found to talk to my PLC what I’ve been able to get is that i’m sending the direct HEX (PLM) commands to the PLC. Which in turn makes  the processing faster. In my environment it helped a little but had its down sides.

My initial articles using the regular Insteon commands are here.

Turn on a group (SM is your VB PLC Object) Using the PLCHEX command

Im suspecting the first HEX codes are the address and command we are sending to the PLC

Example #1,

My PLC ID is 0D.51.32
Group I want on is “1A
Groups use :C5
Command: Turn on (11=On,12=Fast On,13=Off,14 = Fast Off,19=Poll, 10=Ping)
Power:Full (The last FF for 255 HEX)


Sm.SendPLCHex("02 40 01 A1 00 09 FD CB 0D 51 32 00 00 1A C5 11 FF)
Sm.SendPLCHex("02 46 01 42 10 9F") ' - Execute Command?

Advantages is that there is no ‘sendtxt’ echo coming back or even a echo of my text going out, which for me makes the command process faster.  And you still get the ‘echo’ in the same format coming in so no change to your program is needed if you are already capturing th text comming back.

Cons:There is no place to define the HOPS?. Sometimes can fail is you send to many commands one right after the other, Im suspecting that since you we are cutting the initial ack on the start it choke after too many commands.  My solution to this was to pause between each command I sent to it.  A good 1 seconds did more than just fine.

* BTW, I like to send the group commands twice since I dont do Group clean-up’s so I basically execute the second line twice.

Turn on a single device (SM is your VB PLC Object) Using the PLCHEX command

Example #2,

My PLC ID is 0D.51.32
Device I want on is :0A.0B.0C
Single Devices use  :05
Command: Turn on (11=On,12=Fast On,13=Off,14 = Fast Off,19=Poll, 10=Ping)
Power:Full (The last FF for 255 HEX)


Sm.SendPLCHex("02 40 01 A1 00 09 FD CB 0D 51 32 0A 0B 0C  C5 11 FF)
Sm.SendPLCHex("02 46 01 42 10 9F") - ' - Execute Command?

Hope this helps in your development, and drop me a line if this helps you…

Here is another good tip which you may find helpfull.

Lets say you want to control the Monitor’s power state (On/Standby) via VB.

In my case, my Den holds my wife’s and my computer.  The program already (using the before mentioned X10 sensors) detects movement after sunset it will ‘talk’ the weather of two zip codes (Home and work) but I would like it also to ‘bring’ back the computer from its monitor power saving mode, so even before I touch the mouse, the computer is waiting for me.
Basically the code does is calls directly to the User32 library (like to good old VB6 days).    In case we will alter the SC_MONITORPOWER state from -1, to 0, and calling other System values in HEX.

Below is how its done in VB 2005 / .NET,
is the same thing but in “C” code, I basically converted it from here.


1st we will define the Function that will be called,

2nd We will define 2 subroutines or functions that we can call I called them “WakeUp” and “Sleep.

Again this only consist of the monitor ‘power’ state, the computer is not on standby or hibernation. Many of today’s computer now have WOL capability BUT only for wired (Cat 5) connections, in my case I use USB wireless all around the house and I haven’t gotten that part to work yet.

Code below,

Public Class Form1

    '-------------- FUNCTION TO WAKE UP SCREEN
    Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hWnd As IntPtr, ByVal wMsg As Integer, ByVal wParam As Integer, ByVal lParam As Integer) As Integer

    Dim WM_SYSCOMMAND As Integer = &H112
    Dim SC_MONITORPOWER As Integer = &HF170
    Dim HWND_BROADCAST As Integer = &HFFFF

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

                 ... all your startup code here...

 Public Sub Wakeup()

        Beep() '---- Just for fun
    End Sub

    Public Sub Sleep()
    End Sub

One thing I was always interested was being able to notify me when a certain person calls.  And in this case flash a inteson light or two.

My code is in VB 2005, for a VB6 Sample you can go here and basically requires only some little coding.  In the case of VB6 I think you only need to add the Serial object and adjust the port via the objects property settings.

What I used,

  • Analog phone with Caller ID (Vonage actually)
  • Internal / External Modem – Mine is a 56k External modem and have caller Id capability.
  • Hook them up, from the wall to the modem, to the modem to the handset.  🙂

The best way to see if it does is to open Hyper terminal and type the following command AT #CID=1 if it respond’s ok your set. This of course if for the modem i’m using, depending on your device it may be different.  This page includes how you can check for Caller ID capability on your modem

Here is a example of what the modem will pickup and we will parse.


DATE = 0621

TIME = 1113
NMBR = 1407xxxxxxxxx
NAME = Carlos Cobol

If you modem is external like mine you need to make sure AutoAwnser is disabled this is done by the DIP switches, For the US robotics its Dip switch #5 as down. In the case its internal you will need to add the command to disable auto answer. This is done with the command ATS0=0.

My Dip settings are as follows,









Since the code below allows to send some strings up startup, just add the in there if you need to.

Note: I do make reference to textbox called txtDataReceived in my form which will include the output.

First we make our imports include the serial devices (My program writes to a SQL database so some lines may not be needed)

Imports System.IO
Imports System.IO.Ports
Imports System.Net
Imports System.Data
Imports System
Imports System.Drawing
Imports System.Windows.Forms
Imports System.ComponentModel
Imports System.Threading

In our Class definitions we include the reference to the serial port and we’ll call it ‘Modem’

Public Class Form1
    Dim WithEvents Modem As New IO.Ports.SerialPort

In our Load event we will attach to the serial port and give it the basic settings, Here is where you would also add any initialization commands

 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Modem.PortName = "COM1"
        Modem.BaudRate = 9600
        Modem.DataBits = 8
        Modem.Parity = Parity.None
        Modem.StopBits = StopBits.One
        If Modem.IsOpen = False Then Modem.Open()

    End Sub

Next using the “Data Received” event we want to capture the Data which was received, Since there is a thread already running it wont allow us to update the textbox so simple we need to call use using the definition below,

 Private Sub DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles Modem.DataReceived

            txtDataReceived.Invoke(New myDelegate(AddressOf updateTextBox), New Object() {})
        Catch EX As Exception
        End Try
    End Sub

Second function to update the textbox and check for the number, and check for one of the text information above, Ive stripped my code for simplicity but you can search for specific strings here, match it with fields in a database, even FTP the data somewhere else, but that i’ll show you on another post.
Here we will check for the number ‘123456789’

 Public Sub updateTextBox()
        Dim temp As String
        With txtDataReceived
            .Font = New Font("Garamond", 12.0!, FontStyle.Bold)
            .SelectionColor = Color.Red

            temp = Modem.ReadExisting.ToString
            Buffer = Buffer + temp
        End With

        If Buffer.Contains("NMBR") = True Then
            Dim FILENAME As String = "c:\cALLINFO.TXT"  ' - Set a filename
            File.WriteAllText(FILENAME, Buffer)    '---- Write all the buffer for testing if we want to see it
            If Buffer.contains("123456789") then   '----- check for specific #
                End If
        End if                  
End sub