Here is a posting I made on the HomeSeer message board. I now have the
answers to many of my questions.
Guys thanks so much for the information. Since I posted this question I
have bought myself the two Wrox books on Visual Basic .Net (Beginning
and Programming). I have skipped around in the books looking for the
information I would need to build my code natively in .Net.
Through my previous experience with VB 6.0 writing an ActiveX
Executable that talked to HomeSeer 1.7 through COM and vice-versa, and
the answers to my questions here, I now have all the information I
need.
I do like the idea of writing my code as HomeSeer Plug-In since I would
be in the same "environment" as HomeSeer itself with all the
benefits that provides. Since my code will not have any kind of user
interface and I would just like to make this a .Net class, I would like
to write the code as a DLL.
The big three controls that I used to use were the MSComm Control,
Winsock Control and Timers on forms. I have learned the basics of all
three of these in .Net and how they would be imbedded in a .Net class
or as classes of their own called by my .Net class.
Here is what I have done for my three main components:
1. For serial port communications I am using the code posted here by
Rich. It is a .Net Class that looks like a wrapper for calls to the
Windows API. I have compiled the class called "clsRS232" as a DLL
in Visual Studio 2003. It seems to have the events and properties that
I will need, similar to the MSComm Control in Visual Basic 6.0.
2. For Winsock communications (specifically UDP) I have built my own
..Net class that I have compiled into a DLL. Here is the source code for
the class I wrote (it has an event and a few methods), it works great.
Code:
Imports System.Net.Sockets
Imports System.Text
Imports System.Threading
Public Class UDPData
Private ReceivingUDPObject As UdpClient
Private ThreadUDPReceive As System.Threading.Thread
Private RemoteIpEndPoint As New
System.Net.IPEndPoint(System.Net.IPAddress.Any, 0)
Public Event UDPMessageReceived(ByVal Message As String)
Public Sub SendDataViaUDP(ByVal LocalPort As Integer, ByVal
RemoteComputerAddress As String, ByVal RemoteComputerPort As Integer,
ByVal Message As String)
Dim UDPClientObject As New UdpClient(LocalPort)
UDPClientObject.Send(Encoding.ASCII.GetBytes(Message),
Encoding.ASCII.GetBytes(Message).Length, RemoteComputerAddress,
RemoteComputerPort)
UDPClientObject.Close()
UDPClientObject = Nothing
End Sub
Public Sub StartReceivingUDPData(ByVal ListenToPort As Integer)
ReceivingUDPObject = New System.Net.Sockets.UdpClient(ListenToPort)
ThreadUDPReceive = New System.Threading.Thread(AddressOf
ReceiveUDPMessages)
ThreadUDPReceive.Start()
End Sub
Public Sub StopReceivingUDPData()
ThreadUDPReceive.Abort()
ReceivingUDPObject.Close()
End Sub
Private Sub ReceiveUDPMessages()
Dim RawDataReceived As [Byte]() =
ReceivingUDPObject.Receive(RemoteIpEndPoint)
Dim StringReceived As String
StringReceived = Trim(Encoding.ASCII.GetChars(RawDataReceived))
RaiseEvent UDPMessageReceived(StringReceived)
StartNewUDPReceivingThread()
End Sub
Private Sub StartNewUDPReceivingThread()
ThreadUDPReceive = New System.Threading.Thread(AddressOf
ReceiveUDPMessages)
ThreadUDPReceive.Start()
End Sub
End Class
3. For timed events to fire off, I learned how to use timers in a
class. It is very different from the simple little timer control you
add to your forms in Visual Basic 6.0, but again they work just fine.
Code:
Private WithEvents TestTimer As Timers.Timer
Private Ticks As Integer
Sub Start_Timer()
TestTimer = Nothing
TestTimer = New Timers.Timer
TestTimer.Interval = 1000
TestTimer.Start()
End Sub
Private Sub TestTimer_Elapsed(ByVal sender As Object, ByVal e As
System.Timers.ElapsedEventArgs) Handles TestTimer.Elapsed
Console.WriteLine("Tick: " & Ticks)
Ticks += 1
If Ticks > 5 Then
Ticks = 0
TestTimer.Stop()
End If
End Sub
I have completely read the HomeSeer Plug-In SDK and think I understand
what I need to do. At the very least I can get my DLL working and then
deal with the HomeSeer Plug-In issues. I am curious, what is the
difference from an event that a .Net Class fires off and what HomeSeer
calls "Callbacks"? What are Callbacks and why do they have to be
registered and where would I use them? If my process does some
listening on the Serial port and some UDP messages with some looping,
when should I call the equivalent of DoEvents in .Net or use the new
"Threading"?
Thanks again for all your help, and any more guidance or suggestions
would be greatly appreciated!
Jean-Marie Vaneskahian
(e-mail address removed)