Commit 8cff766ab8488f4f7fd32bcfb831c7b23fed6266
1 parent
2c1aba27
Exists in
master
Added port selection for windows
multiple serial case on windows not tested, but 'should' work. changed to sys.exit() mostly out of personal preference - may help to avoid error - see comment l133
Showing
1 changed file
with
56 additions
and
36 deletions
Show diff stats
robots/little_john/telemetry/code/monitor/graph_plotter_rewrite.py
| @@ -8,67 +8,86 @@ | @@ -8,67 +8,86 @@ | ||
| 8 | 8 | ||
| 9 | import pyglet | 9 | import pyglet |
| 10 | #import math | 10 | #import math |
| 11 | -#import time | 11 | +import time |
| 12 | import serial | 12 | import serial |
| 13 | import numpy | 13 | import numpy |
| 14 | import os | 14 | import os |
| 15 | import platform | 15 | import platform |
| 16 | +import sys | ||
| 16 | 17 | ||
| 17 | from colours import * | 18 | from colours import * |
| 18 | # Who doesn't like cross platform development | 19 | # Who doesn't like cross platform development |
| 20 | +#if platform.system()=='Windows': | ||
| 21 | +# targetdevs='COM5' | ||
| 22 | +# os='Windows' | ||
| 23 | + | ||
| 24 | +devpatterns = ['ttyACM', 'ttyUSB', 'rfcomm'] | ||
| 25 | +targetdevs = [] | ||
| 19 | if platform.system()=='Windows': | 26 | if platform.system()=='Windows': |
| 20 | - serialport='COM5' | ||
| 21 | - os='Windows' | 27 | + com_ports = ['COM%s' % (i + 1) for i in range(256)] |
| 28 | + for port in com_ports: | ||
| 29 | + try: | ||
| 30 | + s = serial.Serial(port) | ||
| 31 | + s.close() | ||
| 32 | + targetdevs.append(port) | ||
| 33 | + except (OSError, serial.SerialException): | ||
| 34 | + pass | ||
| 35 | + os='Windows' #may be useful | ||
| 22 | else: | 36 | else: |
| 23 | - devpatterns = ['ttyACM', 'ttyUSB', 'rfcomm'] | ||
| 24 | - | ||
| 25 | alldevs = os.listdir("/dev/") | 37 | alldevs = os.listdir("/dev/") |
| 26 | targetdevs = [] | 38 | targetdevs = [] |
| 27 | for dev in alldevs: | 39 | for dev in alldevs: |
| 28 | for pattern in devpatterns: | 40 | for pattern in devpatterns: |
| 29 | if pattern in dev: | 41 | if pattern in dev: |
| 30 | targetdevs.append(dev) | 42 | targetdevs.append(dev) |
| 43 | + os='Other' #may be useful | ||
| 31 | 44 | ||
| 32 | 45 | ||
| 33 | - if len(targetdevs) == 0: | ||
| 34 | - print("Sorry, no serial devices found.") | ||
| 35 | - print("Exiting...") | ||
| 36 | - elif len(targetdevs) > 1: | ||
| 37 | - print("Found multiple serial devices: ") | ||
| 38 | - for i, dev in enumerate(targetdevs): | ||
| 39 | - print(" " + str(i) + ": /dev/" + dev) | ||
| 40 | - while True: | ||
| 41 | - try: | ||
| 42 | - selection = int(input("Please enter your selection (as a digit):\n > ")) | ||
| 43 | - if selection >= 0 and selection < len(targetdevs): | ||
| 44 | - break | ||
| 45 | - except KeyboardInterrupt: | ||
| 46 | - exit() | ||
| 47 | - except: | ||
| 48 | - print("Choice unrecognised: please try again:") | 46 | +if len(targetdevs) == 0: |
| 47 | + print("Sorry, no serial devices found.") | ||
| 48 | + print("Exiting...") | ||
| 49 | + time.sleep(2) | ||
| 50 | + sys.exit() | ||
| 51 | +elif len(targetdevs) > 1: | ||
| 52 | + print("Found multiple serial devices: ") | ||
| 53 | + for i, dev in enumerate(targetdevs): | ||
| 54 | + print(" " + str(i) + ": /dev/" + dev) | ||
| 55 | + while True: | ||
| 56 | + try: | ||
| 57 | + selection = int(input("Please enter your selection (as a digit):\n > ")) | ||
| 58 | + if selection >= 0 and selection < len(targetdevs): | ||
| 59 | + break | ||
| 60 | + except KeyboardInterrupt: | ||
| 61 | + sys.exit() | ||
| 62 | + except: | ||
| 63 | + print("Choice unrecognised: please try again:") | ||
| 64 | +else: | ||
| 65 | + if os=='Windows': | ||
| 66 | + print("Only found one likely serial device: " + targetdevs[0]) | ||
| 67 | + selection = 0 | ||
| 49 | else: | 68 | else: |
| 50 | print("Only found one likely serial device: /dev/" + dev) | 69 | print("Only found one likely serial device: /dev/" + dev) |
| 51 | - selection = 0 | ||
| 52 | - | 70 | +if os=='Windows': |
| 71 | + serialport=targetdevs[selection] | ||
| 72 | +else: | ||
| 53 | serialport = "/dev/" + targetdevs[selection] | 73 | serialport = "/dev/" + targetdevs[selection] |
| 54 | - os='Other' | ||
| 55 | 74 | ||
| 56 | try: | 75 | try: |
| 57 | -datafeed = serial.Serial( | ||
| 58 | -port=serialport, | ||
| 59 | -baudrate = 9600, | ||
| 60 | -parity=serial.PARITY_NONE, | ||
| 61 | -stopbits=serial.STOPBITS_ONE, | ||
| 62 | -bytesize=serial.EIGHTBITS, | ||
| 63 | -timeout=1 | ||
| 64 | -) | ||
| 65 | - | ||
| 66 | -print("Sucessfully opened " + serialport + " as data source!") | ||
| 67 | -except Exception, e: | 76 | + datafeed = serial.Serial( |
| 77 | + port=serialport, | ||
| 78 | + baudrate = 9600, | ||
| 79 | + parity=serial.PARITY_NONE, | ||
| 80 | + stopbits=serial.STOPBITS_ONE, | ||
| 81 | + bytesize=serial.EIGHTBITS, | ||
| 82 | + timeout=1 | ||
| 83 | + ) | ||
| 84 | + | ||
| 85 | + print("Sucessfully opened " + serialport + " as data source!") | ||
| 86 | +except Exception as e: | ||
| 68 | print("Failed to access " + serialport + " because:") | 87 | print("Failed to access " + serialport + " because:") |
| 69 | print(e) | 88 | print(e) |
| 70 | print("Exiting...") | 89 | print("Exiting...") |
| 71 | - exit() | 90 | + sys.exit() |
| 72 | 91 | ||
| 73 | class Series: | 92 | class Series: |
| 74 | def __init__(self, points=100, title="Series title", xname="x-axis name", yname="y-axis name"): | 93 | 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): | @@ -111,6 +130,7 @@ class Plot(pyglet.window.Window): | ||
| 111 | """Handle a resize event from the pyglet event loop""" | 130 | """Handle a resize event from the pyglet event loop""" |
| 112 | self.bounds = ((int(self.width * self.margins[0]), int(self.width * (1 - self.margins[0]))), | 131 | self.bounds = ((int(self.width * self.margins[0]), int(self.width * (1 - self.margins[0]))), |
| 113 | (int(self.height * self.margins[1]), int(self.height * (1 - self.margins[1])))) | 132 | (int(self.height * self.margins[1]), int(self.height * (1 - self.margins[1])))) |
| 133 | + # 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 | ||
| 114 | pyglet.window.Window.on_resize(self, width, height) | 134 | pyglet.window.Window.on_resize(self, width, height) |
| 115 | 135 | ||
| 116 | def on_draw(self): | 136 | def on_draw(self): |