Skip to content

Commit 2d3a2e1

Browse files
authored
Merge pull request #62 from XYR010/master
re-added an optional timeout for wait_read_frame, as in @thom-nic 's …
2 parents 44a29dc + fbddd60 commit 2d3a2e1

2 files changed

Lines changed: 13 additions & 3 deletions

File tree

xbee/backend/base.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818
class CommandFrameException(KeyError):
1919
pass
2020

21+
class TimeoutException(Exception):
22+
pass
23+
2124

2225
class XBeeBase(object):
2326
"""

xbee/thread/base.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
"""
1414
from xbee.frame import APIFrame
1515
from xbee.backend.base import XBeeBase as _XBeeBase
16+
from xbee.backend.base import TimeoutException as _TimeoutException
1617
import threading
1718
import time
1819

@@ -93,7 +94,7 @@ def run(self):
9394
if self._error_callback:
9495
self._error_callback(e)
9596

96-
def wait_read_frame(self):
97+
def wait_read_frame(self, timeout=None):
9798
"""
9899
wait_read_frame: None -> frame info dictionary
99100
@@ -102,10 +103,10 @@ def wait_read_frame(self):
102103
wait_read_frame attempts to parse the data contained within it
103104
and returns the resulting dictionary
104105
"""
105-
frame = self._wait_for_frame()
106+
frame = self._wait_for_frame(timeout)
106107
return self._split_response(frame.data)
107108

108-
def _wait_for_frame(self):
109+
def _wait_for_frame(self, timeout=None):
109110
"""
110111
_wait_for_frame: None -> binary data
111112
@@ -119,11 +120,17 @@ def _wait_for_frame(self):
119120
"""
120121
frame = APIFrame(escaped=self._escaped)
121122

123+
deadline = 0
124+
if timeout is not None and timeout > 0:
125+
deadline = time.time() + timeout
126+
122127
while True:
123128
if self._callback and not self._thread_continue:
124129
raise ThreadQuitException
125130

126131
if self.serial.inWaiting() == 0:
132+
if deadline and time.time() > deadline:
133+
raise _TimeoutException
127134
time.sleep(.01)
128135
continue
129136

0 commit comments

Comments
 (0)