Program นี้ เป็น Program ตัวอย่างที่ได้มาจากตอนซื้อ MINI2440 มาอ่ะคับ แต่ผมอ่านแล้วไม่เข้าใจอ่ะคับ ว่าส่วนไหนทำงานอย่างไรบ้าง วานพี่ๆ ช่วยตอบหน่อยนะคับ มือใหม่สำหรับเรื่อง ARM9 อ่ะคับ
Code (VB.NET)
Imports System.Runtime.InteropServices
Public Class Form1
' Declare Function
Declare Function CreateFile Lib "coredll.dll" Alias "CreateFile" (ByVal lpFileName As String, ByVal dwDesiredAccess As Long, ByVal dwShareMode As Long, ByVal lpSecurityAttributes As IntPtr, ByVal dwCreationDisposition As Long, ByVal dwFlagsAndAttributes As UInteger, ByVal hTemplateFile As IntPtr) As IntPtr
Declare Function DeviceIoControl Lib "coredll.dll" Alias "DeviceIoControl" (ByVal hDevice As System.IntPtr, ByVal dwIoControlCode As UInteger, ByVal lpInBuffer() As Byte, ByVal nInBufferSize As UInteger, ByVal lpOutBuffer() As Byte, ByVal nOutBufferSize As UInteger, ByVal lpBytesReturned As UInteger, ByVal lpOverlapped As System.IntPtr) As Boolean
Declare Function CloseHandle Lib "coredll.dll" Alias "CloseHandle" (ByVal hDevice As System.IntPtr) As Boolean
' Constant of Function
Const OPEN_EXISTING = 3
Const GENERIC_READ = &H80000000
Const GENERIC_WRITE = &H40000000
Const INVALID_HANDLE_VALUE = -1
' Constant from GPIO Driver
Const PORT_A = &H0
Const PORT_B = &H10
Const PORT_C = &H20
Const PORT_D = &H30
Const PORT_E = &H40
Const PORT_F = &H50
Const PORT_G = &H60
Const PORT_H = &H70
Const PORT_J = &H80
Const SET_OUTPUT = &H4
Const SET_INPUT = &H3
Const GET_PIN = &H2
Const SET_PIN_ON = &H1
Const SET_PIN_OFF = &H0
' Variable of Device
Private hPort As IntPtr
Private Sub cmdGet_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdGet.Click
Dim rBuf(4) As Byte
Dim rLen, rOutput, tmp As UInteger
Dim i As Integer
Dim chk As Boolean
' Check Open Device already
If (hPort <> INVALID_HANDLE_VALUE) Then
' Get value from PORT F
DeviceIoControl(hPort, PORT_F Or GET_PIN, Nothing, 0, rBuf, rLen, 0, IntPtr.Zero)
' Convert rBuf(Array of Byte) to rOutput(UInt32)
rOutput = BitConverter.ToInt32(rBuf, 0)
' Loop pin position in PORT F
For i = 0 To 6
' Define pin position for get value
tmp = 1 << i
' Get value from variable
tmp = rOutput And tmp
' Convert value to Boolean Type
chk = Convert.ToBoolean(tmp)
' Input value to CheckBox by Pin
Select Case i
Case 0
chkOnRs0.Checked = chk
Case 1
chkOnRs1.Checked = chk
Case 2
chkOnRs2.Checked = chk
Case 3
chkOnRs3.Checked = chk
Case 4
chkOnRs4.Checked = chk
Case 5
chkOnRs5.Checked = chk
Case 6
chkOnRs6.Checked = chk
End Select
Next
End If
End Sub
Private Sub cmdSetInput_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSetInput.Click
Dim sBuf(4) As Byte
Dim sInput As UInteger
If (hPort <> INVALID_HANDLE_VALUE) Then
sInput = 0
' Set pin position from CheckBox in to sInput
sInput = sInput + (Convert.ToUInt32(chkInput0.Checked) << 0)
sInput = sInput + (Convert.ToUInt32(chkInput1.Checked) << 1)
sInput = sInput + (Convert.ToUInt32(chkInput2.Checked) << 2)
sInput = sInput + (Convert.ToUInt32(chkInput3.Checked) << 3)
sInput = sInput + (Convert.ToUInt32(chkInput4.Checked) << 4)
sInput = sInput + (Convert.ToUInt32(chkInput5.Checked) << 5)
sInput = sInput + (Convert.ToUInt32(chkInput6.Checked) << 6)
' Convert sInput(UInt32) to sBuf(Array of Byte)
BitConverter.GetBytes(sInput).CopyTo(sBuf, 0)
' Set pin in PORT F = Input Mode
DeviceIoControl(hPort, PORT_F Or SET_INPUT, sBuf, 4, Nothing, 0, 0, IntPtr.Zero)
End If
End Sub
Private Sub cmdSetOutput_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSetOutput.Click
Dim sBuf(4) As Byte
Dim sInput, sOn, sOff As UInteger
If (hPort <> INVALID_HANDLE_VALUE) Then
' Initial variable = 0
sInput = 0
sOn = 0
sOff = 0
' Check in CheckBox
If (chkOutput0.Checked) Then
' Set pin position in to sInput
sInput = sInput + (1 << 0)
If (chkOnOut0.Checked) Then
' Set pin position to set logic = 1
sOn = sOn + (1 << 0)
Else
' Set pin position to set logic = 0
sOff = sOff + (1 << 0)
End If
End If
If (chkOutput1.Checked) Then
sInput = sInput + (1 << 1)
If (chkOnOut1.Checked) Then
sOn = sOn + (1 << 1)
Else
sOff = sOff + (1 << 1)
End If
End If
If (chkOutput2.Checked) Then
sInput = sInput + (1 << 2)
If (chkOnOut2.Checked) Then
sOn = sOn + (1 << 2)
Else
sOff = sOff + (1 << 2)
End If
End If
If (chkOutput3.Checked) Then
sInput = sInput + (1 << 3)
If (chkOnOut3.Checked) Then
sOn = sOn + (1 << 3)
Else
sOff = sOff + (1 << 3)
End If
End If
If (chkOutput4.Checked) Then
sInput = sInput + (1 << 4)
If (chkOnOut4.Checked) Then
sOn = sOn + (1 << 4)
Else
sOff = sOff + (1 << 4)
End If
End If
If (chkOutput5.Checked) Then
sInput = sInput + (1 << 5)
If (chkOnOut5.Checked) Then
sOn = sOn + (1 << 5)
Else
sOff = sOff + (1 << 5)
End If
End If
If (chkOutput6.Checked) Then
sInput = sInput + (1 << 6)
If (chkOnOut6.Checked) Then
sOn = sOn + (1 << 6)
Else
sOff = sOff + (1 << 6)
End If
End If
' Convert sInput(UInt32) to sBuf(Array of Byte)
BitConverter.GetBytes(sInput).CopyTo(sBuf, 0)
' Set pin position in PORT F = Output Mode
DeviceIoControl(hPort, PORT_F Or SET_OUTPUT, sBuf, 4, Nothing, 0, 0, IntPtr.Zero)
' Convert sOn(UInt32) to sBuf(Array of Byte)
BitConverter.GetBytes(sOn).CopyTo(sBuf, 0)
' Set Pin in PORT F = Logic 1
DeviceIoControl(hPort, PORT_F Or SET_PIN_ON, sBuf, 4, Nothing, 0, 0, IntPtr.Zero)
' Convert sOff(UInt32) to sBuf(Array of Byte)
BitConverter.GetBytes(sOff).CopyTo(sBuf, 0)
' Set Pin in PORT F = Logic 0
DeviceIoControl(hPort, PORT_F Or SET_PIN_OFF, sBuf, 4, Nothing, 0, 0, IntPtr.Zero)
End If
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
' Open GPIO Device
hPort = CreateFile("GIO1:", GENERIC_READ Or GENERIC_WRITE, 0, IntPtr.Zero, OPEN_EXISTING, 0, IntPtr.Zero)
If (hPort = INVALID_HANDLE_VALUE) Then
MsgBox("Open GIO1 Driver Fail")
End If
End Sub
Private Sub Form1_Closing(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
If (hPort <> INVALID_HANDLE_VALUE) Then
' Close GPIO Device
CloseHandle(hPort)
End If
End Sub
End Class