X10 Community Forum

🖥️ActiveHome Pro => SDK => Topic started by: EL34 on March 27, 2008, 08:30:58 PM

Title: VB example questions
Post by: EL34 on March 27, 2008, 08:30:58 PM
I thought I would start a new topic because I hijacked brets topic.

I'm starting fresh and coming from a perl background, VB looks strange to me.
I'm using the VB example in the SDK to try and get a grasp.
I've downloaded MS Visual basic 2008 express and started a new project, a windows form application, correct?.
I am thinking that I want a text window in my form that displays the text from monitored x10 traffic
Haven't gotten around to adding any event handlers to my form yet
I pasted the SDK VB code into my project.


right off the bat I have an error:
Quote
Warning   1   Namespace or type specified in the Imports 'ActiveHomeScriptLib' doesn't contain any public member or cannot be found. Make sure the namespace or the type is defined and contains at least one public member. Make sure the imported element name doesn't use any aliases.   C:\Documents and Settings\Owner\Local Settings\Application Data\Temporary Projects\WindowsApplication1\Form1.vb   2   9   WindowsApplication1

What's the correct way to import the x10 library into my project?

Code: [Select]
' VBScript source code
Imports ActiveHomeScriptLib
Imports System

Module Module1
    Dim WithEvents ActiveHomeObj As ActiveHome

    <MTAThread()> Sub Main()

        'create ActiveHome object
        Try
            ActiveHomeObj = CreateObject("X10.ActiveHome")
        Catch exc As Exception
            Console.WriteLine("err num: " & Str(Err.Number) & " '" & exc.ToString & "'")
        Finally
        End Try

        'send a1 on
        Try
            ActiveHomeObj.SendAction("sendplc", "a1 on")
        Catch exc As Exception
            Console.WriteLine("err num: " & Str(Err.Number) & " '" & exc.ToString & "'")
        Finally
        End Try


        'wait for events
        Dim nRead
        While True
            System.Threading.Thread.Sleep(0)
            nRead = Console.Read()
        End While

    End Sub

    'events from ActiveHome: write out received event
    Sub ActiveHome_RecvAction(ByVal bszRecv As Object _
                            , ByVal vParm1 As Object _
                            , ByVal vParm2 As Object _
                            , ByVal vParm3 As Object _
                            , ByVal vParm4 As Object _
                            , ByVal vParm5 As Object _
                            , ByVal vReserved As Object) Handles ActiveHomeObj.RecvAction

        Console.WriteLine("RecvAction: " & bszRecv & " " & vParm1 & " " & vParm2 & " " & vParm3 & " " & vParm4 & " " & vParm5)
    End Sub

End Module

Public Class Form1

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

    End Sub
End Class
Title: Re: VB example questions
Post by: Tuicemen on March 28, 2008, 08:21:20 AM
 Double click on your main form
that will bring up  a code window like this
Code: [Select]
Public Class Form1

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

    End Sub
End Class
Now you can enter in your code as it has to be loaded when the main form loads!
You'll have to click on project then add reference and find activehomescript 1.0 type library
so VB knows where the activehomescript.dll is.
code should look something like this:
Code: [Select]
Imports ActiveHomeScriptLib
Imports System
Public Class Form1
    Dim WithEvents ActiveHomeObj As ActiveHomeScriptLib.ActiveHome

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ActiveHomeObj = New ActiveHome
    End Sub
    'events from ActiveHome: write out received event
    Sub ActiveHome_RecvAction(ByVal bszRecv As Object _
                            , ByVal vParm1 As Object _
                            , ByVal vParm2 As Object _
                            , ByVal vParm3 As Object _
                            , ByVal vParm4 As Object _
                            , ByVal vParm5 As Object _
                            , ByVal vReserved As Object) Handles ActiveHomeObj.RecvAction

        Console.WriteLine("RecvAction: " & bszRecv & " " & vParm1 & " " & vParm2 & " " & vParm3 & " " & vParm4 & " " & vParm5)
    End Sub

End Class
That should get you started! ;)
Title: Re: VB example questions
Post by: EL34 on March 28, 2008, 10:15:47 AM
Ah ok, thanks so much.
I saw the add referance and wasn't sure how to procede. The library is under the COM tab.

I dumped my project, started a new one using your example, no errors, all looks good so far.
I added a panel to the form and then I added a multi line textbox indise the panel.

