Ports
May 21, 2013, 05:34:48 am *
Welcome, Guest. Please login or register.

Login with username, password and session length
News: SMF - Just Installed!
 
   Home   Help Search Login Register  
Welcome to the PORTS Forum. Please post your questions, answers, and comments relating to USB and other hardware interfaces in the appropriate forums below. - Jan
Pages: [1]
  Print  
Author Topic: vb6 events  (Read 2453 times)
marting@thinklogical.com
Member
*
Posts: 9


« on: November 07, 2011, 05:08:25 pm »

Hi,
I have the following code -
What I want it to do is wait for a usb in packet, and use it.  that is pretty clear.  I want it to timeout if no data shows up.  This wont work because the WaitForSingleObject is called after the clocking ReadFile.
Is there a better wqay to do this?  Maybe not read file?  Of a simpler timeout?  I cannot use doevents to run my own time out because the ReadFile is blocking.
Thanks,
Martin

    Security.lpSecurityDescriptor = 0
    Security.bInheritHandle = CInt(True)
    Security.nLength = Len(Security)
   
    EventObject = CreateEvent(Security, CInt(False), CInt(True), "rdUsb")
    HIDOverlapped.Offset = 0
    HIDOverlapped.OffsetHigh = 0
    HIDOverlapped.hEvent = EventObject
   
    'Do
        DoEvents
        RecBuffer(4) = 0
        RecBuffer(5) = 0
        Result = ReadFile _
        (HIDHandle, _
        RecBuffer(0), _
        CLng(Capabilities.InputReportByteLength), _
        rdNum, _
        HIDOverlapped)

        Result = WaitForSingleObject(EventObject, 500)

        Select Case Result
        Case WAIT_OBJECT_0
        ' Success
        ' Use the report data.
            rdNum = (RecBuffer(4) + 5)
...
..
.
Logged
Jan Axelson
Administrator
Frequent Contributor
*****
Posts: 1626


WWW
« Reply #1 on: November 07, 2011, 05:33:19 pm »

Here is the description of WaitForSingleObject:

Waits until the specified object is in the signaled state or the time-out interval elapses.

How is this different from:

I want it to timeout if no data shows up.

Jan
Logged
marting@thinklogical.com
Member
*
Posts: 9


« Reply #2 on: November 08, 2011, 08:04:06 am »

Hi,
The readfile is blocking, if no data comes in then the process never gets to WaitForSingleObject
Logged
Jan Axelson
Administrator
Frequent Contributor
*****
Posts: 1626


WWW
« Reply #3 on: November 08, 2011, 11:33:22 am »

The ReadFile should block only until the timeout. Use a zero timeout if you want it to return immediately if no data.

I have example code here:

http://www.lvr.com/hidpage.htm#MyExampleCode

Jan
Logged
marting@thinklogical.com
Member
*
Posts: 9


« Reply #4 on: November 08, 2011, 12:54:12 pm »

I'm sorry, but I am looking in usbhid20.zip
I do not see where you set the timeout.
I hate to bug you with this, can you please tell me where the timeout is specified?
Thanks
Logged
Jan Axelson
Administrator
Frequent Contributor
*****
Posts: 1626


WWW
« Reply #5 on: November 08, 2011, 01:43:26 pm »

The most recent versions of my code are here:

http://www.lvr.com/hidpage.htm#MyExampleCode

The timeout is the second parameter in WaitForSingleObject. Or search the project for "timeout"

Jan
Logged
marting@thinklogical.com
Member
*
Posts: 9


« Reply #6 on: November 09, 2011, 03:07:13 pm »

The readfile was not returning immediately becaue I did a createfile with file_flag_normal instead of fil_flag_overlapped.
When you do this the readfile is blocking
Because of this the code is never getting to the waitforsingleobject

Such a trivial change causes such a non-trvial outcome
Logged
Jan Axelson
Administrator
Frequent Contributor
*****
Posts: 1626


WWW
« Reply #7 on: November 09, 2011, 03:09:53 pm »

Yes, you need to use overlapped IO. I'm glad you got it working.

Jan
Logged
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC Valid XHTML 1.0! Valid CSS!