标题:VB定时器中断接收USB数据无法关闭窗体
只看楼主
chen3bing
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:507
专家分:104
注 册:2008-11-12
结帖率:91.22%
 问题点数:0 回复次数:2 
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
关闭窗体时先关闭定时器,可是还是会死掉。
请高手指教,谢谢!
搜索更多相关主题的帖子: the Count 定时器 关闭 数据 
2020-03-09 19:47
chen3bing
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:507
专家分:104
注 册:2008-11-12
得分:0 
'取得设备的标识代号
                hiddevice = CreateFile _
                    (DevicePathName, _
                    GENERIC_READ Or GENERIC_WRITE, _
                    (FILE_SHARE_READ Or FILE_SHARE_WRITE), _
                    Security, _
                    OPEN_EXISTING, _
                    FILE_FLAG_OVERLAPPED, _
                    0)
现在改了红色的部分,结果可以关闭窗体了,但是写函数又没用了。
2020-03-16 10:38
chen3bing
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:507
专家分:104
注 册:2008-11-12
得分:0 
写提示参数错误。
result = WriteFile _
    (hiddevice, _
    SendBuffer(0), _
   CLng(Capabilities.OutputReportByteLength), _
    NumberOfBytesWritten, _
    0)
NumberOfBytesWritten返回0,写不成功。
2020-03-16 11:16



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-499661-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.231441 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved