Question

I'm using winapi (in C) to read a batch from a comport using the following creating the port:

hSerial= CreateFile(COM5, 
GENERIC_READ | GENERIC_WRITE,
0,
NULL,
OPEN_EXISTING, 
FILE_ATTRIBUTE_NORMAL |FILE_FLAG_NO_BUFFERING, 
NULL);   

and then reading a batch into Data which is lengthIn long :

ReadFile(hSerial,Data,lengthIn,lengthOut,NULL)

lengthOut represents the amount of data actually read.

I need to make sure I actually read lengthIn bytes at each iteration and not less, how do I do that? How do I make sure lengthIn=lengthOut each time?

I think it has something to do with the attribute while creating the port (like the FILE_FLAG_NO_BUFFERING), but I'm not sure which one and how.

Was it helpful?

Solution

If you set the timeouts on the comm port handle large enough, then ReadFile will effectively not return until the number of bytes you've specified has been read (or an error has occurred).

See the docs on the COMMTIMEOUTS structure (http://msdn.microsoft.com/en-us/library/windows/desktop/aa363190(v=vs.85).aspx) for more details.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top