Commit 4b97ed189cce0cda04b76126388f22ae4f2aa255

Authored by Christopher Stone
1 parent a2336b82
Exists in master

Added interactive serial port selection for multiple likely candidates, automati…

…c connection for a single likely candidate, and error handling for none
robots/little_john/telemetry/code/monitor/graph_plotter_rewrite.py
1   -#!/usr/bin/env python
  1 +#!/usr/bin/env python2
2 2  
3 3 # Tool to draw live graphs of data coming in from serial port
4 4 # Written as a telemetry tool by:
... ... @@ -11,17 +11,56 @@ import pyglet
11 11 #import time
12 12 import serial
13 13 import numpy
  14 +import os
14 15  
15 16 from colours import *
16 17  
17   -datafeed = serial.Serial(
18   -port='/dev/ttyUSB0',
19   -baudrate = 9600,
20   -parity=serial.PARITY_NONE,
21   -stopbits=serial.STOPBITS_ONE,
22   -bytesize=serial.EIGHTBITS,
23   -timeout=1
24   -)
  18 +devpatterns = ['ttyACM', 'ttyUSB', 'rfcomm']
  19 +
  20 +alldevs = os.listdir("/dev/")
  21 +targetdevs = []
  22 +for dev in alldevs:
  23 + for pattern in devpatterns:
  24 + if pattern in dev:
  25 + targetdevs.append(dev)
  26 +
  27 +
  28 +if len(targetdevs) == 0:
  29 + print("Sorry, no serial devices found.")
  30 + print("Exiting...")
  31 +elif len(targetdevs) > 1:
  32 + print("Found multiple serial devices: ")
  33 + for i, dev in enumerate(targetdevs):
  34 + print(" " + str(i) + ": /dev/" + dev)
  35 + while True:
  36 + try:
  37 + selection = int(input("Please enter your selection (as a digit):\n > "))
  38 + if selection >= 0 and selection < len(targetdevs):
  39 + break
  40 + except:
  41 + print("Choice unrecognised: please try again:")
  42 +else:
  43 + print("Only found one likely serial device: /dev/" + dev)
  44 + selection = 0
  45 +
  46 +serialport = "/dev/" + targetdevs[selection]
  47 +
  48 +try:
  49 + datafeed = serial.Serial(
  50 + port=serialport,
  51 + baudrate = 9600,
  52 + parity=serial.PARITY_NONE,
  53 + stopbits=serial.STOPBITS_ONE,
  54 + bytesize=serial.EIGHTBITS,
  55 + timeout=1
  56 + )
  57 +
  58 + print("Sucessfully opened " + serialport + " as data source!")
  59 +except Exception, e:
  60 + print("Failed to access " + serialport + " because:")
  61 + print(e)
  62 + print("Exiting...")
  63 + exit()
25 64  
26 65 class Series:
27 66 def __init__(self, points=100, title="Series title", xname="x-axis name", yname="y-axis name"):
... ... @@ -154,4 +193,4 @@ def pollSerial(elapsed):
154 193 # Pyglet looks after the main event loop, but this ensures that data keeps being read in
155 194 pyglet.clock.schedule_interval(pollSerial, 0.1)
156 195  
157   -pyglet.app.run()
158 196 \ No newline at end of file
  197 +pyglet.app.run()
... ...