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. LPT Listener using thread

LPT Listener using thread

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestion
11 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.
  • T TPN

    Hi All, I am writing an application which checks LPT signal for doing something. My code looks like: void LPTThread() { while (1) { if (ReadLPT(pinX) = signal level Y) {do something;} } //pin is one of pin of LPT port //signal level of each pin is 0 or 1 at a time } This loop cause the CPU usage always 100%, is there any way to prevent it? Can we catch this change by any event handler, it mean when a change occurs at LPT or, this event handler will be called? Thank you for your help.

    C Offline
    C Offline
    Cedric Moonen
    wrote on last edited by
    #2

    I never heard about a function called ReadLPT (and google wasn't of any help), from where is that coming from ? Can you give more details ? Anyway, for serial communication (so COM ports), you can use functions like WaitCommEvent. I suppose there should be something similar for you.


    Cédric Moonen Software developer
    Charting control [v1.2]

    T 1 Reply Last reply
    0
    • C Cedric Moonen

      I never heard about a function called ReadLPT (and google wasn't of any help), from where is that coming from ? Can you give more details ? Anyway, for serial communication (so COM ports), you can use functions like WaitCommEvent. I suppose there should be something similar for you.


      Cédric Moonen Software developer
      Charting control [v1.2]

      T Offline
      T Offline
      TPN
      wrote on last edited by
      #3

      Dear Moonen, Thank for your answer. But I need to clarify that ReadLPT is just only my pseudo code - the code that is responsible for reading LPT port signal/information. Thanks.

      C 1 Reply Last reply
      0
      • T TPN

        Hi All, I am writing an application which checks LPT signal for doing something. My code looks like: void LPTThread() { while (1) { if (ReadLPT(pinX) = signal level Y) {do something;} } //pin is one of pin of LPT port //signal level of each pin is 0 or 1 at a time } This loop cause the CPU usage always 100%, is there any way to prevent it? Can we catch this change by any event handler, it mean when a change occurs at LPT or, this event handler will be called? Thank you for your help.

        R Offline
        R Offline
        Rahul Vaishnav
        wrote on last edited by
        #4

        Hi, I don't know much about LPT signal. but i got simmiler problem in one of my project. you do one thing begin main thread once some where in Oninitdialog() or On InitInstance().In main thread function in while(1) loop call child thread & do your work in child thread & you must end that childthread in that childthread itself. have a look on given sample.... OnInitInstance() { begin MyMainthread()... } void MyMainthread() { while(1) { begin mychildthread() ::Sleep(400); } } void mychildthread() { //Do your work here.... ........ ..... _EndThread(); } I hope this will reduce your CPU Uses..... :| Rahul Vaishnav

        T 1 Reply Last reply
        0
        • T TPN

          Hi All, I am writing an application which checks LPT signal for doing something. My code looks like: void LPTThread() { while (1) { if (ReadLPT(pinX) = signal level Y) {do something;} } //pin is one of pin of LPT port //signal level of each pin is 0 or 1 at a time } This loop cause the CPU usage always 100%, is there any way to prevent it? Can we catch this change by any event handler, it mean when a change occurs at LPT or, this event handler will be called? Thank you for your help.

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

          TPN wrote:

          This loop cause the CPU usage always 100%...

          And rightly so since you are not yielding control of the CPU. Consider a message pump/loop.


          "A good athlete is the result of a good and worthy opponent." - David Crow

          "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

          1 Reply Last reply
          0
          • T TPN

            Dear Moonen, Thank for your answer. But I need to clarify that ReadLPT is just only my pseudo code - the code that is responsible for reading LPT port signal/information. Thanks.

            C Offline
            C Offline
            Cedric Moonen
            wrote on last edited by
            #6

            Ok but that doesn't help. Which functions are you using exactly to access the port ?


            Cédric Moonen Software developer
            Charting control [v1.2]

            T 1 Reply Last reply
            0
            • C Cedric Moonen

              Ok but that doesn't help. Which functions are you using exactly to access the port ?


              Cédric Moonen Software developer
              Charting control [v1.2]

              T Offline
              T Offline
              TPN
              wrote on last edited by
              #7

              Thanks for you reply. I use Inp32 function which is in the inpout32.dll library - a very common used.

              1 Reply Last reply
              0
              • R Rahul Vaishnav

                Hi, I don't know much about LPT signal. but i got simmiler problem in one of my project. you do one thing begin main thread once some where in Oninitdialog() or On InitInstance().In main thread function in while(1) loop call child thread & do your work in child thread & you must end that childthread in that childthread itself. have a look on given sample.... OnInitInstance() { begin MyMainthread()... } void MyMainthread() { while(1) { begin mychildthread() ::Sleep(400); } } void mychildthread() { //Do your work here.... ........ ..... _EndThread(); } I hope this will reduce your CPU Uses..... :| Rahul Vaishnav

                T Offline
                T Offline
                TPN
                wrote on last edited by
                #8

                Thanks for your reply. But the problem is the thread cannot sleep every 400ms before checking LPT port. The application must ensure that, where the LPT signal changes, it will immediately handle that.

                J 2 Replies Last reply
                0
                • T TPN

                  Thanks for your reply. But the problem is the thread cannot sleep every 400ms before checking LPT port. The application must ensure that, where the LPT signal changes, it will immediately handle that.

                  J Offline
                  J Offline
                  JudyL_MD
                  wrote on last edited by
                  #9

                  If the particular pin isn't one of the ones that is monitored by WaitCommEvent, there is no built-in notification mechanism. Short of writing a parallel port driver to implement notification, try putting your busy-wait loop into a seperate thread that runs at a lower priority than the rest of your program. Judy

                  1 Reply Last reply
                  0
                  • T TPN

                    Thanks for your reply. But the problem is the thread cannot sleep every 400ms before checking LPT port. The application must ensure that, where the LPT signal changes, it will immediately handle that.

                    J Offline
                    J Offline
                    JudyL_MD
                    wrote on last edited by
                    #10

                    Also, you use the work "immediately" - how immediate is immediate? Windows is not a real-time OS, and as such, does not guarantee response times, even within drivers.

                    1 Reply Last reply
                    0
                    • T TPN

                      Hi All, I am writing an application which checks LPT signal for doing something. My code looks like: void LPTThread() { while (1) { if (ReadLPT(pinX) = signal level Y) {do something;} } //pin is one of pin of LPT port //signal level of each pin is 0 or 1 at a time } This loop cause the CPU usage always 100%, is there any way to prevent it? Can we catch this change by any event handler, it mean when a change occurs at LPT or, this event handler will be called? Thank you for your help.

                      R Offline
                      R Offline
                      Rahul Vaishnav
                      wrote on last edited by
                      #11

                      I have used this code in my project for diduction of CPU uses.. it is working fine at my end. if you dont want to use Sleep() then remove it & add WaitForSingleObject() as shown in given sample. try it....i hope it will work... while(1) { HANDLE hThread; hThread = (HANDLE)_beginthread(CallchildFunction,0,NULL); WaitForSingleObject(hThread, INFINITE); ::Sleep(500); } :| Rahul Vaishnav

                      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