第一站程式碼
中文版程式碼
適用程式初學者學習
Imports Microsoft.VisualBasic.PowerPacks
Imports LibUsbDotNet
Imports LibUsbDotNet.Main
Public Class Form1
Dim 綠燈資料() As Integer = {&H1, &H2, &H4, &H8, &H10, &H20, &H40, &H80}
Dim 按鈕狀態 As String
Dim 計數值 As Integer
Dim 介面卡 As UsbDevice
Function 綠燈(ByVal 位置) As OvalShape
Return ShapeContainer1.Shapes(位置 + 8)
End Function
Function 紅燈(ByVal 位置) As OvalShape
Return ShapeContainer1.Shapes(位置)
End Function
Sub 綠燈亮起(ByVal 數值)
For 連除計數值 = 0 To 7
綠燈(連除計數值).BackStyle = BackStyle.Opaque
If 數值 Mod 2 = 1 Then
綠燈(連除計數值).BackColor = Color.FromArgb(0, 255, 0)
Else
綠燈(連除計數值).BackColor = Color.FromArgb(0, 128, 0)
End If
數值 = 數值 \ 2
Next
End Sub
Sub 紅燈亮起(ByVal 數值)
For 連除計數值 = 0 To 7
紅燈(連除計數值).BackStyle = BackStyle.Opaque
If 數值 Mod 2 = 1 Then
紅燈(連除計數值).BackColor = Color.FromArgb(255, 0, 0)
Else
紅燈(連除計數值).BackColor = Color.FromArgb(128, 0, 0)
End If
數值 = 數值 \ 2
Next
End Sub
Sub 介面卡控制(ByVal 資料, ByVal 控制)
介面卡.ControlTransfer(New UsbSetupPacket(32, 9, 0, 0, 0), New Byte() {資料, 控制}, 8, 0)
End Sub
Private Sub 綠燈按鈕_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 綠燈按鈕.Click
按鈕狀態 = "綠燈"
計數值 = 0
End Sub
Private Sub 紅燈按鈕_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 紅燈按鈕.Click
按鈕狀態 = "紅燈"
計數值 = 0
End Sub
Private Sub 退出按鈕_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 退出按鈕.Click
按鈕狀態 = "退出"
End Sub
Private Sub 計時_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 計時.Tick
時間.Text = "Current Time : " + Now.ToString("HH:mm:ss")
介面卡 = UsbDevice.OpenUsbDevice(New UsbDeviceFinder(&H1234, &H6789))
If 介面卡 Is Nothing Then
For 迴圈計數值 = 0 To 7
綠燈(迴圈計數值).BackStyle = BackStyle.Transparent
紅燈(迴圈計數值).BackStyle = BackStyle.Transparent
Next
If 按鈕狀態 = "退出" Then
End
End If
Else
綠燈亮起(0)
紅燈亮起(0)
介面卡控制(0, 0)
介面卡控制(0, &H10)
If 按鈕狀態 = "綠燈" And 計數值 < 綠燈資料.Length Then
綠燈亮起(綠燈資料(計數值))
介面卡控制(綠燈資料(計數值), 0)
計數值 = 計數值 + 1
ElseIf 按鈕狀態 = "紅燈" And 計數值 < 8 Then
紅燈亮起(2 ^ 計數值)
介面卡控制(2 ^ 計數值, &H20)
介面卡控制(2 ^ 計數值, &H30)
計數值 = 計數值 + 1
ElseIf 按鈕狀態 = "退出" Then
介面卡.Close()
End
End If
End If
End Sub
End Class
VB6版程式碼
給仍在使用VB6的讀者參考,因VB6已經淘汰且不再更新,建議改用新版Visual Studio。
透過Visual Studio的提示功能,簡化程式輸入難度,讀者學習時事半功倍。雖然程式碼行數多,但許多程式碼是自動產生的,經過實際測驗,使用Visual Studio能夠比VB6省下約一半的時間。
Private Declare Function OpenUsbDevice Lib "USBIO.dll" (ByVal vid As Integer, ByVal pid As Integer) As Boolean
Private Declare Sub OutDataCtrl Lib "USBIO.dll" (ByVal x As Byte, ByVal y As Byte)
Private Declare Sub CloseUsbDevice Lib "USBIO.dll" ()
Dim btn, cnt As Integer
Dim data
Sub ShowG(n)
For i = 0 To 7
G(i).BackStyle = 1
If n Mod 2 = 1 Then
G(i).BackColor = RGB(0, 255, 0)
Else
G(i).BackColor = RGB(0, 128, 0)
End If
n = n \ 2
Next
End Sub
Sub ShowR(n)
For i = 0 To 7
R(i).BackStyle = 1
If n Mod 2 = 1 Then
R(i).BackColor = RGB(255, 0, 0)
Else
R(i).BackColor = RGB(128, 0, 0)
End If
n = n \ 2
Next
End Sub
Private Sub Command1_Click()
btn = 1
cnt = 0
End Sub
Private Sub Command2_Click()
btn = 2
cnt = 0
End Sub
Private Sub Command3_Click()
btn = 3
End Sub
Private Sub Timer1_Timer()
data = Array(&H1, &H2, &H4, &H8, &H10, &H20, &H40, &H80)
Label1.Caption = vbCrLf + "Current Time : " + Time$
If OpenUsbDevice(&H1234, &H6789) = False Then
For i = 0 To 7
G(i).BackStyle = 0
R(i).BackStyle = 0
Next
If btn = 3 Then
End
End If
Else
ShowG (0)
ShowR (0)
OutDataCtrl 0, 0
OutDataCtrl 0, &H10
If btn = 1 And cnt <= UBound(data) Then
ShowG (data(cnt))
OutDataCtrl data(cnt), 0
cnt = cnt + 1
ElseIf btn = 2 And cnt < 8 Then
ShowR (2 ^ cnt)
OutDataCtrl 2 ^ cnt, &H20
OutDataCtrl 2 ^ cnt, &H30
cnt = cnt + 1
ElseIf btn = 3 Then
CloseUsbDevice
End
End If
End If
End Sub
適用程式初學者學習
Imports Microsoft.VisualBasic.PowerPacks
Imports LibUsbDotNet
Imports LibUsbDotNet.Main
Public Class Form1
Dim 綠燈資料() As Integer = {&H1, &H2, &H4, &H8, &H10, &H20, &H40, &H80}
Dim 按鈕狀態 As String
Dim 計數值 As Integer
Dim 介面卡 As UsbDevice
Function 綠燈(ByVal 位置) As OvalShape
Return ShapeContainer1.Shapes(位置 + 8)
End Function
Function 紅燈(ByVal 位置) As OvalShape
Return ShapeContainer1.Shapes(位置)
End Function
Sub 綠燈亮起(ByVal 數值)
For 連除計數值 = 0 To 7
綠燈(連除計數值).BackStyle = BackStyle.Opaque
If 數值 Mod 2 = 1 Then
綠燈(連除計數值).BackColor = Color.FromArgb(0, 255, 0)
Else
綠燈(連除計數值).BackColor = Color.FromArgb(0, 128, 0)
End If
數值 = 數值 \ 2
Next
End Sub
Sub 紅燈亮起(ByVal 數值)
For 連除計數值 = 0 To 7
紅燈(連除計數值).BackStyle = BackStyle.Opaque
If 數值 Mod 2 = 1 Then
紅燈(連除計數值).BackColor = Color.FromArgb(255, 0, 0)
Else
紅燈(連除計數值).BackColor = Color.FromArgb(128, 0, 0)
End If
數值 = 數值 \ 2
Next
End Sub
Sub 介面卡控制(ByVal 資料, ByVal 控制)
介面卡.ControlTransfer(New UsbSetupPacket(32, 9, 0, 0, 0), New Byte() {資料, 控制}, 8, 0)
End Sub
Private Sub 綠燈按鈕_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 綠燈按鈕.Click
按鈕狀態 = "綠燈"
計數值 = 0
End Sub
Private Sub 紅燈按鈕_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 紅燈按鈕.Click
按鈕狀態 = "紅燈"
計數值 = 0
End Sub
Private Sub 退出按鈕_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 退出按鈕.Click
按鈕狀態 = "退出"
End Sub
Private Sub 計時_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 計時.Tick
時間.Text = "Current Time : " + Now.ToString("HH:mm:ss")
介面卡 = UsbDevice.OpenUsbDevice(New UsbDeviceFinder(&H1234, &H6789))
If 介面卡 Is Nothing Then
For 迴圈計數值 = 0 To 7
綠燈(迴圈計數值).BackStyle = BackStyle.Transparent
紅燈(迴圈計數值).BackStyle = BackStyle.Transparent
Next
If 按鈕狀態 = "退出" Then
End
End If
Else
綠燈亮起(0)
紅燈亮起(0)
介面卡控制(0, 0)
介面卡控制(0, &H10)
If 按鈕狀態 = "綠燈" And 計數值 < 綠燈資料.Length Then
綠燈亮起(綠燈資料(計數值))
介面卡控制(綠燈資料(計數值), 0)
計數值 = 計數值 + 1
ElseIf 按鈕狀態 = "紅燈" And 計數值 < 8 Then
紅燈亮起(2 ^ 計數值)
介面卡控制(2 ^ 計數值, &H20)
介面卡控制(2 ^ 計數值, &H30)
計數值 = 計數值 + 1
ElseIf 按鈕狀態 = "退出" Then
介面卡.Close()
End
End If
End If
End Sub
End Class
VB6版程式碼
給仍在使用VB6的讀者參考,因VB6已經淘汰且不再更新,建議改用新版Visual Studio。
透過Visual Studio的提示功能,簡化程式輸入難度,讀者學習時事半功倍。雖然程式碼行數多,但許多程式碼是自動產生的,經過實際測驗,使用Visual Studio能夠比VB6省下約一半的時間。
Private Declare Function OpenUsbDevice Lib "USBIO.dll" (ByVal vid As Integer, ByVal pid As Integer) As Boolean
Private Declare Sub OutDataCtrl Lib "USBIO.dll" (ByVal x As Byte, ByVal y As Byte)
Private Declare Sub CloseUsbDevice Lib "USBIO.dll" ()
Dim btn, cnt As Integer
Dim data
Sub ShowG(n)
For i = 0 To 7
G(i).BackStyle = 1
If n Mod 2 = 1 Then
G(i).BackColor = RGB(0, 255, 0)
Else
G(i).BackColor = RGB(0, 128, 0)
End If
n = n \ 2
Next
End Sub
Sub ShowR(n)
For i = 0 To 7
R(i).BackStyle = 1
If n Mod 2 = 1 Then
R(i).BackColor = RGB(255, 0, 0)
Else
R(i).BackColor = RGB(128, 0, 0)
End If
n = n \ 2
Next
End Sub
Private Sub Command1_Click()
btn = 1
cnt = 0
End Sub
Private Sub Command2_Click()
btn = 2
cnt = 0
End Sub
Private Sub Command3_Click()
btn = 3
End Sub
Private Sub Timer1_Timer()
data = Array(&H1, &H2, &H4, &H8, &H10, &H20, &H40, &H80)
Label1.Caption = vbCrLf + "Current Time : " + Time$
If OpenUsbDevice(&H1234, &H6789) = False Then
For i = 0 To 7
G(i).BackStyle = 0
R(i).BackStyle = 0
Next
If btn = 3 Then
End
End If
Else
ShowG (0)
ShowR (0)
OutDataCtrl 0, 0
OutDataCtrl 0, &H10
If btn = 1 And cnt <= UBound(data) Then
ShowG (data(cnt))
OutDataCtrl data(cnt), 0
cnt = cnt + 1
ElseIf btn = 2 And cnt < 8 Then
ShowR (2 ^ cnt)
OutDataCtrl 2 ^ cnt, &H20
OutDataCtrl 2 ^ cnt, &H30
cnt = cnt + 1
ElseIf btn = 3 Then
CloseUsbDevice
End
End If
End If
End Sub