From b05e88f9d191afe944b881dfed6803c90f3ba07d Mon Sep 17 00:00:00 2001 From: Raphael Michel Date: Fri, 18 Nov 2016 12:50:52 +0100 Subject: [PATCH] Re-connect without proper disconnect fails on Windows --- ecr.py | 1 - utils.py | 16 +++++++++++----- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/ecr.py b/ecr.py index 1ce15b1..d951c14 100644 --- a/ecr.py +++ b/ecr.py @@ -392,7 +392,6 @@ class ECR(object): # note: this only executes utils.detect_pt with the local ecrterm. from ecrterm.utils import detect_pt result = detect_pt(silent=False, ecr=self, timeout=2) - self.transport.connect() return result def parse_str(self, s): diff --git a/utils.py b/utils.py index fd7b5f3..d09a3a9 100644 --- a/utils.py +++ b/utils.py @@ -49,13 +49,19 @@ def detect_pt(device='/dev/ttyUSB0', timeout=2, silent=True, def __detect_pt(port, timeout, ecr): e = ecr or ECR(port) # reconnect to have lower timeout + e.transport.close() e.transport.connect(timeout=timeout) errors = e.transmit(packets.StatusEnquiry()) - if not errors: - if isinstance(e.last.completion, packets.Completion): - return e.last.completion.fixed_values.get('sw-version', True) or True - return True - return False + try: + if not errors: + if isinstance(e.last.completion, packets.Completion): + return e.last.completion.fixed_values.get('sw-version', True) or True + return True + return False + finally: + # Reset timeout + e.transport.close() + e.transport.connect() if silent: try: return __detect_pt(device, timeout, ecr)