Code:
Imports System.Runtime.InteropServices
Public Class TestCortexAPIForm
Declare Auto Function SendMessage Lib "user32" ( _
ByVal hWnd As IntPtr, _
ByVal Msg As Integer, _
ByVal wParam As Integer, _
ByVal lParam As Integer) As Integer
Declare Auto Function RegisterWindowMessage Lib "User32.Dll" _
(ByVal lpString As String) As Integer
Structure CopyDataStruct
Public ID As String
Public Length As Int32
Public Data As String
End Structure
Const CortexControlAPIDiscover = "CortexAPIDiscover"
Const CortexControlAPIAttach = "CortexAPIAttach"
Const WM_COPYDATA = &H4A
Dim UM_CortexControlAPIDiscover As UInt32
Dim UM_CortexControlAPIAttach As UInt32
Dim HWND_BROADCAST As Long = -1
Dim MyCortexHandle As IntPtr = IntPtr.Zero
Dim CortexAPIActive As Boolean
Dim ReceivedMessage As String
Dim aCDS As CopyDataStruct
Dim BoardAddress As Byte
Dim TransmitBuffer() As Byte = {&HFE, &H4C, &H42, &H2, &HFF}
Private Sub TestCortexAPIForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
UM_CortexControlAPIDiscover = RegisterWindowMessage(CortexControlAPIDiscover)
UM_CortexControlAPIAttach = RegisterWindowMessage(CortexControlAPIAttach)
Device_Init()
End Sub
Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
If m.Msg = UM_CortexControlAPIAttach Then
If (m.LParam = 1) And (m.WParam <> Handle) Then
Log("< WM_CortexAPIAttach")
MyCortexHandle = m.WParam
CortexAPIActive = True
End If
m.Result = New IntPtr(1) 'Ensure returned response is correct
Else
If m.Msg = UM_CortexControlAPIDiscover Then
If m.LParam = 1 Then
If (m.WParam <> Me.Handle) Then
MyCortexHandle = m.WParam
CortexAPIActive = True
Log("< WM_CortexAPIDiscover")
Log("> WM_CortexAPIAttach")
SendMessage(HWND_BROADCAST, UM_CortexControlAPIAttach, Handle, IntPtr.Zero)
End If
End If
m.Result = New IntPtr(1) 'Ensure returned response is correct
Else
If m.Msg = WM_COPYDATA Then
If m.WParam = MyCortexHandle Then
aCDS = m.GetLParam(GetType(CopyDataStruct))
ReceivedMessage = aCDS.Data
Log("< " & ReceivedMessage)
If ReceivedMessage = "Disconnect" Then
CortexAPIActive = False
Else
If ReceivedMessage = "Close" Then
Me.Close()
End If
End If
m.Result = New IntPtr(1) 'Ensure returned response is correct
End If
Else
MyBase.WndProc(m)
End If
End If
End If
End Sub
Private Sub TestCortexAPIForm_Shown(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Shown
Log("> WM_CortexAPIDiscover")
SendMessage(HWND_BROADCAST, UM_CortexControlAPIDiscover, Handle, IntPtr.Zero)
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Command(TextBox1.Text)
End Sub
Private Sub Command(ByVal theCommand As String) 'USE THIS TO SEND COMMANDS TO CORTEX
If theCommand <> "" And CortexAPIActive Then
Log("> " & theCommand)
aCDS.ID = "1"
aCDS.Data = theCommand
aCDS.Length = aCDS.Data.Length + 1
Dim lParam As IntPtr
lParam = Marshal.AllocCoTaskMem(Marshal.SizeOf(aCDS))
Marshal.StructureToPtr(aCDS, lParam, False)
SendMessage(MyCortexHandle, WM_COPYDATA, Me.Handle, lParam)
Marshal.FreeCoTaskMem(lParam) ' Free allocated memory
End If
End Sub
Private Sub Log(ByVal AString As String)
TextBox2.AppendText(vbCrLf & DateTime.Now.ToLongTimeString & " " & AString)
LastReceivedResponse.Text = AString
End Sub
Private Sub TestCortexAPIForm_FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
Command("Disconnect")
End Sub
' Modify to suit your setup
Private Sub Device_Init()
SerialPort1.BaudRate = 9600
SerialPort1.PortName = "COM5"
SerialPort1.DataBits = 8
SerialPort1.Parity = IO.Ports.Parity.None
BoardAddress = &H4C
'Set data
TransmitBuffer(1) = BoardAddress
End Sub
Private Sub set_rgb_value(ByVal RedValue, ByVal GreenValue, ByRef BlueValue)
If (SerialPort1.IsOpen = False) Then
SerialPort1.Open()
End If
Debug.Print("Setting RGB Values:" & RedValue & " " & GreenValue & " " & BlueValue)
TransmitBuffer(3) = 1
TransmitBuffer(4) = RedValue
SerialPort1.Write(TransmitBuffer, 0, TransmitBuffer.Length)
'System.Threading.Thread.Sleep(20)
TransmitBuffer(3) = 2
TransmitBuffer(4) = GreenValue
SerialPort1.Write(TransmitBuffer, 0, TransmitBuffer.Length)
'System.Threading.Thread.Sleep(20)
TransmitBuffer(3) = 3
TransmitBuffer(4) = BlueValue
SerialPort1.Write(TransmitBuffer, 0, TransmitBuffer.Length)
'System.Threading.Thread.Sleep(20)
'SerialPort1.Close()
End Sub
Private Sub process_command(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LastReceivedResponse.TextChanged
Dim CommandToProcess(2) As String
If (LastReceivedResponse.Text.Contains("Dimmer")) Then
Debug.Print("LED RGB")
End If
If (LastReceivedResponse.Text.Contains("Lounge Dimmer 1.Set Level (Output)")) Then
' Exact colour
CommandToProcess = LastReceivedResponse.Text.Split("=")
Select Case Convert.ToDecimal(CommandToProcess(1))
Case 100 ' colour
set_rgb_value(10, 255, 255)
Case 10 ' white
set_rgb_value(255, 255, 255)
End Select
End If
End Sub
Private Sub TrackBar2_Scroll(sender As Object, e As System.EventArgs) Handles rgbBlue.Scroll
set_rgb_value(rgbRed.Value, rgbGreen.Value, rgbBlue.Value)
End Sub
Private Sub rgbRed_Scroll(sender As System.Object, e As System.EventArgs) Handles rgbRed.Scroll
set_rgb_value(rgbRed.Value, rgbGreen.Value, rgbBlue.Value)
End Sub
Private Sub rgbGreen_Scroll(sender As System.Object, e As System.EventArgs) Handles rgbGreen.Scroll
set_rgb_value(rgbRed.Value, rgbGreen.Value, rgbBlue.Value)
End Sub
End Class
3 trackbars are used to provide a quick method of updating and testing colour combinations.