Skip to content

Commit

Permalink
Try to read more serial bytes in a loop (#248)
Browse files Browse the repository at this point in the history
  • Loading branch information
davidshin172 authored and mikepurvis committed Sep 9, 2016
1 parent 49ce807 commit 196f5dd
Showing 1 changed file with 15 additions and 4 deletions.
19 changes: 15 additions & 4 deletions rosserial_python/src/rosserial_python/SerialClient.py
Original file line number Diff line number Diff line change
Expand Up @@ -400,12 +400,23 @@ def txStopRequest(self, signal, frame):

def tryRead(self, length):
try:
bytes_read = self.port.read(length)
if len(bytes_read) < length:
read_start = time.time()
read_current = read_start
bytes_remaining = length
result = bytearray()
while bytes_remaining != 0 and read_current - read_start < self.timeout:
received = self.port.read(bytes_remaining)
if len(received) != 0:
result.extend(received)
bytes_remaining -= len(received)
read_current = time.time()

if bytes_remaining != 0:
rospy.logwarn("Serial Port read returned short (expected %d bytes, received %d instead)."
% (length, len(bytes_read)))
% (length, len(length - bytes_remaining)))
raise IOError()
return bytes_read

return bytes(result)
except Exception as e:
rospy.logwarn("Serial Port read failure: %s", e)
raise IOError()
Expand Down

0 comments on commit 196f5dd

Please sign in to comment.