演示.bmp

Option Explicit

Dim iBuff As Byte

Dim buffer As arrRBuffer

Private Sub Command1_Click() ? ? ? ? ? ?'寫入D/A

Dim ss As String

ss = Hex(HScroll1.Value)

Call mStrtoVal("9040" & ss, buffer, 3) ? ? ?'將輸入的十六進(jìn)制格式字符數(shù)據(jù)轉(zhuǎn)成數(shù)值數(shù)據(jù)

bopen = CH341StreamI2C(mIndex, 3, buffer, 1, iBuff)

Label5.Caption = "輸出=" & HScroll1.Value & "/輸出電壓=" & Format((HScroll1 / 51), "0.0")

End Sub

Private Sub Command2_Click() ? ? ? ? ? 'AIN0

Label1.Caption = ""

Call mStrtoVal("9040", buffer, 2) ? ? ?'將輸入的十六進(jìn)制格式字符數(shù)據(jù)轉(zhuǎn)成數(shù)值數(shù)據(jù)

bopen = CH341StreamI2C(mIndex, 2, buffer, 1, iBuff)

bopen = CH341StreamI2C(mIndex, 2, buffer, 1, iBuff)

Label1.Caption = iBuff

End Sub

Private Sub Command3_Click() ? ? ? ? ? 'AIN1

Call mStrtoVal("9041", buffer, 2) ? ? ?'將輸入的十六進(jìn)制格式字符數(shù)據(jù)轉(zhuǎn)成數(shù)值數(shù)據(jù)

bopen = CH341StreamI2C(mIndex, 2, buffer, 1, iBuff)

bopen = CH341StreamI2C(mIndex, 2, buffer, 1, iBuff)

Label2.Caption = iBuff

End Sub

Private Sub Command4_Click() ? ? ? ? ?'AIN2

Call mStrtoVal("9042", buffer, 2) ? ? ?'將輸入的十六進(jìn)制格式字符數(shù)據(jù)轉(zhuǎn)成數(shù)值數(shù)據(jù)

bopen = CH341StreamI2C(mIndex, 2, buffer, 1, iBuff)

bopen = CH341StreamI2C(mIndex, 2, buffer, 1, iBuff)

Label3.Caption = iBuff

End Sub

Private Sub Command5_Click() ? ? ? ? ?'AIN3

Call mStrtoVal("9043", buffer, 2) ? ? ?'將輸入的十六進(jìn)制格式字符數(shù)據(jù)轉(zhuǎn)成數(shù)值數(shù)據(jù)

bopen = CH341StreamI2C(mIndex, 2, buffer, 1, iBuff)

bopen = CH341StreamI2C(mIndex, 2, buffer, 1, iBuff)

Label4.Caption = Format(iBuff / 51, "0.0")

End Sub

Private Sub Command6_Click() ? ? ? ? ? 'QUIT

CH341SetDeviceNotify 0, vbNullString, 0&

If (mOpen = True) Then CH341CloseDevice (mIndex) ? ? ? ?'如果CH341已打開則關(guān)閉

Unload Me ? ?'關(guān)閉窗體

End Sub

Private Sub HScroll1_Change()

Label5.Caption = "輸出=" & HScroll1.Value

End Sub

Private Sub Form_Load()

mIndex = 0

hopen = CH341OpenDevice(mIndex)

If (hopen = INVALID_HANDLE_VALUE) Then

? ? mOpen = False

Else

? ? mOpen = True

End If

Label1.Caption = ""

Label2.Caption = ""

Label3.Caption = ""

Label4.Caption = ""

Label5.Caption = ""

HScroll1.Value = 40


'設(shè)置設(shè)備插拔通知

If CH341SetDeviceNotify(mIndex, vbNullString, AddressOf mPCH341_NOTIFY_ROUTINE) = False Then

? ? MsgBox "設(shè)置設(shè)備插拔通知失敗", vbExclamation

End If

enablebtn (mOpen)

bopen = CH341SetStream(mIndex, &H81) ? ? ? ? ? ? ? ? ? ?'設(shè)置同步串口流模式,高位在前,本例只能用&H80或&H81

'置D/A輸出為零

Call mStrtoVal("904000", buffer, 3) ? ? ? ? ? ? ? ? ? ? ?'將輸入的十六進(jìn)制格式字符數(shù)據(jù)轉(zhuǎn)成數(shù)值數(shù)據(jù)

bopen = CH341StreamI2C(mIndex, 3, buffer, 1, iBuff) ? ? ?'置D/A輸出為零

End Sub

Private Sub CH341_NOTIFY_ROUTINE_KeyUp(KeyCode As Integer, Shift As Integer) ?'設(shè)備插拔通知處理程序

? ? mIndex = 0

? ? iEventStatus = KeyCode '插拔事件

? ? If (iEventStatus = CH341_DEVICE_ARRIVAL) Then ' 設(shè)備插入事件,CH341_DEVICE_ARRIVAL=3已經(jīng)插入

? ? ? ? If (CH341OpenDevice(mIndex) = INVALID_HANDLE_VALUE) Then ? ? 'INVALID_HANDLE_VALUE=-1

? ? ? ? ? ? MsgBox "打開設(shè)備失敗!", vbOK

? ? ? ? ? ? mOpen = False

? ? ? ? Else

? ? ? ? ? ? mOpen = True ?'打開成功

? ? ? ? End If

? ? ElseIf (iEventStatus = CH341_DEVICE_REMOVE) Then ' 設(shè)備拔出事件,CH341_DEVICE_REMOVE=0已經(jīng)拔出

? ? ? ? CH341CloseDevice (mIndex)

? ? ? ? mOpen = False

? ? End If

? ? enablebtn (mOpen) '設(shè)備打開,按鈕可用,設(shè)備沒打開,按鈕禁用

End Sub

Public Sub enablebtn(ByVal bEnable As Boolean) ?'bEnable=true :各窗體按鈕可用 ;=false:enable:各窗體按鈕禁用

? With Form1

? ? .Command1.Enabled = bEnable

? ? .Command2.Enabled = bEnable

? ? .Command3.Enabled = bEnable

? ? .Command4.Enabled = bEnable

? ? .Command5.Enabled = bEnable

? ? .Frame1.Enabled = bEnable

? ? .Frame2.Enabled = bEnable

? ? .HScroll1.Enabled = bEnable

? ? If (bEnable = True) Then '窗體標(biāo)題顯示

? ? ? ? Form1.Caption = "USB已連接"

? ? Else

? ? ? ? Form1.Caption = "USB未連接"

? ? End If

? End With

End Sub