From 8cff766ab8488f4f7fd32bcfb831c7b23fed6266 Mon Sep 17 00:00:00 2001 From: Hugo Stephens Date: Thu, 8 Mar 2018 16:40:51 +0000 Subject: [PATCH] Added port selection for windows --- robots/little_john/telemetry/code/monitor/graph_plotter_rewrite.py | 92 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++------------------------------------ 1 file changed, 56 insertions(+), 36 deletions(-) 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 888a109..ac31dd8 100755 --- a/robots/little_john/telemetry/code/monitor/graph_plotter_rewrite.py +++ b/robots/little_john/telemetry/code/monitor/graph_plotter_rewrite.py @@ -8,67 +8,86 @@ import pyglet #import math -#import time +import time import serial import numpy import os import platform +import sys 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': - serialport='COM5' - os='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: - devpatterns = ['ttyACM', 'ttyUSB', 'rfcomm'] - alldevs = os.listdir("/dev/") targetdevs = [] for dev in alldevs: for pattern in devpatterns: if pattern in dev: targetdevs.append(dev) + os='Other' #may be useful - if len(targetdevs) == 0: - print("Sorry, no serial devices found.") - print("Exiting...") - elif len(targetdevs) > 1: - print("Found multiple serial devices: ") - for i, dev in enumerate(targetdevs): - print(" " + str(i) + ": /dev/" + 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: - exit() - except: - print("Choice unrecognised: please try again:") +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/" + 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/" + dev) - selection = 0 - +if os=='Windows': + serialport=targetdevs[selection] +else: serialport = "/dev/" + targetdevs[selection] - os='Other' 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!") -except Exception, e: + 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!") +except Exception as e: print("Failed to access " + serialport + " because:") print(e) print("Exiting...") - exit() + sys.exit() class Series: def __init__(self, points=100, title="Series title", xname="x-axis name", yname="y-axis name"): @@ -111,6 +130,7 @@ class Plot(pyglet.window.Window): """Handle a resize event from the pyglet event loop""" self.bounds = ((int(self.width * self.margins[0]), int(self.width * (1 - self.margins[0]))), (int(self.height * self.margins[1]), int(self.height * (1 - self.margins[1])))) + # This sometimes seems to throw an error ('AttributeError: 'Plot' object has no attribute 'margins') when started for a second time from the same instance. Interesting. Causes the plot windows to freeze pyglet.window.Window.on_resize(self, width, height) def on_draw(self): -- libgit2 0.21.2