Hmm looks like a line of code was added below my textbox line. I guess the program did that?
Now I want the x10 traffic to print in the textbox, Right now the code references the console?
How do I get the info to print in the textbox?

Thanks for all your help, this is quite diiferent from editing perl by hand in a text editor. LOL

Code: [Select]
Imports ActiveHomeScriptLib
Imports System


Public Class Form1

    Dim WithEvents ActiveHomeObj As ActiveHomeScriptLib.ActiveHome

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ActiveHomeObj = New ActiveHome
    End Sub
    'events from ActiveHome: write out received event
    Sub ActiveHome_RecvAction(ByVal bszRecv As Object _
                            , ByVal vParm1 As Object _
                            , ByVal vParm2 As Object _
                            , ByVal vParm3 As Object _
                            , ByVal vParm4 As Object _
                            , ByVal vParm5 As Object _
                            , ByVal vReserved As Object) Handles ActiveHomeObj.RecvAction

        Console.WriteLine("RecvAction: " & bszRecv & " " & vParm1 & " " & vParm2 & " " & vParm3 & " " & vParm4 & " " & vParm5)
    End Sub



    Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
        ActiveHomeObj = New ActiveHome

    End Sub

    Private Sub Panel1_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Panel1.Paint

    End Sub
End Class

Title: Re: VB example questions
Post by: Tuicemen on March 28, 2008, 10:57:32 AM
remove line:
Code: [Select]
ActiveHomeObj = New ActiveHome in the textbox1 sub it isn't needed, you have already declared that when the form1 loads

replace the line :
Code: [Select]
Console.WriteLine("RecvAction: " & bszRecv & " " & vParm1 & " " & vParm2 & " " & vParm3 & " " & vParm4 & " " & vParm5) with:

Code: [Select]
textbox1.text=("RecvAction: " & bszRecv & " " & vParm1 & " " & vParm2 & " " & vParm3 & " " & vParm4 & " " & vParm5)
You'll find that the 2008 Express adds a lot of code for you as you add buttons etc! ;)

That should display the info in your text box! ;)
Title: Re: VB example questions
Post by: EL34 on March 28, 2008, 12:49:31 PM
Ah yes, that's the ticket, thanks juiceman.

Only problem now is that each x10 event prints over the top of the previous one on line one of the textbox.
I made the text box a multi line text box with scroll bars so there was room for multiple events on separate lines.

If this was perl, I would print each line with a chr 10 carriage return pasted to the end or add ' \n' which is the same thing.
Don't know the coding yet for VB on how you print a carriage return at the end of the data line?
I'll have a looks at the help file.

Code: [Select]
Imports ActiveHomeScriptLib
Imports System

Public Class Form1

    Dim WithEvents ActiveHomeObj As ActiveHomeScriptLib.ActiveHome

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ActiveHomeObj = New ActiveHome
    End Sub
    'events from ActiveHome: write out received event
    Sub ActiveHome_RecvAction(ByVal bszRecv As Object _
                            , ByVal vParm1 As Object _
                            , ByVal vParm2 As Object _
                            , ByVal vParm3 As Object _
                            , ByVal vParm4 As Object _
                            , ByVal vParm5 As Object _
                            , ByVal vReserved As Object) Handles ActiveHomeObj.RecvAction

        'Console.WriteLine("RecvAction: " & bszRecv & " " & vParm1 & " " & vParm2 & " " & vParm3 & " " & vParm4 & " " & vParm5)
        TextBox1.Text = ("RecvAction: " & bszRecv & " " & vParm1 & " " & vParm2 & " " & vParm3 & " " & vParm4 & " " & vParm5)

    End Sub


    Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged

    End Sub

    Private Sub Panel1_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Panel1.Paint

    End Sub
End Class

Title: Re: VB example questions
Post by: EL34 on March 28, 2008, 01:02:38 PM
Ok adding a line feed to the print line didn't do the trick

Code: [Select]
TextBox1.Text = ("RecvAction: " & bszRecv & " " & vParm1 & " " & vParm2 & " " & vParm3 & " " & vParm4 & " " & vParm5 & vbLf)
Maybe I don't want a textbox. I only need to display the events, I don't need to enter any text.
Is the textbox correct or is there a better way to just display text that srolls???
Title: Re: VB example questions
Post by: Puck on March 28, 2008, 01:29:39 PM
Thanks juice

...thanks juiceman.

You might want to check if your letter "T" is incorrectly located on your keyboard.
Title: Re: VB example questions
Post by: EL34 on March 28, 2008, 02:23:05 PM
Ok, I am guessing you are saying his name isn't juice.  ;)

