Author Topic: vb6 events  (Read 30556 times)

marting@thinklogical.com

  • Member
  • ***
  • Posts: 17
vb6 events
« 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)
...
..
.

Jan Axelson

  • Administrator
  • Frequent Contributor
  • *****
  • Posts: 3033
    • Lakeview Research
Re: vb6 events
« 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

marting@thinklogical.com

  • Member
  • ***
  • Posts: 17
Re: vb6 events
« 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

Jan Axelson

  • Administrator
  • Frequent Contributor
  • *****
  • Posts: 3033
    • Lakeview Research
Re: vb6 events
« 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

marting@thinklogical.com

  • Member
  • ***
  • Posts: 17
Re: vb6 events
« 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

Jan Axelson

  • Administrator
  • Frequent Contributor
  • *****
  • Posts: 3033
    • Lakeview Research
Re: vb6 events
« 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

marting@thinklogical.com

  • Member
  • ***
  • Posts: 17
Re: vb6 events
« 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

Jan Axelson

  • Administrator
  • Frequent Contributor
  • *****
  • Posts: 3033
    • Lakeview Research
Re: vb6 events
« 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