USB WriteFile timeout? Topic is solved

If you are using the main C++ distribution of wxWidgets, Feel free to ask any question related to wxWidgets development here. This means questions regarding to C++ and wxWidgets, not compile problems.
Post Reply
macho2000
Earned a small fee
Earned a small fee
Posts: 12
Joined: Sun Feb 01, 2009 9:36 am

USB WriteFile timeout?

Post by macho2000 » Sun Feb 22, 2009 5:15 pm

Hello,

I am working at a project witch is communication with an selfmade USB device. So far everything goes well, I can read and write from/to the device.

But sometimes there is a failure in the USB-device and it blocks. I still get a correct handle to the device, but when I use WriteFile my programm blocks, because it can't write a single byte.

My Code:

Code: Select all

	
//FileHandles zu dem USB-Device für Lese/Schreib Zugriff
HANDLE WriteHandleToMyDevice = INVALID_HANDLE_VALUE;
HANDLE ReadHandleToMyDevice = INVALID_HANDLE_VALUE;
WriteHandleToMyDevice = CreateFile(devicePath, GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, 0);
error = GetLastError();
if (error){
	// FEHLER => Kein Handle erzeugt 
	return 0;	//=> Abbruch mit FEHLER!!!
	}
ReadHandleToMyDevice = CreateFile(devicePath, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, 0);
error = GetLastError();
if (error){
	// FEHLER => Kein Handle erzeugt 
	CloseHandle(WriteHandleToMyDevice);
	return 0;	//=> Abbruch mit FEHLER!!!
	}
	
// Vorbereitung des QueryCommand zur Abfrage des USB-Device
queryCommand.QueryDevice.WindowsReserved = 0;
queryCommand.QueryDevice.Command = QUERY_DEVICE;

//Senden des QueryCommand zum Device
WriteFile(WriteHandleToMyDevice,queryCommand.RawData, 65, &bytesWritten, 0);
Is there a timeout function which I can use? What else can I do create an error in this kind of situation?

Thanks
Matthias


OS: WinXp Pro 2002 SP3
IDE: MS VS2008 C++ Express
wxWidgets 2.8.8
PC: AMD Athlon64 3400

computerquip
Experienced Solver
Experienced Solver
Posts: 72
Joined: Fri Feb 20, 2009 7:13 pm
Location: $(#wx)\src

Post by computerquip » Mon Feb 23, 2009 5:23 pm

Call WriteFile through an if statement because it returns a boolean that determines whether or not the function was successful. Example:

Code: Select all

if (!WriteFile(WriteHandleToMyDevice,queryCommand.RawData, 65, &bytesWritten, 0))
{
  return 0;
}
A timer is slow and takes up resources compared to a chained reaction call. Timers usually aren't the way to go though they may be neccessary at some point. In this case, you probably will never need a timer.

Also, the nNumberOfBytesToWrite parameter or called into the function as the integer 65, isn't supposed to be hardcoded and is relatively small.

nNumberOfBytesToWrite-
The number of bytes to be written to the file or device.

A value of zero specifies a null write operation. The behavior of a null write operation depends on the underlying file system or communications technology.

Pipe write operations across a network are limited to 65,535 bytes per write. For more information regarding pipes, see the Remarks section.

macho2000
Earned a small fee
Earned a small fee
Posts: 12
Joined: Sun Feb 01, 2009 9:36 am

Post by macho2000 » Tue Feb 24, 2009 8:25 pm

Hello,

thanks, but this is not what I am looking for.

In my case the USB-device blocks and WriteFile is in an endless loop and can never reach return 0;
Matthias


OS: WinXp Pro 2002 SP3
IDE: MS VS2008 C++ Express
wxWidgets 2.8.8
PC: AMD Athlon64 3400

computerquip
Experienced Solver
Experienced Solver
Posts: 72
Joined: Fri Feb 20, 2009 7:13 pm
Location: $(#wx)\src

Post by computerquip » Wed Feb 25, 2009 1:10 am

Actually, sorry about that but little did I know that Microsoft already had a timer set up for you!

http://msdn.microsoft.com/en-us/library/aa365747.aspx

I apologize for sending the same link twice. But hit Ctrl + F and search for the documentation on SetCommTimeouts and GetCommTimeouts. This is used in conjuction with COMMTIMEOUTS structure. Perhaps this will suit you better!

Post Reply