From cfa61172cfc82e6e2332a7034b238622969d275f Mon Sep 17 00:00:00 2001 From: Christopher Stone Date: Thu, 8 Mar 2018 17:40:56 +0000 Subject: [PATCH] Moved serial port selector to separate module, and fixed a few bugs in the linux-specific code. Note that this has not been tested for /correctly/ opening the serial port, only that it runs as expected without errors. --- robots/little_john/telemetry/code/monitor/graph_plotter_rewrite.py | 70 +++------------------------------------------------------------------- robots/little_john/telemetry/code/monitor/serialselect.py | 73 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 76 insertions(+), 67 deletions(-) create mode 100644 robots/little_john/telemetry/code/monitor/serialselect.py diff --git a/robots/little_john/telemetry/code/monitor/graph_plotter_rewrite.py b/robots/little_john/telemetry/code/monitor/graph_plotter_rewrite.py index c8207cb..dff2ded 100755 --- a/robots/little_john/telemetry/code/monitor/graph_plotter_rewrite.py +++ b/robots/little_john/telemetry/code/monitor/graph_plotter_rewrite.py @@ -14,77 +14,13 @@ import numpy import os import platform import sys +from serialselect import selectserial from colours import * -# Who doesn't like cross platform development -#if platform.system()=='Windows': -# targetdevs='COM5' -# os='Windows' - -devpatterns = ['ttyACM', 'ttyUSB', 'rfcomm'] -targetdevs = [] -if platform.system()=='Windows': - com_ports = ['COM%s' % (i + 1) for i in range(256)] - for port in com_ports: - try: - s = serial.Serial(port) - s.close() - targetdevs.append(port) - except (OSError, serial.SerialException): - pass - os='Windows' #may be useful -else: - alldevs = os.listdir("/dev/") - targetdevs = [] - for dev in alldevs: - for pattern in devpatterns: - if pattern in dev: - targetdevs.append("/dev/" + dev) - os='Other' #may be useful - - -if len(targetdevs) == 0: - print("Sorry, no serial devices found.") - print("Exiting...") - time.sleep(2) - sys.exit() -elif len(targetdevs) > 1: - print("Found multiple serial devices: ") - for i, dev in enumerate(targetdevs): - print(" " + str(i) + ": " + dev) - while True: - try: - selection = int(input("Please enter your selection (as a digit):\n > ")) - if selection >= 0 and selection < len(targetdevs): - break - except KeyboardInterrupt: - sys.exit() - except: - print("Choice unrecognised: please try again:") -else: - if os=='Windows': - print("Only found one likely serial device: " + targetdevs[0]) - selection = 0 - else: - print("Only found one likely serial device: " + dev) - -serialport=targetdevs[selection] - -try: - datafeed = serial.Serial( - port=serialport, - baudrate = 9600, - parity=serial.PARITY_NONE, - stopbits=serial.STOPBITS_ONE, - bytesize=serial.EIGHTBITS, - timeout=1 - ) +datafeed = selectserial() - print("Sucessfully opened " + serialport + " as data source!") -except Exception as e: - print("Failed to access " + serialport + " because:") - print(e) +if datafeed == None: print("Exiting...") sys.exit() diff --git a/robots/little_john/telemetry/code/monitor/serialselect.py b/robots/little_john/telemetry/code/monitor/serialselect.py new file mode 100644 index 0000000..01e6b00 --- /dev/null +++ b/robots/little_john/telemetry/code/monitor/serialselect.py @@ -0,0 +1,73 @@ +# Module to choose and open a serial port +# Written as a telemetry tool by: +# The UoN Robot Wars Project, 2018 + +def selectserial(): + import platform + import serial + import os + + devpatterns = ['ttyACM', 'ttyUSB', 'rfcomm'] + targetdevs = [] + if platform.system()=='Windows': + com_ports = ['COM%s' % (i + 1) for i in range(256)] + for port in com_ports: + try: + s = serial.Serial(port) + s.close() + targetdevs.append(port) + except (OSError, serial.SerialException): + pass + os='Windows' #may be useful + else: + alldevs = os.listdir("/dev/") + targetdevs = [] + for dev in alldevs: + for pattern in devpatterns: + if pattern in dev: + targetdevs.append("/dev/" + dev) + os='Other' #may be useful + + if len(targetdevs) == 0: + print("Sorry, no serial devices found.") + print("Exiting...") + return None + elif len(targetdevs) > 1: + print("Found multiple serial devices: ") + for i, dev in enumerate(targetdevs): + print(" " + str(i) + ": " + dev) + while True: + try: + selection = int(input("Please enter your selection (as a digit):\n > ")) + if selection >= 0 and selection < len(targetdevs): + break + except KeyboardInterrupt: + sys.exit() + except: + print("Choice unrecognised: please try again:") + else: + if os=='Windows': + print("Only found one likely serial device: " + targetdevs[0]) + else: + print("Only found one likely serial device: " + targetdevs[0]) + selection = 0 + + serialport=targetdevs[selection] + + + try: + datafeed = serial.Serial( + port=serialport, + baudrate = 9600, + parity=serial.PARITY_NONE, + stopbits=serial.STOPBITS_ONE, + bytesize=serial.EIGHTBITS, + timeout=1 + ) + + print("Sucessfully opened " + serialport + " as data source!") + return datafeed + except Exception as e: + print("Failed to access " + serialport + " because:") + print(e) + return None -- libgit2 0.21.2