WaitCommEvent problem

From: marabo82 (marabo82_at_discussions.microsoft.com)
Date: 08/04/04


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



Relevant Pages

  • Re: Barcode without a textbox
    ... all 'keystrokes' sent by the barcode scanner upto the escape code. ... I wrote a barcode application, using a keyboard wedge type scanner, and just ... of course need to bring in the code into the app, but I wanted to know if I ... could use the barcode with a textbox. ...
    (microsoft.public.vb.general.discussion)
  • Dealing with a barcode scanner
    ... I'm writing a small dialog app that, along with taking keyboard input ... also takes input from a USB barcode scanner. ... RETURN) in whatever app has the current focus. ...
    (microsoft.public.vc.mfc)
  • Re: hooking barcode readers to the Pocket PC and what uses people find for these great devices
    ... >One thing we would like to do is use this also with a barcode scanner... ... Most scanners come with a "keyboard wedge". ... effective to add barcode support to any app with _no_ need to modify ...
    (microsoft.public.pocketpc)
  • Re: Pocket PC Barcode scanning
    ... Actually most scanners will just pipe scanned data into the keyboard stream, ... > allows you to scan a barcode and the code goes into a text box. ... > I would like to do something similar using my own .net app and was ... > scanner in the Symbol PPC? ...
    (microsoft.public.pocketpc.developer)
  • Re: Help with bar code
    ... There is no setup required between Access and the barcode scanner itself. ... barcode label itself is an identifier that links the item ... so that I can determine what is an inventory label and what is the label from ...
    (microsoft.public.access.formscoding)