Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Correct Usage of WaitCommEvent?

Correct Usage of WaitCommEvent?

Scheduled Pinned Locked Moved C / C++ / MFC
helpcsscomsecurityjson
3 Posts 2 Posters 3 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    AnotherProgrammer
    wrote on last edited by
    #1

    Hello... I'm currently writing a program that has to read in data from the serial port and then process it. But I thought before I tackle that part, I should write a small sample one to test to see that I can actually receive data. I got this sample program off the MSDN website and made a couple of minor changes (original can be found here: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/devio/base/monitoring\_communications\_events.asp ) to reflect the fact that it's supposed to look for any characters received.

    // used square brackets to make libraries show up here - actual code contains
    // greater than and less than signs
    #include [windows.h]
    #include [assert.h]
    #include [conio.h]
    #include [iostream]
    void main()
    {
        HANDLE hCom;
        OVERLAPPED o;
        BOOL fSuccess;
        DWORD dwEvtMask;
    
        hCom = CreateFile( "COM1",
            GENERIC_READ,
            0,    // exclusive access
            NULL, // default security attributes
            OPEN_EXISTING,
            FILE_FLAG_OVERLAPPED,
            NULL
            );
    
        if (hCom == INVALID_HANDLE_VALUE)
        {
            // Handle the error.
            return;
        }
    
        // Set the event mask.
    
        fSuccess = SetCommMask(hCom, EV_CTS | EV_DSR);
    
        if (!fSuccess)
        {
            // Handle the error.
            return;
        }
    
        // Create an event object for use by WaitCommEvent.
    
        o.hEvent = CreateEvent(
            NULL,   // default security attributes
            FALSE,  // auto reset event
            FALSE,  // not signaled
            NULL    // no name
    		);
    
    
        // Intialize the rest of the OVERLAPPED structure to zero.
        o.Internal = 0;
        o.InternalHigh = 0;
        o.Offset = 0;
        o.OffsetHigh = 0;
    
        assert(o.hEvent);
    
        if (WaitCommEvent(hCom, &dwEvtMask, &o))
        {
            if (dwEvtMask & EV_RXCHAR)
            {
    				cout << "received!";
            }
    
            if (dwEvtMask & EV_CTS)
            {
                // To do.
            }
        }
        getch();
    }
    

    This program, when compiled, does not output "received!", even though I know there is data being sent to the serial port (COM1). Any suggestions as to why? Is there a better way to monitor for the reception of characters through the serial port? Any help is appreciated. Thanks in advance.

    A A 2 Replies Last reply
    0
    • A AnotherProgrammer

      Hello... I'm currently writing a program that has to read in data from the serial port and then process it. But I thought before I tackle that part, I should write a small sample one to test to see that I can actually receive data. I got this sample program off the MSDN website and made a couple of minor changes (original can be found here: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/devio/base/monitoring\_communications\_events.asp ) to reflect the fact that it's supposed to look for any characters received.

      // used square brackets to make libraries show up here - actual code contains
      // greater than and less than signs
      #include [windows.h]
      #include [assert.h]
      #include [conio.h]
      #include [iostream]
      void main()
      {
          HANDLE hCom;
          OVERLAPPED o;
          BOOL fSuccess;
          DWORD dwEvtMask;
      
          hCom = CreateFile( "COM1",
              GENERIC_READ,
              0,    // exclusive access
              NULL, // default security attributes
              OPEN_EXISTING,
              FILE_FLAG_OVERLAPPED,
              NULL
              );
      
          if (hCom == INVALID_HANDLE_VALUE)
          {
              // Handle the error.
              return;
          }
      
          // Set the event mask.
      
          fSuccess = SetCommMask(hCom, EV_CTS | EV_DSR);
      
          if (!fSuccess)
          {
              // Handle the error.
              return;
          }
      
          // Create an event object for use by WaitCommEvent.
      
          o.hEvent = CreateEvent(
              NULL,   // default security attributes
              FALSE,  // auto reset event
              FALSE,  // not signaled
              NULL    // no name
      		);
      
      
          // Intialize the rest of the OVERLAPPED structure to zero.
          o.Internal = 0;
          o.InternalHigh = 0;
          o.Offset = 0;
          o.OffsetHigh = 0;
      
          assert(o.hEvent);
      
          if (WaitCommEvent(hCom, &dwEvtMask, &o))
          {
              if (dwEvtMask & EV_RXCHAR)
              {
      				cout << "received!";
              }
      
              if (dwEvtMask & EV_CTS)
              {
                  // To do.
              }
          }
          getch();
      }
      

      This program, when compiled, does not output "received!", even though I know there is data being sent to the serial port (COM1). Any suggestions as to why? Is there a better way to monitor for the reception of characters through the serial port? Any help is appreciated. Thanks in advance.

      A Offline
      A Offline
      AnotherProgrammer
      wrote on last edited by
      #2

      The ReadFile function also has me a bit concerned. Assuming WaitCommEvent works, and the "event" is that a character has been received (EV_RXCHAR), would this piece of code output the character that was received (data)?

      OVERLAPPED obj;
      BOOL fsuccess;
      HANDLE hCom;
      BYTE data;
      DWORD event = EV_RXCHAR;
      DWORD transferred;
      
      // ...
      // Open the port, give the correct settings, 
      // ...
      
      if (WaitCommEvent(hCom, &event, &obj))
      {
            cout << "IT DETECTED SOMETHING! ";
            fsuccess = ReadFile (hCom, &data, 1, &transferred, &obj);
            if (fsuccess)
            {
                 cout << "IT READ A CHARACTER: " << data;
            }
            getch();
      }
      

      Oh, in both of the above programs, the serial port is constantly being fed data continuously. Thanks for any help.

      1 Reply Last reply
      0
      • A AnotherProgrammer

        Hello... I'm currently writing a program that has to read in data from the serial port and then process it. But I thought before I tackle that part, I should write a small sample one to test to see that I can actually receive data. I got this sample program off the MSDN website and made a couple of minor changes (original can be found here: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/devio/base/monitoring\_communications\_events.asp ) to reflect the fact that it's supposed to look for any characters received.

        // used square brackets to make libraries show up here - actual code contains
        // greater than and less than signs
        #include [windows.h]
        #include [assert.h]
        #include [conio.h]
        #include [iostream]
        void main()
        {
            HANDLE hCom;
            OVERLAPPED o;
            BOOL fSuccess;
            DWORD dwEvtMask;
        
            hCom = CreateFile( "COM1",
                GENERIC_READ,
                0,    // exclusive access
                NULL, // default security attributes
                OPEN_EXISTING,
                FILE_FLAG_OVERLAPPED,
                NULL
                );
        
            if (hCom == INVALID_HANDLE_VALUE)
            {
                // Handle the error.
                return;
            }
        
            // Set the event mask.
        
            fSuccess = SetCommMask(hCom, EV_CTS | EV_DSR);
        
            if (!fSuccess)
            {
                // Handle the error.
                return;
            }
        
            // Create an event object for use by WaitCommEvent.
        
            o.hEvent = CreateEvent(
                NULL,   // default security attributes
                FALSE,  // auto reset event
                FALSE,  // not signaled
                NULL    // no name
        		);
        
        
            // Intialize the rest of the OVERLAPPED structure to zero.
            o.Internal = 0;
            o.InternalHigh = 0;
            o.Offset = 0;
            o.OffsetHigh = 0;
        
            assert(o.hEvent);
        
            if (WaitCommEvent(hCom, &dwEvtMask, &o))
            {
                if (dwEvtMask & EV_RXCHAR)
                {
        				cout << "received!";
                }
        
                if (dwEvtMask & EV_CTS)
                {
                    // To do.
                }
            }
            getch();
        }
        

        This program, when compiled, does not output "received!", even though I know there is data being sent to the serial port (COM1). Any suggestions as to why? Is there a better way to monitor for the reception of characters through the serial port? Any help is appreciated. Thanks in advance.

        A Offline
        A Offline
        Antti Keskinen
        wrote on last edited by
        #3

        You (or the MSDN example) does not configure the timeout values for the communications port. These timeouts MUST be specified, otherwise the last settings which were effect on the port (by another application or by system defaults) are applied, and may cause serious misbehaviour. For information how to configure the timeouts and what timeouts are needed, see MSDN with the keyword 'SetCommTimeouts'. Also, if you need a non-overlapped I/O sample, see this CodeProject article and browse through it's source code. -Antti Keskinen ---------------------------------------------- The definition of impossible is strictly dependant on what we think is possible.

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups