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. One doubt

One doubt

Scheduled Pinned Locked Moved C / C++ / MFC
cssalgorithms
9 Posts 6 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.
  • D Offline
    D Offline
    Deepu Antony
    wrote on last edited by
    #1

    Hi everybody In my application i want to receive some data and after reeving the data i have to perform some operation. For the data to come it will take some 0 to 3 sec. So i am using Sleep(3000) to wait the data to come.But some times the data will come in less than 1 second.So unnecessary i have to wait. Before i was using this algorithm While(dataHasNotCome) { //wait } But it failed as the data may not come sometimes and be in infinite loop. I can use a counter and increment it till a particular value and stop the loop. Other than that is there any other method. Regards Deepu

    _ T K S D 5 Replies Last reply
    0
    • D Deepu Antony

      Hi everybody In my application i want to receive some data and after reeving the data i have to perform some operation. For the data to come it will take some 0 to 3 sec. So i am using Sleep(3000) to wait the data to come.But some times the data will come in less than 1 second.So unnecessary i have to wait. Before i was using this algorithm While(dataHasNotCome) { //wait } But it failed as the data may not come sometimes and be in infinite loop. I can use a counter and increment it till a particular value and stop the loop. Other than that is there any other method. Regards Deepu

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

      You can use asynchronous read here. Since you have not mentioned how you are reading data, I will assume you can use the file manipulation functions on some sort of handle or you are using sockets. If you are accessing a handle can use ReadFile or ReadFileEx with an OVERLAPPED structure. For sockets you can use WSARecv function with an OVERLAPPED structure. After calling the function you can wait on the event handle inside the OVERLAPPED structure.

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

      D 1 Reply Last reply
      0
      • _ _Superman_

        You can use asynchronous read here. Since you have not mentioned how you are reading data, I will assume you can use the file manipulation functions on some sort of handle or you are using sockets. If you are accessing a handle can use ReadFile or ReadFileEx with an OVERLAPPED structure. For sockets you can use WSARecv function with an OVERLAPPED structure. After calling the function you can wait on the event handle inside the OVERLAPPED structure.

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

        D Offline
        D Offline
        Deepu Antony
        wrote on last edited by
        #3

        I am reading data from the COM port.It is happening in a thread and it keep on reading the data and this data is stored in one CMainFrame variable. My part is to get this variable in my class and do some manipulations after the data has come. The only thing i have to do is to check the data has come or not.

        _ 1 Reply Last reply
        0
        • D Deepu Antony

          I am reading data from the COM port.It is happening in a thread and it keep on reading the data and this data is stored in one CMainFrame variable. My part is to get this variable in my class and do some manipulations after the data has come. The only thing i have to do is to check the data has come or not.

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

          Open the port with the overlapped flag.

          CreateFile(_T("COM1"), GENERIC_READ, FILE_SHARE_READ, 0, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, 0);

          Post the asynchronous read.

          OVERLAPPED over = { 0 };
          over.hEvent = ::CreateEvent(0, 0, 0, 0);
          DWORD dwRead = 0;
          ReadFile(m_hPort, buffer, len, &dwRead, &over);

          Wait on the event for 3 seconds.

          if (WAIT_OBJECT_0 != WaitForSingleObject(over.hEvent, 3000))
          return;
          GetOverlappedResult(m_hPort, &over, &dwReade, 0);

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

          D 1 Reply Last reply
          0
          • _ _Superman_

            Open the port with the overlapped flag.

            CreateFile(_T("COM1"), GENERIC_READ, FILE_SHARE_READ, 0, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, 0);

            Post the asynchronous read.

            OVERLAPPED over = { 0 };
            over.hEvent = ::CreateEvent(0, 0, 0, 0);
            DWORD dwRead = 0;
            ReadFile(m_hPort, buffer, len, &dwRead, &over);

            Wait on the event for 3 seconds.

            if (WAIT_OBJECT_0 != WaitForSingleObject(over.hEvent, 3000))
            return;
            GetOverlappedResult(m_hPort, &over, &dwReade, 0);

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

            D Offline
            D Offline
            Deepu Antony
            wrote on last edited by
            #5

            Thanks.

            1 Reply Last reply
            0
            • D Deepu Antony

              Hi everybody In my application i want to receive some data and after reeving the data i have to perform some operation. For the data to come it will take some 0 to 3 sec. So i am using Sleep(3000) to wait the data to come.But some times the data will come in less than 1 second.So unnecessary i have to wait. Before i was using this algorithm While(dataHasNotCome) { //wait } But it failed as the data may not come sometimes and be in infinite loop. I can use a counter and increment it till a particular value and stop the loop. Other than that is there any other method. Regards Deepu

              T Offline
              T Offline
              Taran9
              wrote on last edited by
              #6

              Why don't you register a callback in the module where data is rcvd. and call that callback as soon as (dataHasCome)

              1 Reply Last reply
              0
              • D Deepu Antony

                Hi everybody In my application i want to receive some data and after reeving the data i have to perform some operation. For the data to come it will take some 0 to 3 sec. So i am using Sleep(3000) to wait the data to come.But some times the data will come in less than 1 second.So unnecessary i have to wait. Before i was using this algorithm While(dataHasNotCome) { //wait } But it failed as the data may not come sometimes and be in infinite loop. I can use a counter and increment it till a particular value and stop the loop. Other than that is there any other method. Regards Deepu

                K Offline
                K Offline
                KarstenK
                wrote on last edited by
                #7

                Reading on a COM-Port is fine be reading 1 (in words: "one") byte while waiting for completion (or time out) :-\

                Press F1 for help or google it. Greetings from Germany

                1 Reply Last reply
                0
                • D Deepu Antony

                  Hi everybody In my application i want to receive some data and after reeving the data i have to perform some operation. For the data to come it will take some 0 to 3 sec. So i am using Sleep(3000) to wait the data to come.But some times the data will come in less than 1 second.So unnecessary i have to wait. Before i was using this algorithm While(dataHasNotCome) { //wait } But it failed as the data may not come sometimes and be in infinite loop. I can use a counter and increment it till a particular value and stop the loop. Other than that is there any other method. Regards Deepu

                  S Offline
                  S Offline
                  Stuart Dootson
                  wrote on last edited by
                  #8

                  Where does the data come from? For most input devices, there is some way of blocking until the input device has some data, rather than having to sleep. But we can't help you if you don't give us that detail!

                  Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

                  1 Reply Last reply
                  0
                  • D Deepu Antony

                    Hi everybody In my application i want to receive some data and after reeving the data i have to perform some operation. For the data to come it will take some 0 to 3 sec. So i am using Sleep(3000) to wait the data to come.But some times the data will come in less than 1 second.So unnecessary i have to wait. Before i was using this algorithm While(dataHasNotCome) { //wait } But it failed as the data may not come sometimes and be in infinite loop. I can use a counter and increment it till a particular value and stop the loop. Other than that is there any other method. Regards Deepu

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

                    Assuming your data is coming via the serial port, read here.

                    "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

                    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