PORTS Forum
Ports and Interfaces => USB => Topic started by: KhairulAkmal27 on May 05, 2017, 04:45:16 am
-
I have to convert an application from C++(a working one) to C#. However, in the newly C# codes, I fail to read the data using WinUsb_ReadPipe, despite successfully in other operation such WinUsb_GetDescriptor, WinUsb_QueryDeviceInformation, WinUsb_Initialize, WinUsb_WritePipe.
In the WinUsb_SetPipePolicy (Read) I have set,
1) Raw_IO = true
2) AUTO_CLEAR_STALL = True
3) TRANSFER_TIMEOUT = 1000 //1s
While for the WinUsb_SetPipePolicy (Write) I only set the TRANSFER_TIMEOUT = 1000.
At first, I execute the WinUsb_WritePipe, it returns success. When I execute the WinUsb_ReadPipe, the transaction FAIL and the Last error is 121 (ERROR_SEM_TIMEOUT).
This is the codes I used to read the pipe.
if (!(InTEGRAusbHandle.IsInvalid))
{
do
{
bytesReaded = nBytesToRead;
try
{
isSuccess = NativeMethods.WinUsb_ReadPipe(
InTEGRAusbHandle,
InTEGRA_BULK_IN.PipeId, //PipeId = 131
bRxBuffer, //bRxBuffer[65536]
nBytesToRead, //64
ref bytesReaded,
IntPtr.Zero);
}
catch (Exception ex)
{
isSuccess = false;
return false;
throw;
}
if (!isSuccess)
{
System.Console.WriteLine(Marshal.GetLastWin32Error());
}
timeoutCounter++;
} while ((isSuccess == false) && (timeoutCounter <= 1));}
I have debugged the operation line by line and all operation return success, however, when it comes to WinUsb_ReadPipe, It fail with Last Error code is ERROR_SEM_TIMEOUT
Once The code is executed, I get
1) isSuccess = FALSE
2) bytesReaded = 0
-
Does the C++ code also use WinUSB?
Use whatever debugging tools you have to verify that the device is sending data.
To isolate the problems, try increasing the timeout period and sending less data (if the device is sending a large block).
Also see:
https://www.winvistatips.com/threads/winusb-error-121-semaphore-timeout.806993/
-
1) The C++ used WinUsb.
2) The device is sending the data. I have verified it using the debugging tools from the C++ codes.
3) I also have increase the timeout period and adjusted the firmware to send smaller data but the same error occurs.
4) Before I post this problems here, I have go through the forum that you (Jan Axelson) have post to me.
Is there any setting that I may have forgot to set up that resulting this problems?..
-
The timeout error seems to indicate that the device is failing to send data. If the firmware is the same for both versions, it seems unlikely that the problem is in firmware, yet...
If you haven't verified that the data is going out in the C# version, you might do that. If you have control over the device firmware, have the device send some initial data (not dependent on anything else happening). Maybe light an LED when the data goes out.
-
I have solved the problems... It was not at all about the WinUsb Setting. The problems is with the error checking
Thanks for the help that you have provided to me..
-
Excellent! Thanks for reporting what you found.