PocketMatrix forums

A read-only archive of the PocketMatrix forums (2001–2014). Posting and accounts are closed.

serial com

how can i read more than 1 char in my buffer?
Code:
while (hPort != INVALID_HANDLE_VALUE)
{
// Wait for an event to occur for the port.
WaitCommEvent (hPort, &dwCommModemStatus;, 0);

// Re-specify the set of events to be monitored for the port.
SetCommMask (hPort, EV_RXCHAR | EV_CTS | EV_DSR | EV_RING);

if (dwCommModemStatus & EV_RXCHAR)
{
// Loop for waiting for the data.
do
{
// Read the data from the serial port.
ReadFile (hPort, &Byte;, 1, &dwBytesTransferred;, 0);

// Display the data read.
if (dwBytesTransferred == 1)
ProcessChar (Byte);

} while (dwBytesTransferred == 1);
}
You could try telling readfile to read more than one byte and then find out how much it actually gave you in the dwBytesTransferred variable.

How much it'll actually help you I don't know - it looks like the WaitCommEvent will tell you whenever a single byte comes into the buffer so you might find that dwBytesTransferred is always one.
hi,

i tried telling ReadFile to read more than 1 byte, but it wouldn´t help, because i think WaitComEvent waits ONLY for one char. is there another possibility to read more than one char at once.

thanks a lot
DW
It is possible that ReadFile when used on serial COMs will never read more than one byte. A lot of Win32 serial code that I have seen never issues reads of more than one byte.

However, it is also possible that you may read more than one byte but that ReadFile will not block to get the whole request. It is possible that your code modified to read more than one byte per ReadFile call would do that if your device was under heavy load. If it's not it will be able to call ReadFile much too often for any bytes to queue up in the internal buffers.
Pejo Software - Per · 03 Mar 2003, 21:35 UTC
I am not sure what it is that you want to do but you can as stated above use ReadFile and get it to block until you have read the number of bytes you want to read. There is no need to use WaitForCommEvent in that case. You should then use SetCommTimeouts to set the timeouts to the appropriate values.
If you however want to check how many bytes there is in the internal after WaitForCommEvent you could use the ClearCommError function. The cbInQue member of the COMMSTAT struct will give you that value.
thanks a lot !! i solved my problem with this tip.