演示.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)      '将输入的十六进制格式字符数据转成数值数据

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)      '将输入的十六进制格式字符数据转成数值数据

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)      '将输入的十六进制格式字符数据转成数值数据

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)      '将输入的十六进制格式字符数据转成数值数据

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)      '将输入的十六进制格式字符数据转成数值数据

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已打开则关闭

Unload Me    '关闭窗体

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


'设置设备插拔通知

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

    MsgBox "设置设备插拔通知失败", vbExclamation

End If

enablebtn (mOpen)

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

'置D/A输出为零

Call mStrtoVal("904000", buffer, 3)                      '将输入的十六进制格式字符数据转成数值数据

bopen = CH341StreamI2C(mIndex, 3, buffer, 1, iBuff)      '置D/A输出为零

End Sub

Private Sub CH341_NOTIFY_ROUTINE_KeyUp(KeyCode As Integer, Shift As Integer)  '设备插拔通知处理程序

    mIndex = 0

    iEventStatus = KeyCode '插拔事件

    If (iEventStatus = CH341_DEVICE_ARRIVAL) Then ' 设备插入事件,CH341_DEVICE_ARRIVAL=3已经插入

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

            MsgBox "打开设备失败!", vbOK

            mOpen = False

        Else

            mOpen = True  '打开成功

        End If

    ElseIf (iEventStatus = CH341_DEVICE_REMOVE) Then ' 设备拔出事件,CH341_DEVICE_REMOVE=0已经拔出

        CH341CloseDevice (mIndex)

        mOpen = False

    End If

    enablebtn (mOpen) '设备打开,按钮可用,设备没打开,按钮禁用

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 '窗体标题显示

        Form1.Caption = "USB已连接"

    Else

        Form1.Caption = "USB未连接"

    End If

  End With

End Sub