Click here to Skip to main content
15,891,835 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am using Bulk USB driver (sample from MS WinDDK). We have application software developed in Win32 which communicates our device through USB port (USB host at PC side and USB device at hardware side). In Application software, we are using CreateFile, WriteFile and ReadFile API to communicate with USB device. Right now we are calling ReadFile function to receive data from USB port But now we want to receive data on event (not forcefully calling ReadFile). In Serial Communication there is EV_RXCHAR event which generates event when data is available on port. In C# Serial class, 'DataReceived' event is used for this purpose. But For USB, i could not find any suitable method/architecture which is used to receive data on event.

Please kindly suggest some solution.
Posted

1 solution

What you are talking about is asynchronous data processing.

To implement this you need to use ReadFile in asynchronous (OVERLAPPED) mode.

1. Create an event.
2. Create an OVERLAPPED structure containing a handle to this event
3. Pass this to your ReadFile function
4. In a separate thread wait on the event handle. When the event is signaled process the data that has arrived.
5. Use GetOverlappedResult() to find out how many bytes have arrived. http://msdn.microsoft.com/en-us/library/windows/desktop/ms683209(v=vs.85).aspx[^]



This may be helpful
http://msdn.microsoft.com/en-us/library/windows/desktop/aa365683(v=vs.85).aspx[^]
 
Share this answer
 
v3
Comments
pravin29 3-Apr-12 10:05am    
Thank you for your reply.
I did this. Here is code -

Collapse | Copy Code


g_hw_USBWrite = CreateFile (UsbWritePipe,
GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL, // no SECURITY_ATTRIBUTES structure
OPEN_EXISTING, // No special create flags
0, // No special attributes
NULL); // No template file

hEventObject = CreateEvent((LPSECURITY_ATTRIBUTES)NULL,FALSE,TRUE,"");
HIDOverlapped.hEvent = hEventObject;
HIDOverlapped.Offset = 0;
HIDOverlapped.OffsetHigh = 0;

InputReportBuffer[0] = 0;

int s_i_NoOfBytes = 1;
Result = ReadFile(g_hw_USBRead,InputReportBuffer,s_i_NoOfBytes,&BytesRead,(LPOVERLAPPED) &HIDOverlapped);
Result = WaitForSingleObject(hEventObject,1000);

switch (Result)
{
case WAIT_OBJECT_0:
{
// Success;
// Use the report data;
break;
}
case WAIT_TIMEOUT:
{
// Timeout error;
//Cancel the read operation.
CancelIo(g_hw_USBRead);
break;
}
default:
{
// Undefined error;
//Cancel the read operation.
CancelIo(g_hw_USBRead);
break;
}
}


Actually problem is, application software doesn't know how much data (or bytes) to receive from device. In overlapped IO there is necessity to call ReadFile first and then wait for signaling. I just want DataReceived event similar to serial (which invoke by OS)
[no name] 3-Apr-12 19:13pm    
1. The event being signaled alerts that data has arrived. You can create your own DataReceived event.
2. GetOverlappedResult() tells how much data.
3. WaitForSingleObject() must be called inside a thread to get all the data. In your example only one lot of data can be received.
4. ReadFile() operates differently when OVERLAPPED is used.
pravin29 4-Apr-12 1:27am    
1. The event being signaled alerts that data has arrived. You can create your own DataReceived event.
>> Let's assume device sent 1 byte of data on USB port. Now how could application software signaled this event. In VC++, For serial, communication there is EV_RXCHAR event. When this event signaled then we call ReadFile. In C# direct event provided from Serial class.

Serial _serialVP = new SerialPort("COM7");

_serialVP.BaudRate = 115200;
_serialVP.Parity = Parity.None;
_serialVP.StopBits = StopBits.One;
_serialVP.DataBits = 8;
_serialVP.Handshake = Handshake.None;

_serialVP.DataReceived += new SerialDataReceivedEventHandler(DataReceviedHandler);

private void DataReceviedHandler(
object sender,
SerialDataReceivedEventArgs e)
{
SerialPort sp = (SerialPort)sender;
int inData = sp.ReadByte();

}

This example is for serial. i wish same implementation for USB.
[no name] 4-Apr-12 1:42am    
You are using a USB bulk transfer endpoint. 100 + Mbps. I have explained how to use it. You are not dealing in single characters. It is not an rs232 serial port. Perhaps you should be using another endpoint.
pravin29 4-Apr-12 1:59am    
We are using Endpoint OUT - 81 and IN - 2 (Bulk USB transfer)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900