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
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Non Ovelapped Serial I/O

Non Ovelapped Serial I/O

Scheduled Pinned Locked Moved C / C++ / MFC
helptutorialquestion
12 Posts 5 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.
  • S Offline
    S Offline
    sunny_vc
    wrote on last edited by
    #1

    HI all, I am working on Non Overlapped serial I/O application. I have only one thread to receive the data.For send i dont have any thread. I just call Write file when i need to send . My problem is, When I have my receive thread wait for an event using WaitCommEvent, it ties up the serial port. Then my sending function can't access the port with WriteFile. I knew waiting for a comm event would block the receiving thread but didn't expect that it would block use of the port. Any suggestions on how to do non-overlapped communication, where the send fucntion can write while the receive thread waits for a comm event?

    Regards, Sunil Kumar

    _ D 2 Replies Last reply
    0
    • S sunny_vc

      HI all, I am working on Non Overlapped serial I/O application. I have only one thread to receive the data.For send i dont have any thread. I just call Write file when i need to send . My problem is, When I have my receive thread wait for an event using WaitCommEvent, it ties up the serial port. Then my sending function can't access the port with WriteFile. I knew waiting for a comm event would block the receiving thread but didn't expect that it would block use of the port. Any suggestions on how to do non-overlapped communication, where the send fucntion can write while the receive thread waits for a comm event?

      Regards, Sunil Kumar

      _ Offline
      _ Offline
      _Superman_
      wrote on last edited by
      #2

      Try to open the port twice using CreateFile. Use one handle for writing and another for reading from the port.

      «_Superman_» I love work. It gives me something to do between weekends.

      S 1 Reply Last reply
      0
      • _ _Superman_

        Try to open the port twice using CreateFile. Use one handle for writing and another for reading from the port.

        «_Superman_» I love work. It gives me something to do between weekends.

        S Offline
        S Offline
        sunny_vc
        wrote on last edited by
        #3

        I dont think I can use CreateFile twice. It is unable to create the second time.The handle is getting some undefined value, when it is opened the second time.

        Regards, Sunil Kumar

        _ 1 Reply Last reply
        0
        • S sunny_vc

          I dont think I can use CreateFile twice. It is unable to create the second time.The handle is getting some undefined value, when it is opened the second time.

          Regards, Sunil Kumar

          _ Offline
          _ Offline
          _Superman_
          wrote on last edited by
          #4

          You should be able to open more than once if you specify the FILE_SHARE_READ and FILE_SHARE_WRITE flags.

          «_Superman_» I love work. It gives me something to do between weekends.

          CPalliniC L 2 Replies Last reply
          0
          • _ _Superman_

            You should be able to open more than once if you specify the FILE_SHARE_READ and FILE_SHARE_WRITE flags.

            «_Superman_» I love work. It gives me something to do between weekends.

            CPalliniC Offline
            CPalliniC Offline
            CPallini
            wrote on last edited by
            #5

            «_Superman_» wrote:

            You should be able to open more than once if you specify the FILE_SHARE_READ and FILE_SHARE_WRITE flags.

            You cannot do that with a communication resource (MSDN [^])

            The CreateFile function can create a handle to a communications resource, such as the serial port COM1. For communications resources, the dwCreationDisposition parameter must be OPEN_EXISTING, the dwShareMode parameter must be zero (exclusive access), and the hTemplateFile parameter must be NULL. Read, write, or read/write access can be specified, and the handle can be opened for overlapped I/O.

            :)

            If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
            This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
            [My articles]

            In testa che avete, signor di Ceprano?

            S _ 2 Replies Last reply
            0
            • CPalliniC CPallini

              «_Superman_» wrote:

              You should be able to open more than once if you specify the FILE_SHARE_READ and FILE_SHARE_WRITE flags.

              You cannot do that with a communication resource (MSDN [^])

              The CreateFile function can create a handle to a communications resource, such as the serial port COM1. For communications resources, the dwCreationDisposition parameter must be OPEN_EXISTING, the dwShareMode parameter must be zero (exclusive access), and the hTemplateFile parameter must be NULL. Read, write, or read/write access can be specified, and the handle can be opened for overlapped I/O.

              :)

              If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
              This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
              [My articles]

              S Offline
              S Offline
              sunny_vc
              wrote on last edited by
              #6

              yeah, I am unable to open it twice even if I mention the shared attributes. There is no other option for non overlapped I/O, if I have to WriteFile, when my receive thread wait for an event using WaitCommEvent?

              Regards, Sunil Kumar

              CPalliniC 1 Reply Last reply
              0
              • S sunny_vc

                yeah, I am unable to open it twice even if I mention the shared attributes. There is no other option for non overlapped I/O, if I have to WriteFile, when my receive thread wait for an event using WaitCommEvent?

                Regards, Sunil Kumar

                CPalliniC Offline
                CPalliniC Offline
                CPallini
                wrote on last edited by
                #7

                As workaround, you may use the same thread for both I/O. :)

                If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                [My articles]

                In testa che avete, signor di Ceprano?

                S 1 Reply Last reply
                0
                • CPalliniC CPallini

                  As workaround, you may use the same thread for both I/O. :)

                  If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                  This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                  [My articles]

                  S Offline
                  S Offline
                  sunny_vc
                  wrote on last edited by
                  #8

                  Even If I have to send data in the same receive thread, I am unable to do as WaitCommEvent never returns. My code block is:

                  if (!SetCommMask( pc->hCommn, EV\_RXCHAR))
                  {
                  	//Error
                  }
                  for ( ; ; ) 
                  {
                  if (WaitCommEvent(pc->hCommn, &dwCommEvent, NULL)) 
                  {
                    
                  	do
                  	{
                  	if (ReadFile(pc->hCommn, &chRead, 1, &dwRead, NULL))
                  	  {
                  		 // A byte has been read; process it.
                  		  pc->CopyRx(chRead);
                  	  }
                  	  else
                  	  {
                  		 // An error occurred in the ReadFile call.
                  
                  		 break;
                  	  }
                  	}while(dwRead);
                  }
                  

                  else
                  // Error in WaitCommEvent.
                  break;
                  }

                  Where can I call my send fucntion in this thread, if I have some data to send. Please suggest.

                  Regards, Sunil Kumar

                  1 Reply Last reply
                  0
                  • CPalliniC CPallini

                    «_Superman_» wrote:

                    You should be able to open more than once if you specify the FILE_SHARE_READ and FILE_SHARE_WRITE flags.

                    You cannot do that with a communication resource (MSDN [^])

                    The CreateFile function can create a handle to a communications resource, such as the serial port COM1. For communications resources, the dwCreationDisposition parameter must be OPEN_EXISTING, the dwShareMode parameter must be zero (exclusive access), and the hTemplateFile parameter must be NULL. Read, write, or read/write access can be specified, and the handle can be opened for overlapped I/O.

                    :)

                    If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                    This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
                    [My articles]

                    _ Offline
                    _ Offline
                    _Superman_
                    wrote on last edited by
                    #9

                    I was wondering if DuplicateHandle[^] will work.

                    «_Superman_» I love work. It gives me something to do between weekends.

                    1 Reply Last reply
                    0
                    • S sunny_vc

                      HI all, I am working on Non Overlapped serial I/O application. I have only one thread to receive the data.For send i dont have any thread. I just call Write file when i need to send . My problem is, When I have my receive thread wait for an event using WaitCommEvent, it ties up the serial port. Then my sending function can't access the port with WriteFile. I knew waiting for a comm event would block the receiving thread but didn't expect that it would block use of the port. Any suggestions on how to do non-overlapped communication, where the send fucntion can write while the receive thread waits for a comm event?

                      Regards, Sunil Kumar

                      D Offline
                      D Offline
                      David Crow
                      wrote on last edited by
                      #10

                      Have you read this?

                      "Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown

                      "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

                      S 1 Reply Last reply
                      0
                      • D David Crow

                        Have you read this?

                        "Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown

                        "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

                        S Offline
                        S Offline
                        sunny_vc
                        wrote on last edited by
                        #11

                        Hi, I already read the article.But I am trying using Non overlapped I/O. If it is impossible to do using Non overlapped, i will shift to overlapped.

                        Regards, Sunil Kumar

                        1 Reply Last reply
                        0
                        • _ _Superman_

                          You should be able to open more than once if you specify the FILE_SHARE_READ and FILE_SHARE_WRITE flags.

                          «_Superman_» I love work. It gives me something to do between weekends.

                          L Offline
                          L Offline
                          Lost User
                          wrote on last edited by
                          #12

                          You cannot do that with a communication resource (MSDN [^])

                          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