Click here to Skip to main content
15,896,118 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: Question about a edit box! Help! Pin
DuFF21-Apr-03 7:08
DuFF21-Apr-03 7:08 
GeneralI need help with MATLAB questions Pin
nxz420-Apr-03 11:17
nxz420-Apr-03 11:17 
GeneralRe: I need help with MATLAB questions Pin
Nick Parker20-Apr-03 16:51
protectorNick Parker20-Apr-03 16:51 
GeneralRe: I need help with MATLAB questions Pin
Anonymous20-Apr-03 21:52
Anonymous20-Apr-03 21:52 
GeneralRe: I need help with MATLAB questions Pin
Toni7821-Apr-03 11:58
Toni7821-Apr-03 11:58 
GeneralUsing the serial com port Pin
Q15002220-Apr-03 10:05
Q15002220-Apr-03 10:05 
GeneralRe: Using the serial com port Pin
anju20-Apr-03 17:38
anju20-Apr-03 17:38 
GeneralRe: Using the serial com port Pin
Toni7821-Apr-03 11:53
Toni7821-Apr-03 11:53 
I had the exact same problem 3 years ago. You can't just read by calling ReadFile. I cann't really remember how I solved it but I will give you a part of an article that I downloaded back then and that luckily for you I still have it. The article is called "Serial Communications in Win32".

Reading
The ReadFile function issues a read operation. ReadFileEx also issues a read operation, but since it is not available on Windows 95, it is not discussed in this article. Here is a code snippet that details how to issue a read request. Notice that the function calls a function to process the data if the ReadFile returns TRUE. This is the same function called if the operation becomes overlapped. Note the fWaitingOnRead flag that is defined by the code; it indicates whether or not a read operation is overlapped. It is used to prevent the creation of a new read operation if one is outstanding.

DWORD dwRead;
BOOL fWaitingOnRead = FALSE;
OVERLAPPED osReader = {0};

// Create the overlapped event. Must be closed before exiting
// to avoid a handle leak.
osReader.hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);

if (osReader.hEvent == NULL)
// Error creating overlapped event; abort.

if (!fWaitingOnRead) {
// Issue read operation.
if (!ReadFile(hComm, lpBuf, READ_BUF_SIZE, &dwRead, &osReader)) {
if (GetLastError() != ERROR_IO_PENDING) // read not delayed?
// Error in communications; report it.
else
fWaitingOnRead = TRUE;
}
else {
// read completed immediately
HandleASuccessfulRead(lpBuf, dwRead);
}
}

The second part of the overlapped operation is the detection of its completion. The event handle in the OVERLAPPED structure is passed to the WaitForSingleObject function, which will wait until the object is signaled. Once the event is signaled, the operation is complete. This does not mean that it was completed successfully, just that it was completed. The GetOverlappedResult function reports the result of the operation. If an error occurred, GetOverlappedResult returns FALSE and GetLastError returns the error code. If the operation was completed successfully, GetOverlappedResult will return TRUE.

Note GetOverlappedResult can detect completion of the operation, as well as return the operation’s failure status. GetOverlappedResult returns FALSE and GetLastError returns ERROR_IO_INCOMPLETE when the operation is not completed. In addition, GetOverlappedResult can be made to block until the operation completes. This effectively turns the overlapped operation into a nonoverlapped operation and is accomplished by passing TRUE as the bWait parameter.

Here is a code snippet that shows one way to detect the completion of an overlapped read operation. Note that the code calls the same function to process the data that was called when the operation completed immediately. Also note the use of the fWaitingOnRead flag. Here it controls entry into the detection code, since it should be called only when an operation is outstanding.

#define READ_TIMEOUT 500 // milliseconds

DWORD dwRes;

if (fWaitingOnRead) {
dwRes = WaitForSingleObject(osReader.hEvent, READ_TIMEOUT);
switch(dwRes)
{
// Read completed.
case WAIT_OBJECT_0:
if (!GetOverlappedResult(hComm, &osReader, &dwRead, FALSE))
// Error in communications; report it.
else
// Read completed successfully.
HandleASuccessfulRead(lpBuf, dwRead);

// Reset flag so that another opertion can be issued.
fWaitingOnRead = FALSE;
break;

case WAIT_TIMEOUT:
// Operation isn't complete yet. fWaitingOnRead flag isn't
// changed since I'll loop back around, and I don't want
// to issue another read until the first one finishes.
//
// This is a good time to do some background work.
break;

default:
// Error in the WaitForSingleObject; abort.
// This indicates a problem with the OVERLAPPED structure's
// event handle.
break;
}
}



// Afterall I realized that even my comment lines have bugs
GeneralWorker-Thread In Win32 Application Pin
ZarrinPour20-Apr-03 5:27
ZarrinPour20-Apr-03 5:27 
GeneralRe: Worker-Thread In Win32 Application Pin
Ravi Bhavnani20-Apr-03 6:34
professionalRavi Bhavnani20-Apr-03 6:34 
GeneralRe: Worker-Thread In Win32 Application Pin
Johann Gerell20-Apr-03 6:48
Johann Gerell20-Apr-03 6:48 
GeneralRe: Worker-Thread In Win32 Application Pin
Ravi Bhavnani20-Apr-03 12:09
professionalRavi Bhavnani20-Apr-03 12:09 
GeneralAdding controls to a ListView based app Pin
TelMonks20-Apr-03 4:01
TelMonks20-Apr-03 4:01 
GeneralRe: Adding controls to a ListView based app Pin
Ravi Bhavnani20-Apr-03 6:35
professionalRavi Bhavnani20-Apr-03 6:35 
GeneralRe: Adding controls to a ListView based app Pin
TelMonks20-Apr-03 7:59
TelMonks20-Apr-03 7:59 
GeneralMeeting troubles when reading two integers from a file. Pin
George220-Apr-03 2:41
George220-Apr-03 2:41 
GeneralRe: Meeting troubles when reading two integers from a file. Pin
Nick Parker20-Apr-03 3:15
protectorNick Parker20-Apr-03 3:15 
GeneralRe: Meeting troubles when reading two integers from a file. Pin
George220-Apr-03 3:23
George220-Apr-03 3:23 
QuestionHow to let vector store reference? Pin
George220-Apr-03 2:31
George220-Apr-03 2:31 
AnswerRe: How to let vector store reference? Pin
Nick Parker20-Apr-03 3:22
protectorNick Parker20-Apr-03 3:22 
GeneralRe: How to let vector store reference? Pin
George220-Apr-03 3:27
George220-Apr-03 3:27 
GeneralRe: How to let vector store reference? Pin
Nitron21-Apr-03 3:09
Nitron21-Apr-03 3:09 
GeneralRe: How to let vector store reference? Pin
George221-Apr-03 19:52
George221-Apr-03 19:52 
GeneralRe: How to let vector store reference? Pin
Nitron22-Apr-03 2:51
Nitron22-Apr-03 2:51 
GeneralMacro Question / getting detailed Class / Functioncall Information Pin
Sendel20-Apr-03 1:39
Sendel20-Apr-03 1:39 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.