Imports System.Runtime.InteropServices Public Class Form1 Private Const IOCTL_GPIO_SET_PIN_CONFIGURATION As Integer = 262144 Private Const IOCTL_GPIO_SET_PIN_OUTPUT_VALUE As Integer = 262148 Private Const IOCTL_GPIO_GET_PIN_INPUT_VALUE As Integer = 262152 Dim bytePtr As IntPtr Friend Enum GPIO_PORT As Integer PORT_A = 0 PORT_B = 1 PORT_C = 2 PORT_D = 3 PORT_E = 4 PORT_F = 5 PORT_G = 6 PORT_H = 7 PORT_I = 8 PORT_J = 9 End Enum Friend Enum GPIO_PIN_CONFIGURATION As Integer OUTPUT = 0 INPUT_WITH_PULLUP = 1 INPUT_WITHOUT_PULLUP = 2 End Enum Friend Enum GPIO_PIN_VALUE As Integer LOW = 0 HIGH = 1 End Enum _ Friend Structure GPIO_SET_PIN_CONFIGURATION Public portNumber As GPIO_PORT Public pinNumber As Integer Public pinConfiguration As GPIO_PIN_CONFIGURATION Public pinValue As GPIO_PIN_VALUE End Structure _ Friend Structure GPIO_SET_PIN_OUTPUT_VALUE Public portNumber As GPIO_PORT Public pinNumber As Integer Public pinValue As GPIO_PIN_VALUE End Structure _ Friend Structure GPIO_GET_PIN_OUTPUT_VALUE Public portNumber As GPIO_PORT Public pinNumber As Integer Public pinValue As GPIO_PIN_VALUE End Structure _ Public Shared Function DeviceIoControl(ByVal hDevice As Integer, ByVal dwIoControlCode As Integer, ByVal lpInBuffer As IntPtr, ByVal nInBufferSize As Integer, ByVal lpOutBuffer As Byte(), ByVal nOutBufferSize As Integer, ByRef lpBytesReturned As Integer, ByVal lpOverlapped As IntPtr) As Integer End Function _ Public Shared Function CreateFile(ByVal lpFileName As String, ByVal dwDesiredAccess As UInteger, ByVal dwShareMode As UInteger, ByVal lpSecurityAttributes As IntPtr, ByVal dwCreationDisposition As UInteger, ByVal dwFlagsAndAttributes As UInteger, ByVal hTemplateFile As IntPtr) As IntPtr End Function Private Shared _gpioFile As IntPtr = CreateFile("GPI0:", &H40000000, 0, IntPtr.Zero, 3, 0, IntPtr.Zero) Dim ledValues(4) As Byte Dim inputNumbers(6) As Integer Dim checkStates(6) As Integer Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click toggleLed(5) If (ledValues(0) = 1) Then Button1.Text = "LED 1 ON" Else Button1.Text = "LED 1 OFF" End If End Sub Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click toggleLed(6) If (ledValues(1) = 1) Then Button2.Text = "LED 2 ON" Else Button2.Text = "LED 2 OFF" End If End Sub Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click toggleLed(7) If (ledValues(2) = 1) Then Button3.Text = "LED 3 ON" Else Button3.Text = "LED 3 OFF" End If End Sub Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click toggleLed(8) If (ledValues(3) = 1) Then Button4.Text = "LED 4 ON" Else Button4.Text = "LED 4 OFF" End If End Sub Private Sub Form1_Closed(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Closed Marshal.FreeHGlobal(bytePtr) End Sub Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim accessType As Integer = 1 Dim size(4) As Byte Dim pinConfig As GPIO_SET_PIN_CONFIGURATION ' Allocate a block of unmanaged memory to receive the structure bytePtr = Marshal.AllocHGlobal(12) pinConfig.pinConfiguration = GPIO_PIN_CONFIGURATION.OUTPUT pinConfig.pinValue = GPIO_PIN_VALUE.LOW pinConfig.portNumber = GPIO_PORT.PORT_B For i = 0 To 4 ledValues(i) = 0 pinConfig.pinNumber = 4 + i ' Copy the structure to the memory block Marshal.StructureToPtr(pinConfig, bytePtr, False) DeviceIoControl(_gpioFile, IOCTL_GPIO_SET_PIN_CONFIGURATION, bytePtr, 16, size, 0, accessType, IntPtr.Zero) Next Button1.Text = "LED 1 OFF" Button2.Text = "LED 2 OFF" Button3.Text = "LED 3 OFF" Button4.Text = "LED 4 OFF" ' inputs initialization inputNumbers(0) = 0 inputNumbers(1) = 3 inputNumbers(2) = 5 inputNumbers(3) = 6 inputNumbers(4) = 7 inputNumbers(5) = 11 pinConfig.portNumber = GPIO_PORT.PORT_G pinConfig.pinConfiguration = GPIO_PIN_CONFIGURATION.INPUT_WITHOUT_PULLUP For cpt = 0 To 5 pinConfig.pinNumber = inputNumbers(cpt) ' Copy the structure to the memory block Marshal.StructureToPtr(pinConfig, bytePtr, False) DeviceIoControl(_gpioFile, IOCTL_GPIO_SET_PIN_CONFIGURATION, bytePtr, 16, size, 0, accessType, IntPtr.Zero) Next CheckBox1.Text = "Button 1" CheckBox2.Text = "Button 2" CheckBox3.Text = "Button 3" CheckBox4.Text = "Button 4" CheckBox5.Text = "Button 5" CheckBox6.Text = "Button 6" End Sub Private Sub toggleLed(ByVal ledNumber As Byte) Dim accessType As Integer = 1 Dim pinConfig(13) As Byte Dim size(4) As Byte Dim pinValue As GPIO_SET_PIN_OUTPUT_VALUE If (ledValues(ledNumber - 5) = 1) Then ledValues(ledNumber - 5) = 0 pinValue.pinValue = GPIO_PIN_VALUE.LOW Else ledValues(ledNumber - 5) = 1 pinValue.pinValue = GPIO_PIN_VALUE.HIGH End If pinValue.portNumber = GPIO_PORT.PORT_B pinValue.pinNumber = ledNumber ' Copy the structure to the memory block Marshal.StructureToPtr(pinValue, bytePtr, False) DeviceIoControl(_gpioFile, IOCTL_GPIO_SET_PIN_OUTPUT_VALUE, bytePtr, 12, size, 0, accessType, IntPtr.Zero) End Sub Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick Dim pinValue As GPIO_GET_PIN_OUTPUT_VALUE Dim pinValueRead As GPIO_GET_PIN_OUTPUT_VALUE Dim cpt As Integer Dim accessType As Integer = 1 Dim size(4) As Byte pinValue.portNumber = GPIO_PORT.PORT_G For cpt = 0 To 5 pinValue.pinNumber = inputNumbers(cpt) ' Copy the structure to the memory block Marshal.StructureToPtr(pinValue, bytePtr, False) DeviceIoControl(_gpioFile, IOCTL_GPIO_GET_PIN_INPUT_VALUE, bytePtr, 12, Size, 0, accessType, IntPtr.Zero) ' copy the memory block to the structure pinValueRead = CType(Marshal.PtrToStructure(bytePtr, GetType(GPIO_GET_PIN_OUTPUT_VALUE)), GPIO_GET_PIN_OUTPUT_VALUE) If (pinValueRead.pinValue = GPIO_PIN_VALUE.HIGH) Then checkStates(cpt) = 1 Else checkStates(cpt) = 0 End If Next If (checkStates(0) = 1) Then CheckBox1.CheckState = CheckState.Checked Else CheckBox1.CheckState = CheckState.Unchecked End If If (checkStates(1) = 1) Then CheckBox2.CheckState = CheckState.Checked Else CheckBox2.CheckState = CheckState.Unchecked End If If (checkStates(2) = 1) Then CheckBox3.CheckState = CheckState.Checked Else CheckBox3.CheckState = CheckState.Unchecked End If If (checkStates(3) = 1) Then CheckBox4.CheckState = CheckState.Checked Else CheckBox4.CheckState = CheckState.Unchecked End If If (checkStates(4) = 1) Then CheckBox5.CheckState = CheckState.Checked Else CheckBox5.CheckState = CheckState.Unchecked End If If (checkStates(5) = 1) Then CheckBox6.CheckState = CheckState.Checked Else CheckBox6.CheckState = CheckState.Unchecked End If End Sub End Class