Re: Re: add timeout to xbee.wait_read_frame

90 views
Skip to first unread message

Paul Malmsten

unread,
Aug 14, 2014, 9:13:05 PM8/14/14
to paul knijn, gdr...@gmail.com, bla...@bravo5.org, python-...@googlegroups.com
Hi Paul,

My apologies for the delay getting back to you.

I like where you are going with this. However, it needs a little more:
  • The main loop has a time out, but the serial port may or may not be configured to use a timeout. If the serial port never times out, then we will never return from read() and check the outer loop to see that we should move on. This code should either check that a timeout is set for the serial port and throw an exception if one is not, or perhaps just set a timeout on the serial port directly. I'm looking at the timeout property here (http://pyserial.sourceforge.net/pyserial_api.html).
  • As a matter of taste, I prefer that library code not print anything to the console directly. In this case, application code could check the result and print to the console there easily enough.
  • I would like to see unit tests like the others in the package and some documentation that users can work with
What do you think? I'm happy to answer questions and such, but my time to work on this project directly is rather limited lately.

Regards,
~Paul Malmsten


On Tue, Aug 5, 2014 at 4:03 PM, paul knijn <paul...@gmail.com> wrote:
Hello,

could you assist to get this timeout change for retries in Xbee.py?

Regards,
Paulus


-------- Origineel bericht --------
Onderwerp: Re: add timeout to xbee.wait_read_frame
Datum: Mon, 4 Aug 2014 13:33:51 -0700
Van: Amit Snyderman <amitsn...@gmail.com>
Aan: paul knijn <paul...@gmail.com>


Paulus, this page lists their full email addresses: https://pypi.python.org/pypi/XBee

Paul Malmsten <pmal...@gmail.com>

On Monday, August 4, 2014 at 1:05 PM, paul knijn wrote:

Hi Amit,

could you forward this email. The address I got does not work : pmalms...@gmail.com

Cheers,
Paulus

Amit Snyderman schreef op 4-8-2014 4:24:
Hey Paul, I’m actually not the maintainer of this project anymore. I’d recommend sending a patch (unfortunately they’re not on GitHub or this would be a perfect pull-request opportunity) to the current maintainers. You can find their info @ https://code.google.com/p/python-xbee/

On Sunday, August 3, 2014 at 12:24 PM, paul knijn wrote:

Hi,

im having problems that my programs are getting stuck on packet loss due to the infinite read loop in wait_read_frame().

Do you think you could add a timeout? I already implemented this, I just would like a versioned installer. If timeout expires I want to do a retry on the read...

Cheers,
Paulus

    def _wait_for_frame(self, timeout):
        """
        _wait_for_frame: None -> binary data
       
        _wait_for_frame will read from the serial port until a valid
        API frame arrives. It will then return the binary data
        contained within the frame.

        If this method is called as a separate thread
        and self.thread_continue is set to False, the thread will
        exit by raising a ThreadQuitException.
        """
        frame = APIFrame(escaped=self._escaped)
        t0 = time.time()
       
        while (time.time()-t0 < timeout):
       
                if self._callback and not self._thread_continue:
                    raise ThreadQuitException

                if self.serial.inWaiting() == 0:
                    time.sleep(.01)
                    continue
               
                byte = self.serial.read()

                if byte != APIFrame.START_BYTE:
                    continue

                # Save all following bytes, if they are not empty
                if len(byte) == 1:
                    frame.fill(byte)
                   
                while(frame.remaining_bytes() > 0):
                    byte = self.serial.read()
                   
                    if len(byte) == 1:
                        frame.fill(byte)

                try:
                    # Try to parse and return result
                    frame.parse()
                   
                    # Ignore empty frames
                    if len(frame.data) == 0:
                        frame = APIFrame()
                        continue
                       
                    return frame
                except ValueError:
                    # Bad frame, so restart
                    frame = APIFrame(escaped=self._escaped)
                   
        print "time-out while trying to read data"






Reply all
Reply to author
Forward
0 new messages