Author Topic: aborting a USB Asynchronous transfer in .NET  (Read 14621 times)

acfasano

  • Member
  • ***
  • Posts: 6
aborting a USB Asynchronous transfer in .NET
« on: May 20, 2012, 11:36:42 am »
I am using your .NET HID subroutines to communicate to a HID device.
At a certain point, I have to begin waiting for an input report, which can take up to 60 seconds to come.
But depending on the user hitting a cancel button, I need to immediately send a "31h" code to the board so that it will send the input report immediately to the host.
It used to work in VB6 with the following code

recv_buf(0) = 1
result = ReadFile(ReadHandle, recv_buf(0), CLng(Capabilities.InputReportByteLength), NumberOfBytesRead, HIDOverlapped) bAlertable = True While (recv_buf(1) <> 240) 'And (Interromper = False)
  result2 = WaitForSingleObject(EventObject, 50)
  Call DisplayResultOfAPICall("WaitForSingleObject")
  DoEvents 'para botao esc poder fazer Interromper=true
  If Interromper = True And JaEnviouPacoteInterrupcao = False Then
       send_buf(0) = 1 'REPORT ID=1 The next bytes are data
       result = WriteFile(HIDHandle, send_buf(0), CLng(Capabilities.OutputReportByteLength), 1, 0)
       Call DisplayResultOfAPICall("WriteFile")       JaEnviouPacoteInterrupcao = True         
 End If
End while

Now I am using your .NET code (The most up to date one with Filestream) But no out packet is allowed to be sent while the computer waits for the IN Packet.
The code is below. Is there a way to really make sure an out packet goes out while waiting for the in packet ? What should I do ?


        fileStreamdevicedata.BeginRead(recv_buf, 0, recv_buf.Length, New AsyncCallback(AddressOf GetInputReport), recv_buf)

        While sucessoLeituraUSB = False
            System.Windows.Forms.Application.DoEvents()
            If Interromper = True And JaEnviouPacoteInterrupcao = False Then
                send_buf(0) = 1 'REPORT ID=1
                send_buf(1) = CByte(ConverteHexaParaDecimal("31"))
                fileStreamdevicedata.Write(send_buf, 0, MyHid.Capabilities.OutputReportByteLength)
                JaEnviouPacoteInterrupcao = True
            End If
        End While

Thank you so much.

Antonio Fasano

Jan Axelson

  • Administrator
  • Frequent Contributor
  • *****
  • Posts: 3033
    • Lakeview Research
Re: aborting a USB Asynchronous transfer in .NET
« Reply #1 on: May 21, 2012, 12:06:17 am »
BeginRead is asynchronous. It doesn't block the main program thread.

You can add code after BeginRead to send a report.

Jan