Any help on the VD code while you are here?

Ooops, forgot, my keyboard also has the D and B keys switched.
Title: Re: VB example questions
Post by: -Bill- (of wgjohns.com) on March 28, 2008, 09:28:23 PM
Ok adding a line feed to the print line didn't do the trick

Code: [Select]
TextBox1.Text = ("RecvAction: " & bszRecv & " " & vParm1 & " " & vParm2 & " " & vParm3 & " " & vParm4 & " " & vParm5 & vbLf)
Maybe I don't want a textbox. I only need to display the events, I don't need to enter any text.
Is the textbox correct or is there a better way to just display text that srolls???

First thing you need to understand is that "TextBox1.Text = ..." sets the complete contents of the textbox.  ;)
To add to the current contents you should use something more like "TextBox1.Text = TextBox1.Text & ...".

Also, personally, I would end with a VBCRLF not a VBLF.

Hope that clears things up some.
Title: Re: VB example questions
Post by: EL34 on March 28, 2008, 10:03:44 PM
Ah, ok, I see, you are adding strings together and adding a CR at the end of each string, correct

Thanks Bill
Title: Re: VB example questions
Post by: -Bill- (of wgjohns.com) on March 28, 2008, 10:15:37 PM
Ah, ok, I see, you are adding strings together and adding a CR at the end of each string, correct

Thanks Bill

Yep!
Title: Re: VB example questions
Post by: EL34 on March 28, 2008, 10:18:37 PM
Ah thanks Bill

Here's what I coded and it works fine

Code: [Select]
    'events from ActiveHome: write out received event
    Sub ActiveHome_RecvAction(ByVal bszRecv As Object _
                            , ByVal vParm1 As Object _
                            , ByVal vParm2 As Object _
                            , ByVal vParm3 As Object _
                            , ByVal vParm4 As Object _
                            , ByVal vParm5 As Object _
                            , ByVal vReserved As Object) Handles ActiveHomeObj.RecvAction
        Dim actiontext
        actiontext = ("RecvAction: " & bszRecv & " " & vParm1 & " " & vParm2 & " " & vParm3 & " " & vParm4 & " " & vParm5)
        actiontext = actiontext & vbCrLf
        TextBox1.Text = TextBox1.Text & actiontext


Title: Re: VB example questions
Post by: EL34 on March 29, 2008, 01:20:35 PM
Got a better grip now on the VB basics of query, sendplc and monitoring x10 traffic thanks to the help I received here.

I replaced my morning COFFEE ON perl program with a VB program.
I have one more bit to work out before the VB code does everything that the perl code did.

Here's the way I use the coffee on program
Macro A1 is triggered via a slimfire keychain remote when I wake up in the morning while I am still in bed.
The A1 macro executes coffee.exe
coffee.exe turns on kitchen lights the coffee pot and the F1 module which is my "coffee program is already on yes or no" decision module.
I do this so that if I press the button twice, and F1 is already on, that means coffee.exe is already running, and so it does not start another copy of coffee.exe.

The last thing I need to work out is that in my perl program, I had a sleep timer that would turn all the coffee modules off after an hour.
This is so I don't have to manually turn everything off in AHP and it will shut the coffee pot off in case I forget.
In perl, you just say
sleep(60000); #sleep one hour
exit; # exit the program

I can't find any VB examples of how you exit a windows forms program cleanly.
Check out my sub SleepExit below
Any tips on how I should run through the main sub and then exit after 1 hour?
Thanks again for all the help

Code: [Select]
Imports ActiveHomeScriptLib
Imports System

