使用readfile读取usb端口时的vb6 WaitForSingleObject [英] vb6 WaitForSingleObject when reading usb port with readfile

查看:383
本文介绍了使用readfile读取usb端口时的vb6 WaitForSingleObject的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下代码在vb6的Public Sub Main()中。应该坐下来继续读取USB设备。它确实有效,但它非常不可靠。我有一个USB分析器,告诉我数据回来但没有进入WAIT_OBJECT_O的情况。



我试过让超时200毫秒似乎效果最好。



有没有更好的方法来写这个以便它总能得到usb数据并处理它?<​​br />


Hi the code below is in Public Sub Main() of vb6. It is supposed to sit and keep reading from a usb device. It does "work" however it is very unreliable. I have a USB analyzer that shows me that the data is coming back but not getting into the WAIT_OBJECT_O case.

I have tried making the timeout all sorts of things 200 milliseconds seems to work best.

Is there a better way to write this so that it will always get usb data and process it?

Do DoEvents

    If Form1.Terminate = False Then
        If MyDeviceDetected = True Then
            RecBuffer(4) = 0
            RecBuffer(5) = 0
            Result = ReadFile _
            (HIDHandle, _
            RecBuffer(0), _
            CLng(Capabilities.InputReportByteLength), _
            rdNum, _
            HIDOverlapped)

            errorNum = Err.LastDllError
            Debug.Print "err=" & Err.LastDllError

            ResultWait = WaitForSingleObject(EventObject, INFINITE)

            Select Case ResultWait
            Case WAIT_OBJECT_0
                Print #2, "RD-> " & rdNum & " - " & (RecBuffer(4) + 6) & " r= " & Result
                Form1.devStatus = RET_SUCCESS
                'CancelIo (ReadHandle)
            Case WAIT_TIMEOUT
                If (errorNum <> ERROR_IO_PENDING) Then ' returns 1167 when the device is removed
                'CancelIo (ReadHandle)
                    Print #2, "***TO"
                    Form1.devStatus = RET_USBDEVREVTOOLOW
                    If Form1.tmrFindHid.Enabled = True Then
                        MyDeviceDetected = False
                    End If
                End If
                'Exit Sub
            Case Else
                Print #2, "***BAD"
                'CancelIo (ReadHandle)
                Form1.devStatus = RET_USBDEVREVTOOLOW
                If Form1.tmrFindHid.Enabled = True Then
                    MyDeviceDetected = False
                End If
                'Exit Sub
            End Select
        'Loop Until res2 = 0

        End If
    Else
        Exit Sub
    End If
    'Sleep (10)
Loop

推荐答案

什么是EventObject?我建议你的问题在哪里......实际上是在阅读USB。



如果没有进入WAIT_OBJECT_0的情况,实际返回的值是多少?这也将提供有关如何解决问题的线索。



您可以查看此处的链接 http://www.dreamincode.net/forums/topic/11832-polling-the-usb-port-in-vb6/ [ ^ ]以更好的方式获得一些帮助。



但是,正如SA在评论中所说,VB6不是最好的工具实现这一点。



现代语言可以从MSDN的Express Versions中获得,在这种情况下,以下文章可能有用http://www.developerfusion.com/article/84338/making-usb-c-friendly/ [ ^ ]



以下是快速下载的链接[ ^ ]
What is EventObject ? I would suggest that's where your problem is ... with what is actually reading the USB.

If it's not getting into the WAIT_OBJECT_0 case, what value is actually being returned? That will also give clues on how to solve your issue.

You could have a look at the links here http://www.dreamincode.net/forums/topic/11832-polling-the-usb-port-in-vb6/[^] to get some assistance on better ways of doing this.

However, As SA says in their comment, VB6 is not the best tool to achieve this.

Modern languages are available from from MSDN in their Express Versions in which case the following article may be useful http://www.developerfusion.com/article/84338/making-usb-c-friendly/[^]

Here is a link for Express downloads[^]




事情是,我有时会进入WAIT_OBJECT_0但不可靠或每次交易都进入。

我发现原因是读取交易那个叫做获取数据的人是在一个循环中完成的,它阻止了main运行将读取调用移动到每隔这么多毫秒发生一次的计时器,然后主程序就可以运行并获取数据
Hi,
The thing is I do get into the WAIT_OBJECT_0 sometimes but not reliably or on every transaction.
I found out that the reason was the read transaction that was calling to get the data was done in a loop and it was preventing main from running I had to move the read call to a timer thatfires every so many milliseconds and then the main can run and get data


这篇关于使用readfile读取usb端口时的vb6 WaitForSingleObject的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