WaitCommEvent problem
From: marabo82 (marabo82_at_discussions.microsoft.com)
Date: 08/04/04
- Next message: chen: "Re: Can CCeSocket be a chat server"
- Previous message: Paul G. Tobey [eMVP]: "Re: SetWindowText question."
- Next in thread: Olivier: "Re: WaitCommEvent problem"
- Reply: Olivier: "Re: WaitCommEvent problem"
- Messages sorted by: [ date ] [ thread ]
Date: Wed, 4 Aug 2004 16:57:01 -0700
Hi All,
(Environment: SH3 device, PB 4.2)
My app is a Headless serial port App, it sends a trigger command to a
barcode scanner (via writefile , works fine)
the scanner sends the barcode ascii characters (all fine and Dundee) then i
have to write it to a file (so other app's can access it)
all that works perfect, however, i need my app to timeout if no data was
received within 3 seconds so that i am not waiting forever for the scanner to
send some data. (when i send the trigger command to the scanner, it will turn
on and attempt to capture barcode 4 ever, it will only send some data when it
captures barcode) i want my app to terminate if no data was received from the
scanner within 3 seconds
I am doing the COM port init, read, write all in the same UI (no seperate
threads, works perfect, juts want it to timeout)
i am using WaitCommEvent (hPort, &dwCommModemStatus, 0). i read in one of
the posting by Steve Maillet that
WaitCommEvent will timeout according to the timeout values set by
SetCommTimeouts(). However, this doesn't seem to work
, i think those values will only apply to ReadFile/WriteFile()
My approach was to set a timeout for the event to occur, and quit if it
doesn't occur within 3 seconds. i did the following:
Time_out_event=CreateEvent(NULL,false,false,NULL);
And waited for that event to occur after i call WaitCommEvent().
<snap>
WaitCommEvent (hPort, &dwCommModemStatus, 0 ); //overlapped I/O is not
supported in windows CE
// Re-specify the set of events to be monitored for the port
SetCommMask (hPort, EV_RXCHAR | EV_CTS | EV_DSR | EV_RLSD | EV_RING);
//Wait for the event to occur for 3 seconds
Time_out_result=WaitForSingleObject(Time_out_event,3000);
</snap>
However, WaitCommEvent never timeout -- if no data is received within 3
seconds, it will wait 4 ever--.
oddly, when the scanner gets some data, the WaitCommevent() will return and
WaitForSingleObject() will think
that WAIT_TIMEOUT event has occured instead WAIT_OBJECT_0 (which i believe
indicated that an event has occured)
here is the code:
<code snap>
HANDLE Time_out_event=0;
DWORD Time_out_result=0;
...
CommTimeouts.ReadIntervalTimeout = MAXWORD; //i also tried 1000, 0
CommTimeouts.ReadTotalTimeoutMultiplier = 0;
CommTimeouts.ReadTotalTimeoutConstant = 0;
CommTimeouts.WriteTotalTimeoutMultiplier = 10;
CommTimeouts.WriteTotalTimeoutConstant = 1000;
...
SetCommMask (hPort, EV_RXCHAR | EV_CTS | EV_DSR | EV_RLSD | EV_RING);
// Wait for an event to occur for the port.
Time_out_event=CreateEvent(NULL,false,false,NULL);
if (Time_out_event== INVALID_HANDLE_VALUE)
{
// Could not create a Time out event
PostMessage(HWND_BROADCAST,ERROR_BARCODE_MSG,0, 0);
RETAILMSG(1,(TEXT("\r\nError: Unable to Create a Time Out Event \n")));
//close the barcodemsg file
CloseHandle(barcodemsg_file);
//close serial port
CloseHandle( hPort);
return -1;
}
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_RLSD | EV_RING);
//Wait for the event to occur for 3 seconds
Time_out_result=WaitForSingleObject(Time_out_event,3000);
switch(Time_out_result)
{
case WAIT_OBJECT_0 :
//note: the program never go here, even if the scanner send the data
before 3 seconds have elapsed
//in theory, the program should get here if data was received before 3
seconds.
// Operation completed
//// Re-specify the set of events to be monitored for the port
//SetCommMask (hPort, EV_RXCHAR | EV_CTS | EV_DSR | EV_RLSD | EV_RING);
RETAILMSG(1,(TEXT("\r\nSucccess: Operation Completed\n")));
if (dwCommModemStatus & EV_RXCHAR)
{
do
{
// Read the data from the serial port.
ReadFile (hPort, &byte, 1, &dwBytesTransferred, 0);
if (dwBytesTransferred == 1)
{
WriteFile (barcodemsg_file,&byte,1,&dwNumBytesWritten,NULL);
}
}while (dwBytesTransferred == 1);
}
// Retrieve modem control-register values.
GetCommModemStatus (hPort, &dwCommModemStatus);
ResetEvent ( Time_out_event );
break;
case WAIT_TIMEOUT:
//note: the code end up here when the barcode sends the data, otherwise
// WaitCommevent() will block 4 ever.
//in theory, the code should only get here if Time_out_event has occurred
//The time-out interval elapsed, and the objects state is nonsignaled
PostMessage(HWND_BROADCAST,ERROR_BARCODE_MSG,0, 0);
RETAILMSG(1,(TEXT("\r\nError: WaitCommEvent didn't succeed or timed
out\n")));
//close the barcodemsg file
CloseHandle(barcodemsg_file);
//close serial port
CloseHandle( hPort);
exit(-1);
break;
}
</snap>
Any idea? will WaitSingleObject() ever work in windows CE?
do i have to Set/Reset Time_out_event somewhere?
Your help is greatly appreciated.
Mo
marabo82@hotmail.com
- Next message: chen: "Re: Can CCeSocket be a chat server"
- Previous message: Paul G. Tobey [eMVP]: "Re: SetWindowText question."
- Next in thread: Olivier: "Re: WaitCommEvent problem"
- Reply: Olivier: "Re: WaitCommEvent problem"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|