Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow setting the timeout for the serial line. #91

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions mp/conserial.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@


class ConSerial(ConBase):
def __init__(self, port, baudrate=115200, reset=False):
def __init__(self, port, baudrate=115200, reset=False, timeout=1):
ConBase.__init__(self)

try:
self.serial = Serial(port, baudrate=baudrate, interCharTimeout=1)
self.serial = Serial(port, baudrate=baudrate, interCharTimeout=1, timeout=timeout)

if reset:
logging.info("Hard resetting device at port: %s" % port)
Expand All @@ -44,7 +44,7 @@ def __init__(self, port, baudrate=115200, reset=False):
self.serial.setDTR(False)

self.serial.close()
self.serial = Serial(port, baudrate=baudrate, interCharTimeout=1)
self.serial = Serial(port, baudrate=baudrate, interCharTimeout=1, timeout=timeout)

while True:
time.sleep(2.0)
Expand Down
3 changes: 2 additions & 1 deletion mp/mpfexp.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,8 @@ def __con_from_str(self, constr):
else:
baudrate = 115200

con = ConSerial(port=port, baudrate=baudrate, reset=self.reset)
timeout = 0.1 # 100 ms timeout in serial.read()
con = ConSerial(port=port, baudrate=baudrate, reset=self.reset, timeout=timeout)

elif proto.strip(" ") == "tn":

Expand Down
22 changes: 9 additions & 13 deletions mp/pyboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,24 +36,20 @@ def close(self):

def read_until(self, min_num_bytes, ending, timeout=10, data_consumer=None):

data = self.con.read(min_num_bytes)
to_read = max(min_num_bytes, len(ending))
data = self.con.read(to_read)
if data_consumer:
data_consumer(data)
timeout_count = 0
time_start = time.monotonic()
while True:
if data.endswith(ending):
break
elif self.con.inWaiting() > 0:
new_data = self.con.read(1)
data = data + new_data
if data_consumer:
data_consumer(new_data)
timeout_count = 0
else:
timeout_count += 1
if timeout is not None and timeout_count >= 100 * timeout:
break
time.sleep(0.01)
new_data = self.con.read(1)
data = data + new_data
if data_consumer:
data_consumer(new_data)
if time.monotonic() - time_start > timeout:
break
return data

def enter_raw_repl(self):
Expand Down