Commit cfa61172cfc82e6e2332a7034b238622969d275f
1 parent
e48ed38f
Exists in
master
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.
Showing
2 changed files
with
76 additions
and
67 deletions
Show diff stats
robots/little_john/telemetry/code/monitor/graph_plotter_rewrite.py
| @@ -14,77 +14,13 @@ import numpy | @@ -14,77 +14,13 @@ import numpy | ||
| 14 | import os | 14 | import os |
| 15 | import platform | 15 | import platform |
| 16 | import sys | 16 | import sys |
| 17 | +from serialselect import selectserial | ||
| 17 | 18 | ||
| 18 | from colours import * | 19 | from colours import * |
| 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 = [] | ||
| 26 | -if platform.system()=='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 | ||
| 36 | -else: | ||
| 37 | - alldevs = os.listdir("/dev/") | ||
| 38 | - targetdevs = [] | ||
| 39 | - for dev in alldevs: | ||
| 40 | - for pattern in devpatterns: | ||
| 41 | - if pattern in dev: | ||
| 42 | - targetdevs.append("/dev/" + dev) | ||
| 43 | - os='Other' #may be useful | ||
| 44 | - | ||
| 45 | - | ||
| 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) | ||
| 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 | ||
| 68 | - else: | ||
| 69 | - print("Only found one likely serial device: " + dev) | ||
| 70 | - | ||
| 71 | -serialport=targetdevs[selection] | ||
| 72 | - | ||
| 73 | 20 | ||
| 74 | -try: | ||
| 75 | - datafeed = serial.Serial( | ||
| 76 | - port=serialport, | ||
| 77 | - baudrate = 9600, | ||
| 78 | - parity=serial.PARITY_NONE, | ||
| 79 | - stopbits=serial.STOPBITS_ONE, | ||
| 80 | - bytesize=serial.EIGHTBITS, | ||
| 81 | - timeout=1 | ||
| 82 | - ) | 21 | +datafeed = selectserial() |
| 83 | 22 | ||
| 84 | - print("Sucessfully opened " + serialport + " as data source!") | ||
| 85 | -except Exception as e: | ||
| 86 | - print("Failed to access " + serialport + " because:") | ||
| 87 | - print(e) | 23 | +if datafeed == None: |
| 88 | print("Exiting...") | 24 | print("Exiting...") |
| 89 | sys.exit() | 25 | sys.exit() |
| 90 | 26 |
robots/little_john/telemetry/code/monitor/serialselect.py
0 → 100644
| @@ -0,0 +1,73 @@ | @@ -0,0 +1,73 @@ | ||
| 1 | +# Module to choose and open a serial port | ||
| 2 | +# Written as a telemetry tool by: | ||
| 3 | +# The UoN Robot Wars Project, 2018 | ||
| 4 | + | ||
| 5 | +def selectserial(): | ||
| 6 | + import platform | ||
| 7 | + import serial | ||
| 8 | + import os | ||
| 9 | + | ||
| 10 | + devpatterns = ['ttyACM', 'ttyUSB', 'rfcomm'] | ||
| 11 | + targetdevs = [] | ||
| 12 | + if platform.system()=='Windows': | ||
| 13 | + com_ports = ['COM%s' % (i + 1) for i in range(256)] | ||
| 14 | + for port in com_ports: | ||
| 15 | + try: | ||
| 16 | + s = serial.Serial(port) | ||
| 17 | + s.close() | ||
| 18 | + targetdevs.append(port) | ||
| 19 | + except (OSError, serial.SerialException): | ||
| 20 | + pass | ||
| 21 | + os='Windows' #may be useful | ||
| 22 | + else: | ||
| 23 | + alldevs = os.listdir("/dev/") | ||
| 24 | + targetdevs = [] | ||
| 25 | + for dev in alldevs: | ||
| 26 | + for pattern in devpatterns: | ||
| 27 | + if pattern in dev: | ||
| 28 | + targetdevs.append("/dev/" + dev) | ||
| 29 | + os='Other' #may be useful | ||
| 30 | + | ||
| 31 | + if len(targetdevs) == 0: | ||
| 32 | + print("Sorry, no serial devices found.") | ||
| 33 | + print("Exiting...") | ||
| 34 | + return None | ||
| 35 | + elif len(targetdevs) > 1: | ||
| 36 | + print("Found multiple serial devices: ") | ||
| 37 | + for i, dev in enumerate(targetdevs): | ||
| 38 | + print(" " + str(i) + ": " + dev) | ||
| 39 | + while True: | ||
| 40 | + try: | ||
| 41 | + selection = int(input("Please enter your selection (as a digit):\n > ")) | ||
| 42 | + if selection >= 0 and selection < len(targetdevs): | ||
| 43 | + break | ||
| 44 | + except KeyboardInterrupt: | ||
| 45 | + sys.exit() | ||
| 46 | + except: | ||
| 47 | + print("Choice unrecognised: please try again:") | ||
| 48 | + else: | ||
| 49 | + if os=='Windows': | ||
| 50 | + print("Only found one likely serial device: " + targetdevs[0]) | ||
| 51 | + else: | ||
| 52 | + print("Only found one likely serial device: " + targetdevs[0]) | ||
| 53 | + selection = 0 | ||
| 54 | + | ||
| 55 | + serialport=targetdevs[selection] | ||
| 56 | + | ||
| 57 | + | ||
| 58 | + try: | ||
| 59 | + datafeed = serial.Serial( | ||
| 60 | + port=serialport, | ||
| 61 | + baudrate = 9600, | ||
| 62 | + parity=serial.PARITY_NONE, | ||
| 63 | + stopbits=serial.STOPBITS_ONE, | ||
| 64 | + bytesize=serial.EIGHTBITS, | ||
| 65 | + timeout=1 | ||
| 66 | + ) | ||
| 67 | + | ||
| 68 | + print("Sucessfully opened " + serialport + " as data source!") | ||
| 69 | + return datafeed | ||
| 70 | + except Exception as e: | ||
| 71 | + print("Failed to access " + serialport + " because:") | ||
| 72 | + print(e) | ||
| 73 | + return None |