Reconnect on serial port errors for KISS interface

This commit is contained in:
Mark Qvist 2021-12-05 11:44:30 +01:00
parent 7092589388
commit a692d29c90

View File

@ -53,6 +53,7 @@ class KISSInterface(Interface):
if beacon_data == None: if beacon_data == None:
beacon_data = "" beacon_data = ""
self.pyserial = serial
self.serial = None self.serial = None
self.owner = owner self.owner = owner
self.name = name self.name = name
@ -85,8 +86,20 @@ class KISSInterface(Interface):
self.parity = serial.PARITY_ODD self.parity = serial.PARITY_ODD
try: try:
self.open_port()
except Exception as e:
RNS.log("Could not open serial port "+self.port, RNS.LOG_ERROR)
raise e
if self.serial.is_open:
self.configure_device()
else:
raise IOError("Could not open serial port")
def open_port(self):
RNS.log("Opening serial port "+self.port+"...") RNS.log("Opening serial port "+self.port+"...")
self.serial = serial.Serial( self.serial = self.pyserial.Serial(
port = self.port, port = self.port,
baudrate = self.speed, baudrate = self.speed,
bytesize = self.databits, bytesize = self.databits,
@ -99,11 +112,9 @@ class KISSInterface(Interface):
write_timeout = None, write_timeout = None,
dsrdtr = False, dsrdtr = False,
) )
except Exception as e:
RNS.log("Could not open serial port "+self.port, RNS.LOG_ERROR)
raise e
if self.serial.is_open:
def configure_device(self):
# Allow time for interface to initialise before config # Allow time for interface to initialise before config
sleep(2.0) sleep(2.0)
thread = threading.Thread(target=self.readLoop) thread = threading.Thread(target=self.readLoop)
@ -119,8 +130,6 @@ class KISSInterface(Interface):
self.setFlowControl(self.flow_control) self.setFlowControl(self.flow_control)
self.interface_ready = True self.interface_ready = True
RNS.log("KISS interface configured") RNS.log("KISS interface configured")
else:
raise IOError("Could not open serial port")
def setPreamble(self, preamble): def setPreamble(self, preamble):
@ -295,5 +304,20 @@ class KISSInterface(Interface):
if RNS.Reticulum.panic_on_interface_error: if RNS.Reticulum.panic_on_interface_error:
RNS.panic() RNS.panic()
self.online = False
self.serial.close()
self.reconnect_port()
def reconnect_port(self):
while not self.online:
try:
time.sleep(5)
RNS.log("Attempting to reconnect serial port "+str(self.port)+" for "+str(self)+"...")
self.open_port()
if self.serial.is_open:
self.configure_device()
except Exception as e:
RNS.log("Error while reconnecting port, the contained exception was: "+str(e), RNS.LOG_ERROR)
def __str__(self): def __str__(self):
return "KISSInterface["+self.name+"]" return "KISSInterface["+self.name+"]"