Public Class coffee

    Dim WithEvents ActiveHomeObj As ActiveHomeScriptLib.ActiveHome

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

        'A1 macro on event in AHP calls this program Coffee on
        Try
            ActiveHomeObj.SendAction("sendplc", "F1 on") ' coffee on module
            ActiveHomeObj.SendAction("sendplc", "H6 on") ' kitchen lights
            ActiveHomeObj.SendAction("sendplc", "H8 on") ' coffee pot
        Catch exc As Exception
            Statusbox.Text = ("err num: " & Str(Err.Number) & " '" & exc.ToString & "'")
        Finally
        End Try
        Status()
    End Sub

    'Sleep for x minutes and then exit program
    Private Sub SleepExit()
        System.Threading.Thread.Sleep(15000) ' sleep and then exit
        Statusbox.Text = "Exit Coffee program"
        'exit code will go here

    End Sub

    ' check on/off status of f1 module
    Private Sub Status()
        'query f1 0=off 1=on
        System.Threading.Thread.Sleep(5000) 'wait 5 seconds
        Dim querystatus
        Try
            querystatus = ActiveHomeObj.SendAction("queryplc", "f1 on")
            If querystatus = 0 Then
                Statusbox.Text = "Coffee is Off"
            ElseIf querystatus = 1 Then
                Statusbox.Text = "Coffee is On"
            End If
        Finally
        End Try

    End Sub

    ' turn on coffee with button
    Private Sub ButtonOn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonOn.Click
        'Coffee on F1, H8 and H6
        Try
            ActiveHomeObj.SendAction("sendplc", "F1 on")
            ActiveHomeObj.SendAction("sendplc", "H6 on")
            ActiveHomeObj.SendAction("sendplc", "H8 on")
        Finally
        End Try
        Status()
    End Sub

    ' turn off coffee with button
    Private Sub ButtonOff_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonOff.Click
        'Coffee off F1, H8 and H6
        Try
            ActiveHomeObj.SendAction("sendplc", "F1 off")
            ActiveHomeObj.SendAction("sendplc", "H6 off")
            ActiveHomeObj.SendAction("sendplc", "H8 off")
        Finally
        End Try
        Status()
    End Sub

    Private Sub StatusButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles StatusButton.Click
        Status()
    End Sub

    Private Sub StatusBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Statusbox.TextChanged
    End Sub

End Class




Title: Re: VB example questions
Post by: -Bill- (of wgjohns.com) on March 29, 2008, 04:25:41 PM
EL34,

I believe the exit command you're looking for is "Application.Exit()".

You might also want to look into adding a "Timer Control" to your main form.

By handling the timer control's "Tick" event...
Code: [Select]
    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        ... commands to execute each tick ...
    End Sub
You can have your program count "ticks" and act appropriately.  This is advantagous because it doesn't cause the program to "lock up" while it is waiting.

If you do use the timer, remember to add to your Main Form loading event:
Code: [Select]
        ' Initialize the timer
        Timer1.Enabled = True
Title: Re: VB example questions
Post by: EL34 on March 30, 2008, 07:10:12 AM
Thanks for the info BIll.
I'll go check out the timer and app exit info.
Title: Re: VB example questions
Post by: EL34 on March 31, 2008, 10:11:20 AM
Hey Bill, the Timer function doc say it is only good up to 65,000 or so ticks which is just over a minute.

I tested it and it won't time for very long.
I'll have to count ticks myself or something in order to get longer timer periods

The ApplicationExit docs are a bit sketchy and don't clearly show how to use it with a win form

The exit code below works, I am just wondering if it is exiting the simple form with one button cleanly and not leaving any tidbits behind in Windows OS?

Is this the correct way to use it in Win forms?

Code: [Select]
Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Application.Exit()
    End Sub
End Class


Thanks again for the help.
Title: Re: VB example questions
Post by: -Bill- (of wgjohns.com) on March 31, 2008, 11:52:48 PM
Hey Bill, the Timer function doc say it is only good up to 65,000 or so ticks which is just over a minute.

I tested it and it won't time for very long.
I'll have to count ticks myself or something in order to get longer timer periods

Exactly!
By handling the timer control's "Tick" event...
Code: [Select]
    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        ... commands to execute each tick ...
    End Sub
You can have your program count "ticks" and act appropriately.  This is advantagous because it doesn't cause the program to "lock up" while it is waiting.

If you do use the timer, remember to add to your Main Form loading event:
Code: [Select]
        ' Initialize the timer
        Timer1.Enabled = True

The ApplicationExit docs are a bit sketchy and don't clearly show how to use it with a win form

The exit code below works, I am just wondering if it is exiting the simple form with one button cleanly and not leaving any tidbits behind in Windows OS?

Is this the correct way to use it in Win forms?

Code: [Select]
Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Application.Exit()
    End Sub
End Class


No guarantees.  However that is basically the way I use it too.
Title: Re: VB example questions
Post by: Tuicemen on April 03, 2008, 09:05:20 AM
-Bill- (of wgjohns.com): thanks for jumping in, I figured you would  while I was away! ;)

