2 Replies Latest reply on Oct 18, 2012 8:37 AM by steve.flora

    Timer for RS232 input loop??

    steve.flora

      I've been struggleing with how to add a timer to a loop that will exit after a discrete amount of time (say 10 seconds).

         

      What I have going on is I'm collecting input from an RS232 interface to fill up a string (that includes a checksum at the end).

         

      The loop exits when the checksum is received. I also want to exit if I  don't receive it after a discrete amount of time (that 10 second value above).

         

      I tried to implement it using a 8 bit counter that was clocked by a 1 seconds clock with the result that I could get the counter value to 10, exit, but never able to reset the counter back to 0 for the next pass (kept exiting every 10 seconds

         

      How do folks that do this type of thing implement implement a time so your not stuck forever in the serial processing loop forever with garbage in the string buffer?

        • 1. Re: Timer for RS232 input loop??
          user_1377889

          For receiving data over a serial interface non-blocking the best solution is a combination of Rx driven interrupt and a circular buffer.

             

          If you prefer, another way (easier) is to define for the component a buffer large enough and let int be handled by the internal interrupt which will be enabled automatically. You just have to poll for the buffer containing the required amount of chars.

             

           

             

          If you still are interrested in exiting a loop: Set up a timer, start it when entering the routine, poll in a loop Rx and timer status and exit when any of them occurs after stopping the timer and if Rx had a char get it and return the value, else return an indication for no char.

             

           

             

          Happy coding

             

          Bob

          • 2. Re: Timer for RS232 input loop??
            steve.flora

            If you still are interrested in exiting a loop: Set up a timer, start it when entering the routine, poll in a loop Rx and timer status and exit when any of them occurs after stopping the timer and if Rx had a char get it and return the value, else return an indication for no char.

               

             

               

            Happy coding

               

            Bob

               

            Actually, that is what I did using a counter and a 1HZ clock. I poll the counter for being greater than 10 seconds, clear things and return to main. Unfortunately, I can not clear the counter and stop it, so every 10 seconds it continues to exit because the counter counts to 10. I've roamed the examples and do not have a clear understanding of why it behaves and played with all kinds of library methods to no avail.