From: NeilBrown Date: Fri, 13 Dec 2013 09:28:53 +0000 (+1100) Subject: atchan: retry if open fails. X-Git-Url: http://git.neil.brown.name/?a=commitdiff_plain;h=5cbdb18bb72f3856db5975d8fa8c96fd8d2e0b96;p=plato.git atchan: retry if open fails. This is useful for USB attached TTY which resets occasionally for some reason. --- diff --git a/gsm/atchan.py b/gsm/atchan.py index bc92b23..a1c6ed1 100644 --- a/gsm/atchan.py +++ b/gsm/atchan.py @@ -36,11 +36,20 @@ class AtChannel: self.sock = None self.connected = False - def connect(self): + def connect(self, retry=10000): if self.sock != None: return log("connect to", self.path) - s = open(self.path,"r+") + s = None + while s == None and retry > 0: + retry -= 1 + try: + s = open(self.path,"r+") + except IOError: + time.sleep(0.4) + s = None + if not s: + return False #s.setblocking(0) fd = s.fileno() attr = termios.tcgetattr(fd) @@ -48,10 +57,11 @@ class AtChannel: attr[6][termios.VMIN] = 0 attr[6][termios.VTIME] = 0 termios.tcsetattr(fd, termios.TCSANOW, attr) - + self.watcher = gobject.io_add_watch(s, gobject.IO_IN, self.readdata) self.sock = s self.connected = True + return True def readdata(self, io, arg): try: @@ -116,7 +126,7 @@ class AtChannel: self.timer = None self.timedout() return False - + def set_timeout(self, delay): if self.pending: raise ValueError @@ -127,7 +137,7 @@ class AtChannel: if self.pending: gobject.source_remove(self.timer) self.pending = False - + def abort_timeout(self): if self.pending: self.cancel_timeout()