VB定时器中断接收USB数据无法关闭窗体
我写了个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
关闭窗体时先关闭定时器,可是还是会死掉。
请高手指教,谢谢!