Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problems using Win32 Comm API

Status
Not open for further replies.

amarti44

Programmer
Feb 16, 2001
23
US
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
 
Alberto,

> I don't know the mount of data to read every time

Am I missing something? If you are to receive variable length data you need to have some sort of protocol to determine when you have received the complete set of data, don't you?

> I'm writing a program to communicate with a reader

Whatever 'a reader' is should determine the protocol and make the specification available to you for development.

Good luck
-pete
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top