EL34: With VB you'll find there is usually more then one way of doing something!
Use the How do I help to find examples for code your looking for (usualy it doesn't work %100 the way you want but it gives you ideas)! ;)
Another way you can do your timer is the use current time "+" (I don't have the code but it should get you thinking) ;)
If you wish just to exit a sub the command is "exit sub" this doesn't exit the program just the sub!
Hope this helps! ;)
Title: Re: VB example questions
Post by: EL34 on April 03, 2008, 09:28:51 AM
Thanks Tuiceman

I have written 3 windows form VB programs in the last 3 day. All off them are using the app(exit), no problems yet so it looks good so far ;D

The timer thing was easy.
I have the timer count to 60,000, which is one minute
On the timer event I add 1 to the TotalTime variable every the time evert is triggered.
TotalTime variable is the number of minutes the program has been running and so I just do a If Then on the TotalTime variable to see if it's time to exit the app.

I have been using the help and how do I ever time I need to translate a perl command to vb
They have examples but not all of them are exactly like you need.

for example, I needed to dim and fill an array with 16 values. All the array helps and how do I's did not show this clearly.
I ended up searching on the web and found a post somewhere that showed it using the curly braces like this { a,b,c,d,etc}

a bit of history just so you guys don't think I'm a toal idiot.  ;D
I have been programming since 1978 and so I know what I need to do in my head, it's just a matter of figuring out how it is done in VB.
I started on a commador pet, you know the ones that had a cassette to load programs and the keyboard and screen were all one piece.
After that I programmed commador business machines for quite a while.
Took some IBM 360 assembly languege courses in about 1980 and did some assembly.
went to gw basic sometime in the 80's
gave up programming all through the 90's
learned perl in 2001 because my business shopping cart is written in perl

I still write cgi shopping cart interfaces, web forms in perl and until recently, x10 home automation programs.
Wrote a big program in perl that integrates Quickbooks into my shopping cart using the Quickbooks SDK
All my orders import from the web to my local quickbooks machine, no hand punching data

The reason I am now using VB express is because it's way easier to write windows programs in Vb than it is in perl

Long winded post but just want to say thanks to Bill and you for the VB help
Title: Re: VB example questions
Post by: -Bill- (of wgjohns.com) on April 03, 2008, 10:50:40 PM
-Bill- (of wgjohns.com): thanks for jumping in, I figured you would  while I was away! ;)

You're welcome... I think?   ;)   ;D


EL34,

Been down nearly identical roads and chose VB for the very same reasons myself!

Beware, I may hit you up for pearl scripting advice someday!  ;)
Title: Re: VB example questions
Post by: EL34 on April 04, 2008, 10:29:25 AM
I'll do what I can.  ;D
I'm no expert on perl either.
Basically my approach to programming is that when I need to do something, I figure out the commands it takes to do it, write the app and then I am on to the next project.
The next project can mean any one of 10 different hobbies I have.  :D

A serious perl programmer would laugh at the way I code my apps.
I always write in a real simple to understand format with plenty of remarks.
A year from now when I have to go back and alter a program, I can understand what I did better if I keep it simple.

A real perl programmer would reduce my code down to nothing but a bunch of nested symbols that would be impossible for me to read.  ;D
Title: Re: VB example questions
Post by: EL34 on April 07, 2008, 09:12:48 PM
well, allot has happened since I downloaded VB 2008 express and got the excellent help from Tuice and Bill

Have written several small windows apps , but I now have combined them all into my X10 monitor program
It monitors X10 traffic
It is tied in with my non X10 motion detectors and can turn on light and sirens, etc
It turns on my kitchen lights and coffee when I hit a keychain button while still in bed
It does several other subroutine type things when something is triggered

I just finished hooking into my one wire weather network. My app now monitors several DS18S20 temp sensors in my green house.
I am about to write more code that makes decisions on when to turn on exhaust fans, water circulating pumps, etc in the greenhouse, based on temp sensor readings from the one wire network. I have a solar hot water panel and tank in the green house that stores hot water and then circulates it at night to keep plants warm.

here's a current screen shot, got lots more to do.

(http://www.el34world.com/Misc/home/images/X10Monitor.gif)
Title: Re: VB example questions
Post by: -Bill- (of wgjohns.com) on April 08, 2008, 01:24:35 AM
EL34,

Looks like you've been busy!

Cool!   8)
Title: Re: VB example questions
Post by: Tuicemen on April 08, 2008, 07:45:44 AM
Nice Job EL34
Sounds and looks cool! ;)
Title: Re: VB example questions
Post by: ebpDoug on April 01, 2010, 10:17:55 PM
removed