注册 登录
编程论坛 VB6论坛

VB定时器中断接收USB数据无法关闭窗体

chen3bing 发布于 2020-03-09 19:47, 1541 次点击
我写了个USB通讯的小程序,用定时器中断读数据正常。
可是点击关闭按钮,就死掉了。
我的程序如下:
读程序:
Private Sub ReadReport1()

'Read data from the device.

Dim Count
Dim NumberOfBytesRead As Long

'Allocate a buffer for the report.
'Byte 0 is the report ID.

Dim ReadBuffer() As Byte
Dim UBoundReadBuffer As Integer


'ReadFile是区块调用
'这个应用程序会"挂"住,直到设备送出所需的数据量为止
'为了避免"挂"了,必须确定设备总是有数据来加以送出

Dim ByteValue As String


      

      
'The ReadBuffer array begins at 0, so subtract 1 from the number of bytes.
'ReadFile数组是从0开始的,因此须将字节的数目减去1

ReDim ReadBuffer(Capabilities.InputReportByteLength - 1)



'Do an overlapped ReadFile.
'The function returns immediately, even if the data hasn't been received yet.
'传读取缓冲区的第一个字节的地址

result = ReadFile _
    (hiddevice, _
    ReadBuffer(0), _
    CLng(Capabilities.InputReportByteLength), _
    NumberOfBytesRead, _
    HIDOverlapped)










TextIR.Text = ""
For Count = 1 To UBound(ReadBuffer)
   
    'Add a leading 0 to values 0 - Fh.
   
    If Len(Hex$(ReadBuffer(Count))) < 2 Then
        ByteValue = "0" & Hex$(ReadBuffer(Count))
    Else
        ByteValue = Hex$(ReadBuffer(Count))
    End If
   
   
   
    'Display the received bytes in the text box.
    '将所接收到的字节放置在文本框内
   
    TextIR.SelStart = Len(TextIR.Text)
    'TextIR.SelText = ByteValue & vbCrLf
    TextIR.SelText = ByteValue & " "
Next Count




End Sub
定时器程序:
Private Sub Timer1_Timer()

  
    Call ReadReport1
  

End Sub
关闭窗体时先关闭定时器,可是还是会死掉。
请高手指教,谢谢!
2 回复
#2
chen3bing2020-03-16 10:38
'取得设备的标识代号
                hiddevice = CreateFile _
                    (DevicePathName, _
                    GENERIC_READ Or GENERIC_WRITE, _
                    (FILE_SHARE_READ Or FILE_SHARE_WRITE), _
                    Security, _
                    OPEN_EXISTING, _
                    FILE_FLAG_OVERLAPPED, _
                    0)
现在改了红色的部分,结果可以关闭窗体了,但是写函数又没用了。
#3
chen3bing2020-03-16 11:16
写提示参数错误。
result = WriteFile _
    (hiddevice, _
    SendBuffer(0), _
   CLng(Capabilities.OutputReportByteLength), _
    NumberOfBytesWritten, _
    0)
NumberOfBytesWritten返回0,写不成功。
1