Please login or register.

Login with username, password and session length
Pages: [1] 2

Author Topic: VB example questions  (Read 40321 times)

EL34

  • Hero Member
  • *****
  • Helpful Post Rating: 21
  • Posts: 278
    • My X-10 projects
VB example questions
« 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
Logged
W10 - CM15A - AHP 3.301 - i Witness - MyHouse online - Smart Macros - SDK using Visual Basic express 2008
My X10 page-> http://www.el34world.com/Misc/home/X10_0.htm

Tuicemen

  • Administrator
  • Hero Member
  • ****
  • Helpful Post Rating: 283
  • Posts: 10509
  • I don't work for X10, I use it successfuly!
Re: VB example questions
« Reply #1 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! ;)
Logged
Please Read Topic:
General Forum Etiquette
Before you post!

EL34

  • Hero Member
  • *****
  • Helpful Post Rating: 21
  • Posts: 278
    • My X-10 projects
Re: VB example questions
« Reply #2 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

Logged
W10 - CM15A - AHP 3.301 - i Witness - MyHouse online - Smart Macros - SDK using Visual Basic express 2008
My X10 page-> http://www.el34world.com/Misc/home/X10_0.htm

Tuicemen

  • Administrator
  • Hero Member
  • ****
  • Helpful Post Rating: 283
  • Posts: 10509
  • I don't work for X10, I use it successfuly!
Re: VB example questions
« Reply #3 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! ;)
Logged
Please Read Topic:
General Forum Etiquette
Before you post!

EL34

  • Hero Member
  • *****
  • Helpful Post Rating: 21
  • Posts: 278
    • My X-10 projects
Re: VB example questions
« Reply #4 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

Logged
W10 - CM15A - AHP 3.301 - i Witness - MyHouse online - Smart Macros - SDK using Visual Basic express 2008
My X10 page-> http://www.el34world.com/Misc/home/X10_0.htm

EL34

  • Hero Member
  • *****
  • Helpful Post Rating: 21
  • Posts: 278
    • My X-10 projects
Re: VB example questions
« Reply #5 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???
Logged
W10 - CM15A - AHP 3.301 - i Witness - MyHouse online - Smart Macros - SDK using Visual Basic express 2008
My X10 page-> http://www.el34world.com/Misc/home/X10_0.htm

Puck

  • Advanced Member
  • Hero Member
  • ******
  • Helpful Post Rating: 171
  • Posts: 1799
Re: VB example questions
« Reply #6 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.
Logged

EL34

  • Hero Member
  • *****
  • Helpful Post Rating: 21
  • Posts: 278
    • My X-10 projects
Re: VB example questions
« Reply #7 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.
Logged
W10 - CM15A - AHP 3.301 - i Witness - MyHouse online - Smart Macros - SDK using Visual Basic express 2008
My X10 page-> http://www.el34world.com/Misc/home/X10_0.htm

-Bill- (of wgjohns.com)

  • Advanced Member
  • Hero Member
  • ******
  • Helpful Post Rating: 81
  • Posts: 1340
  • He's just this guy. You know?
    • wgjohns.com
Re: VB example questions
« Reply #8 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.
Logged
-Bill- (of wgjohns.com)
bill@wgjohns.com

In the real world, the only constant is change.

When I'm online you can find me in the Home Automation Chat Room!

EL34

  • Hero Member
  • *****
  • Helpful Post Rating: 21
  • Posts: 278
    • My X-10 projects
Re: VB example questions
« Reply #9 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
Logged
W10 - CM15A - AHP 3.301 - i Witness - MyHouse online - Smart Macros - SDK using Visual Basic express 2008
My X10 page-> http://www.el34world.com/Misc/home/X10_0.htm

-Bill- (of wgjohns.com)

  • Advanced Member
  • Hero Member
  • ******
  • Helpful Post Rating: 81
  • Posts: 1340
  • He's just this guy. You know?
    • wgjohns.com
Re: VB example questions
« Reply #10 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!
Logged
-Bill- (of wgjohns.com)
bill@wgjohns.com

In the real world, the only constant is change.

When I'm online you can find me in the Home Automation Chat Room!

EL34

  • Hero Member
  • *****
  • Helpful Post Rating: 21
  • Posts: 278
    • My X-10 projects
Re: VB example questions
« Reply #11 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


Logged
W10 - CM15A - AHP 3.301 - i Witness - MyHouse online - Smart Macros - SDK using Visual Basic express 2008
My X10 page-> http://www.el34world.com/Misc/home/X10_0.htm

EL34

  • Hero Member
  • *****
  • Helpful Post Rating: 21
  • Posts: 278
    • My X-10 projects
Re: VB example questions
« Reply #12 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




Logged
W10 - CM15A - AHP 3.301 - i Witness - MyHouse online - Smart Macros - SDK using Visual Basic express 2008
My X10 page-> http://www.el34world.com/Misc/home/X10_0.htm

-Bill- (of wgjohns.com)

  • Advanced Member
  • Hero Member
  • ******
  • Helpful Post Rating: 81
  • Posts: 1340
  • He's just this guy. You know?
    • wgjohns.com
Re: VB example questions
« Reply #13 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
Logged
-Bill- (of wgjohns.com)
bill@wgjohns.com

In the real world, the only constant is change.

When I'm online you can find me in the Home Automation Chat Room!

EL34

  • Hero Member
  • *****
  • Helpful Post Rating: 21
  • Posts: 278
    • My X-10 projects
Re: VB example questions
« Reply #14 on: March 30, 2008, 07:10:12 AM »

Thanks for the info BIll.
I'll go check out the timer and app exit info.
Logged
W10 - CM15A - AHP 3.301 - i Witness - MyHouse online - Smart Macros - SDK using Visual Basic express 2008
My X10 page-> http://www.el34world.com/Misc/home/X10_0.htm
Pages: [1] 2
 

X10.com | About X10 | X10 Security Systems | Cameras| Package Deals
© Copyright 2014-2016 X10.com All rights reserved.