From ffc6adefbd7f1ca3d40a92dcc035951ec695258c Mon Sep 17 00:00:00 2001 From: Andrew Tridgell Date: Wed, 29 Nov 2023 13:07:41 +1100 Subject: [PATCH] sertotcp: auto-reopen serial port --- tools/sertotcp.py | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/tools/sertotcp.py b/tools/sertotcp.py index 8603c35bd..32f62ac1b 100755 --- a/tools/sertotcp.py +++ b/tools/sertotcp.py @@ -1,4 +1,4 @@ -#!/usr/bin/env python +#!/usr/bin/env python3 """ map a serial port to an outgoing TCP connection @@ -39,12 +39,27 @@ def open_socket(): while True: gotdata = False + if serport is None: + try: + print("Reopening %s" % args.serialport) + serport = serial.Serial(args.serialport, args.baudrate, timeout=0) + except Exception: + time.sleep(1) + continue + print("Opened %s" % args.serialport) + + if tcpsock is None: open_socket() time.sleep(0.1) continue - n = serport.inWaiting() + try: + n = serport.inWaiting() + except Exception: + serport = None + time.sleep(1) + continue if n > 0: b = serport.read(n) if b: