I'm writing a program to communicate with a reader using a serial port. I don't want to use multithreading or overlapped read or write. The function that handle the read operation has the following prototype:
BOOL ReadCommBlock(HWND hWnd, BYTE *inBuff)
I don't know the mount of data to read every time; therefore I do not include the BytestoRead as a function parameter. I need to find out that using another way. In addition, the data sometimes is not sent in steady streams. For instance, I need to receive a block of 11 bytes but the reader send the first 8 and then the other 3. My function is implemented in the following way:
BOOL ReadCommBlock(HWND hWnd, BYTE *inBuff)
{
// Var declaration
// read only number of bytes in queue
ClearCommError(hCom, &dwErrorFlags, &ComStat);
dwLength = min((DWORD)MAXBLOCK, ComStat.cbInQue);
if(dwLength > 0){
do{ // Process data
BytestoRead = 1; // read one byte at a time
ReadStatus=ReadFile(hCom, inBuff, BytestoRead,
&BytesRead, NULL);
//Process the data read with Read File
dwLength--;
}while(dwLength > 0);
}
return TRUE;
}
Using the member of the COMSTAT structure cbInQue that returns the bytes received by the serial provider but not yet read by a ReadFile operation, I thought that I could solve the problem but when I request
dwLength = min((DWORD)MAXBLOCK, ComStat.cbInQue);
in the previous example (8 of 11) always dwLength is equal to 8. Even if later in the loop I check again to see if more bytes have been received, I cannot updated dwLength to 11. I now something is wrong that the interrupt is not released but I don't know how to solve it.
Thanks in Advance,
Alberto
BOOL ReadCommBlock(HWND hWnd, BYTE *inBuff)
I don't know the mount of data to read every time; therefore I do not include the BytestoRead as a function parameter. I need to find out that using another way. In addition, the data sometimes is not sent in steady streams. For instance, I need to receive a block of 11 bytes but the reader send the first 8 and then the other 3. My function is implemented in the following way:
BOOL ReadCommBlock(HWND hWnd, BYTE *inBuff)
{
// Var declaration
// read only number of bytes in queue
ClearCommError(hCom, &dwErrorFlags, &ComStat);
dwLength = min((DWORD)MAXBLOCK, ComStat.cbInQue);
if(dwLength > 0){
do{ // Process data
BytestoRead = 1; // read one byte at a time
ReadStatus=ReadFile(hCom, inBuff, BytestoRead,
&BytesRead, NULL);
//Process the data read with Read File
dwLength--;
}while(dwLength > 0);
}
return TRUE;
}
Using the member of the COMSTAT structure cbInQue that returns the bytes received by the serial provider but not yet read by a ReadFile operation, I thought that I could solve the problem but when I request
dwLength = min((DWORD)MAXBLOCK, ComStat.cbInQue);
in the previous example (8 of 11) always dwLength is equal to 8. Even if later in the loop I check again to see if more bytes have been received, I cannot updated dwLength to 11. I now something is wrong that the interrupt is not released but I don't know how to solve it.
Thanks in Advance,
Alberto