Compare View

switch
from
...
to
 
Commits (2)
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,58 @@ 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 KeyboardInterrupt:
  41 + exit()
  42 + except:
  43 + print("Choice unrecognised: please try again:")
  44 +else:
  45 + print("Only found one likely serial device: /dev/" + dev)
  46 + selection = 0
  47 +
  48 +serialport = "/dev/" + targetdevs[selection]
  49 +
  50 +try:
  51 + datafeed = serial.Serial(
  52 + port=serialport,
  53 + baudrate = 9600,
  54 + parity=serial.PARITY_NONE,
  55 + stopbits=serial.STOPBITS_ONE,
  56 + bytesize=serial.EIGHTBITS,
  57 + timeout=1
  58 + )
  59 +
  60 + print("Sucessfully opened " + serialport + " as data source!")
  61 +except Exception, e:
  62 + print("Failed to access " + serialport + " because:")
  63 + print(e)
  64 + print("Exiting...")
  65 + exit()
25 66  
26 67 class Series:
27 68 def __init__(self, points=100, title="Series title", xname="x-axis name", yname="y-axis name"):
... ... @@ -154,4 +195,4 @@ def pollSerial(elapsed):
154 195 # Pyglet looks after the main event loop, but this ensures that data keeps being read in
155 196 pyglet.clock.schedule_interval(pollSerial, 0.1)
156 197  
157   -pyglet.app.run()
158 198 \ No newline at end of file
  199 +pyglet.app.run()
... ...