Reconnect on serial port errors for KISS interface
This commit is contained in:
parent
7092589388
commit
a692d29c90
@ -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,44 +86,52 @@ class KISSInterface(Interface):
|
|||||||
self.parity = serial.PARITY_ODD
|
self.parity = serial.PARITY_ODD
|
||||||
|
|
||||||
try:
|
try:
|
||||||
RNS.log("Opening serial port "+self.port+"...")
|
self.open_port()
|
||||||
self.serial = serial.Serial(
|
|
||||||
port = self.port,
|
|
||||||
baudrate = self.speed,
|
|
||||||
bytesize = self.databits,
|
|
||||||
parity = self.parity,
|
|
||||||
stopbits = self.stopbits,
|
|
||||||
xonxoff = False,
|
|
||||||
rtscts = False,
|
|
||||||
timeout = 0,
|
|
||||||
inter_byte_timeout = None,
|
|
||||||
write_timeout = None,
|
|
||||||
dsrdtr = False,
|
|
||||||
)
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
RNS.log("Could not open serial port "+self.port, RNS.LOG_ERROR)
|
RNS.log("Could not open serial port "+self.port, RNS.LOG_ERROR)
|
||||||
raise e
|
raise e
|
||||||
|
|
||||||
if self.serial.is_open:
|
if self.serial.is_open:
|
||||||
# Allow time for interface to initialise before config
|
self.configure_device()
|
||||||
sleep(2.0)
|
|
||||||
thread = threading.Thread(target=self.readLoop)
|
|
||||||
thread.setDaemon(True)
|
|
||||||
thread.start()
|
|
||||||
self.online = True
|
|
||||||
RNS.log("Serial port "+self.port+" is now open")
|
|
||||||
RNS.log("Configuring KISS interface parameters...")
|
|
||||||
self.setPreamble(self.preamble)
|
|
||||||
self.setTxTail(self.txtail)
|
|
||||||
self.setPersistence(self.persistence)
|
|
||||||
self.setSlotTime(self.slottime)
|
|
||||||
self.setFlowControl(self.flow_control)
|
|
||||||
self.interface_ready = True
|
|
||||||
RNS.log("KISS interface configured")
|
|
||||||
else:
|
else:
|
||||||
raise IOError("Could not open serial port")
|
raise IOError("Could not open serial port")
|
||||||
|
|
||||||
|
|
||||||
|
def open_port(self):
|
||||||
|
RNS.log("Opening serial port "+self.port+"...")
|
||||||
|
self.serial = self.pyserial.Serial(
|
||||||
|
port = self.port,
|
||||||
|
baudrate = self.speed,
|
||||||
|
bytesize = self.databits,
|
||||||
|
parity = self.parity,
|
||||||
|
stopbits = self.stopbits,
|
||||||
|
xonxoff = False,
|
||||||
|
rtscts = False,
|
||||||
|
timeout = 0,
|
||||||
|
inter_byte_timeout = None,
|
||||||
|
write_timeout = None,
|
||||||
|
dsrdtr = False,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def configure_device(self):
|
||||||
|
# Allow time for interface to initialise before config
|
||||||
|
sleep(2.0)
|
||||||
|
thread = threading.Thread(target=self.readLoop)
|
||||||
|
thread.setDaemon(True)
|
||||||
|
thread.start()
|
||||||
|
self.online = True
|
||||||
|
RNS.log("Serial port "+self.port+" is now open")
|
||||||
|
RNS.log("Configuring KISS interface parameters...")
|
||||||
|
self.setPreamble(self.preamble)
|
||||||
|
self.setTxTail(self.txtail)
|
||||||
|
self.setPersistence(self.persistence)
|
||||||
|
self.setSlotTime(self.slottime)
|
||||||
|
self.setFlowControl(self.flow_control)
|
||||||
|
self.interface_ready = True
|
||||||
|
RNS.log("KISS interface configured")
|
||||||
|
|
||||||
|
|
||||||
def setPreamble(self, preamble):
|
def setPreamble(self, preamble):
|
||||||
preamble_ms = preamble
|
preamble_ms = preamble
|
||||||
preamble = int(preamble_ms / 10)
|
preamble = int(preamble_ms / 10)
|
||||||
@ -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+"]"
|
Loading…
Reference in New Issue
Block a user