Author Topic: readfile does not block  (Read 14186 times)

marting@thinklogical.com

  • Member
  • ***
  • Posts: 17
readfile does not block
« on: September 26, 2013, 05:27:42 pm »
Hi,
In your
Hid.vb file
There is a Friend Overrides Sub Read
 
Then there is a call of
success = FileIO.ReadFile _
                           (readHandle, _
                             nonmanagedBuffer, _
                             inputReportBuffer.Length, _
                             numberOfBytesRead, _
                             nonmanagedOverlapped)
 
I would like this to be blocking.
I looked at the createfile but do not know why it is not blocking you mention that it will not block in the comments but I am not sure why it doesn’t.
Do you have any input on this?

I have tried various things with createfile as I was fairly sure this was the culprit.
Right now this read is essentially called in an endless loop and as it does not block it is giving old and new data all mixed up.

Thanks,
Martin

Jan Axelson

  • Administrator
  • Frequent Contributor
  • *****
  • Posts: 3033
    • Lakeview Research
Re: readfile does not block
« Reply #1 on: September 26, 2013, 06:54:37 pm »
See the documentation for ReadFile's lpOverlapped parameter. If the parameter is null, the function blocks.

marting@thinklogical.com

  • Member
  • ***
  • Posts: 17
Re: readfile does not block
« Reply #2 on: September 27, 2013, 10:11:32 am »
Hi,
I am using your project generic_hid_vb_46.zip as a base point
I have a device connected to the system that is spitting out data
If I use basically your project I get the data but it is jumbled because there are repeats of the data.
If I modify the success = FileIO.ReadFile _
                 (readHandle, _
                  nonmanagedBuffer, _
                  inputReportBuffer.Length, _
                  numberOfBytesRead, _
                  nonmanagedOverlapped)
to have
                'IntPtr.zero

in the overlapped then no data comes back.
I have a very similar project in vb6 that works for the most part, it was written like so (abbreviate).

UsbEventObject = CreateEvent(usbSecurity, CInt(True), CInt(False), "rdUsb")
            HIDOverlapped.Offset = 0
            HIDOverlapped.OffsetHigh = 0
            HIDOverlapped.hEvent = UsbEventObject

Public Sub WaitForUsbRet()
Dim Result As Long
Dim ResultWait As Long
Dim res2 As Integer
Dim RecBuffer(65) As Byte
Dim rdNum As Long
Dim i As Integer
Dim res3 As Long
res2 = 1

Form1.lstBoxRec.AddItem "StartWait"

    Do
'        Form1.ShpStatus.BackColor = Form1.ShpStatus.BackColor + 1
       
        DoEvents
       
        RecBuffer(4) = 0
        RecBuffer(5) = 0
        Result = ReadFile _
        (HIDHandle, _
        RecBuffer(0), _
        CLng(Capabilities.InputReportByteLength), _
        rdNum, _
        HIDOverlapped)

        ResultWait = WaitForSingleObject(UsbEventObject, 500)

        Select Case ResultWait
            Case WAIT_TIMEOUT
'            ' Timeout error.
'                Form1.lstBoxRec.AddItem "Out"
                CancelIo (ReadHandle)
                Form1.devStatus = RET_USBDEVREVTOOLOW
                DoEvents
            Case WAIT_FAILED Or WAIT_ABANDONED
'                Form1.lstBoxRec.AddItem "FAIL"
                Form1.devStatus = RET_USBDEVREVTOOLOW
                DoEvents
            Case WAIT_OBJECT_0
'    '        ' Success
'    '        ' Use the report data.
'                Form1.lstBoxRec.AddItem "Waited"
                rdNum = (RecBuffer(4) + 6)
                If rdNum > 65 Then rdNum = 65
                If (rdNum > 5) Then
                    For i = 1 To rdNum
                       Form1.putInCircBuff (RecBuffer(i))
                    Next i
                End If
'                Form1.lstBoxRec.ListIndex = Form1.lstBoxRec.ListCount - 1
                If (rdNum > 0) Then Form1.tmrCommHndl.Enabled = True
                DoEvents
                Form1.devStatus = RET_SUCCESS
            Case Else
'                Form1.lstBoxRec.AddItem "Else"
                CancelIo (ReadHandle)
                Form1.devStatus = RET_USBDEVREVTOOLOW
        End Select
'        res3 = ResetEvent(EventObject)
    Loop Until res2 = 0
End Sub

I understand that the problems I am having with the vb.net project are basically my lack of understanding, such as making a friend function for the read and the fact that the application does not debug well in visual basic 2010 express.

Any advice you can give would be appreciated.

Thanks,
Martin

marting@thinklogical.com

  • Member
  • ***
  • Posts: 17
Re: readfile does not block
« Reply #3 on: September 30, 2013, 03:18:51 pm »
Hi,
I am sorry to bother you with this, I have simplified things down so that I can hope to be more clear.

I have in vb.net express 2010

....

Imports System.Threading

.......

Private usbReaderThread As Thread
Public recBuffer(65) As Int32

...in frmmain.startup
    usbReaderThread = New Thread(AddressOf usbReaderTask)
    usbReaderThread.IsBackground = True
    usbReaderThread.Start()
.....

    Private Sub usbReaderTask()
        Dim numberOfBytesRead As Int32
        Dim result As Int32
        Dim success As Boolean
        Dim i As Integer
        Dim nonmanagedBuffer As IntPtr
        Do
            If (myDeviceDetected = True) Then
                If (Not (readHandle.IsClosed) And Not (readHandle.IsInvalid)) Then
                    success = FileIO.ReadFile _
                             (readHandle, _
                              CType(recBuffer(1), IntPtr), _
                              recBuffer.Length, _
                              numberOfBytesRead, _
                              IntPtr.Zero)

                    For i = 1 To numberOfBytesRead
                        putInCircBuff(CByte(i))
                    Next i
                End If
                Thread.Sleep(10)
            End If
        Loop
    End Sub


I have done all sorts of things to the readfile to get it to open in different modes, I call your find hid to get the handle then let the thread run.  It never returns data and I do not know why.
The data is coming in I can see it in the vb6 version.  Do you have any idea why?

Thanks,
Martin

Jan Axelson

  • Administrator
  • Frequent Contributor
  • *****
  • Posts: 3033
    • Lakeview Research
Re: readfile does not block
« Reply #4 on: October 01, 2013, 07:31:23 pm »
Readfile returns when the HID driver has received a report with a valid number of bytes as defined by the devices's report descriptor. Post the report descriptor.

marting@thinklogical.com

  • Member
  • ***
  • Posts: 17
Re: readfile does not block
« Reply #5 on: October 02, 2013, 10:16:46 am »
I posted more including descriptors in a topic called "vb readfile woes"