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. WaitCommEvent and GetCommMask (and overlapping)

WaitCommEvent and GetCommMask (and overlapping)

Scheduled Pinned Locked Moved C / C++ / MFC
helptutorialcomquestion
5 Posts 2 Posters 0 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

    Thanks for the help on my earlier problem.. I have now undertaken rewriting the serial portion of my code to use CreateFile and the like. It's all a bit complicated though, and being new to this, I have a few questions. It starts with overlapping, and also involves WaitCommEvent and GetCommMask. I did a search on the topics and read a few articles that were found, but while they cleared a few things up, they still left me with a few questions unanswered. First, I understand that SetCommMask allows you to screen for only a few of the events. But after calling SetCommMask to look for only what you want, how does one screen for these events and process them appropriately? I know you can use WaitCommEvent, but in that case, why would you even need GetCommMask? I also need to use overlapping for this program, but I'm not sure how to implement it or even what it actually is (do I need to use the overlapped structure at all?). I was able to find this piece of example code on MSDN: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/devio/base/monitoring\_communications\_events.asp I do not fully understand how it operates, though. Also, this article was very helpful, to a point: http://www.codeproject.com/system/serial\_com.asp Where I got lost was the huge block of code where he uses the OVERLAPPED structure and the Threadfn function, after which he writes "If you understood the above code , you will understand the whole of this article and the source code provided." Gulp. As always, the help is very much appreciated.

    A 2 Replies Last reply
    0
    • A AnotherProgrammer

      Thanks for the help on my earlier problem.. I have now undertaken rewriting the serial portion of my code to use CreateFile and the like. It's all a bit complicated though, and being new to this, I have a few questions. It starts with overlapping, and also involves WaitCommEvent and GetCommMask. I did a search on the topics and read a few articles that were found, but while they cleared a few things up, they still left me with a few questions unanswered. First, I understand that SetCommMask allows you to screen for only a few of the events. But after calling SetCommMask to look for only what you want, how does one screen for these events and process them appropriately? I know you can use WaitCommEvent, but in that case, why would you even need GetCommMask? I also need to use overlapping for this program, but I'm not sure how to implement it or even what it actually is (do I need to use the overlapped structure at all?). I was able to find this piece of example code on MSDN: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/devio/base/monitoring\_communications\_events.asp I do not fully understand how it operates, though. Also, this article was very helpful, to a point: http://www.codeproject.com/system/serial\_com.asp Where I got lost was the huge block of code where he uses the OVERLAPPED structure and the Threadfn function, after which he writes "If you understood the above code , you will understand the whole of this article and the source code provided." Gulp. As always, the help is very much appreciated.

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

      From this link: Configuring a communications resource: #include int main(int argc, char *argv[]) { DCB dcb; HANDLE hCom; BOOL fSuccess; char *pcCommPort = "COM2"; hCom = CreateFile( pcCommPort, GENERIC_READ | GENERIC_WRITE, 0, // must be opened with exclusive-access NULL, // no security attributes OPEN_EXISTING, // must use OPEN_EXISTING 0, // not overlapped I/O NULL // hTemplate must be NULL for comm devices ); pcCommPort is declared as a pointer to a character, which is assigned "COM2". How does this make sense? A char cannot be more than one character in length, right? Upon testing, trying to do *pcCommPort = "COM3" later in the program does indeed yield "error C2440: '=' : cannot convert from 'char [5]' to 'char'" which actually makes sense. So why is that same assignment statement allowed upon declaration? Also, if at some point, I need to change the COM port to say, COM3, how would I accomplish that? *pcCommPort = "COM3" did not work, as I mentioned. Thanks in advance.

      P 1 Reply Last reply
      0
      • A AnotherProgrammer

        From this link: Configuring a communications resource: #include int main(int argc, char *argv[]) { DCB dcb; HANDLE hCom; BOOL fSuccess; char *pcCommPort = "COM2"; hCom = CreateFile( pcCommPort, GENERIC_READ | GENERIC_WRITE, 0, // must be opened with exclusive-access NULL, // no security attributes OPEN_EXISTING, // must use OPEN_EXISTING 0, // not overlapped I/O NULL // hTemplate must be NULL for comm devices ); pcCommPort is declared as a pointer to a character, which is assigned "COM2". How does this make sense? A char cannot be more than one character in length, right? Upon testing, trying to do *pcCommPort = "COM3" later in the program does indeed yield "error C2440: '=' : cannot convert from 'char [5]' to 'char'" which actually makes sense. So why is that same assignment statement allowed upon declaration? Also, if at some point, I need to change the COM port to say, COM3, how would I accomplish that? *pcCommPort = "COM3" did not work, as I mentioned. Thanks in advance.

        P Offline
        P Offline
        Paul Ranson
        wrote on last edited by
        #3

        "COM2" is a constant null terminated string allocated somewhere by the compiler. It's reasonable to set a char * to point to its first character. But really it should be a const char * since the value of the string should not be changed. Try writing pcCommPort = "COM3", now you're setting the value of pcCommPort to the address of "COM3". Previously you were trying to set what pcCommPort pointed at (in this case presumably the constant string "COM2") to your new string. Which isn't what you intended. Paul

        A 1 Reply Last reply
        0
        • P Paul Ranson

          "COM2" is a constant null terminated string allocated somewhere by the compiler. It's reasonable to set a char * to point to its first character. But really it should be a const char * since the value of the string should not be changed. Try writing pcCommPort = "COM3", now you're setting the value of pcCommPort to the address of "COM3". Previously you were trying to set what pcCommPort pointed at (in this case presumably the constant string "COM2") to your new string. Which isn't what you intended. Paul

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

          Thanks Paul -- that was it! I don't know why I didn't catch it myself. Still looking for some clarification and answers about my original post... your help is greatly appreciated.

          1 Reply Last reply
          0
          • A AnotherProgrammer

            Thanks for the help on my earlier problem.. I have now undertaken rewriting the serial portion of my code to use CreateFile and the like. It's all a bit complicated though, and being new to this, I have a few questions. It starts with overlapping, and also involves WaitCommEvent and GetCommMask. I did a search on the topics and read a few articles that were found, but while they cleared a few things up, they still left me with a few questions unanswered. First, I understand that SetCommMask allows you to screen for only a few of the events. But after calling SetCommMask to look for only what you want, how does one screen for these events and process them appropriately? I know you can use WaitCommEvent, but in that case, why would you even need GetCommMask? I also need to use overlapping for this program, but I'm not sure how to implement it or even what it actually is (do I need to use the overlapped structure at all?). I was able to find this piece of example code on MSDN: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/devio/base/monitoring\_communications\_events.asp I do not fully understand how it operates, though. Also, this article was very helpful, to a point: http://www.codeproject.com/system/serial\_com.asp Where I got lost was the huge block of code where he uses the OVERLAPPED structure and the Threadfn function, after which he writes "If you understood the above code , you will understand the whole of this article and the source code provided." Gulp. As always, the help is very much appreciated.

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

            I'm now down to the point where I have the code written for everything up to where I understand -- meaning, the WaitCommEvent part is the only part that needs completing. Any help with this?

            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