#! /usr/bin/env python # # Version History: # # Version 1.0: Initial Release # # Version 1.1: 27 Sept 2012. # Change: This version won't delete (pop) data unless it is going to enter # in a new piece of data. # from serial import * import sys import time from pylab import * from collections import deque # Parameters you may change: # plotSkip: Refresh the plot after this many data lines are read # buffL: buffer length, ie, how much data to plot. # startSkip: A serial port seems to have a "memory" of several lines, # which were saved from the previous experiment run. # ** Must be greater than 0. plotSkip = 3 buffL = 80 startSkip = 30 # remove junk from start of file. for i in range(startSkip): line = sys.stdin.readline() # Use the most recent line to determine how many columns (streams) there are. lineInt = [int(i) for i in line.split()] columns = len(lineInt) streams = columns/2 # Every stream requires two columns (time, rss) rss = lineInt[1::2] # take odd number columns times = lineInt[0::2] # take even number columns # Init the figure. ion() RSSBuffer = [] TimeBuffer = [] linePlot = [] cla() for n in range(streams): RSSBuffer.append( deque([rss[n]] * buffL)) TimeBuffer.append( deque([times[n]] * buffL)) l, = plot(arange(buffL)-buffL, RSSBuffer[n],label=str(n)) axis([-buffL*28, 0, -95, -25]) xlabel('Received Power (dBm)') ylabel('Measurement Time Ago (samples)') linePlot.append(l) # Run forever, adding lines as they are available. counter = 0 while 1: line = sys.stdin.readline() if not line: continue while line[-1] != '\n': # If line is incomplete, add to it. line += fin.readline() # Get the integers from the line string data = [int(i) for i in line.split()] rss = data[1::2] # take odd number columns times = data[0::2] # take even number columns # Append the queue for each stream with the newest RSS and Timestamps for i in range(streams): # data > -10 indicates no data measured. Don't include a new value. if (rss[i] < -10): oldRSS = RSSBuffer[i].popleft() oldTS = TimeBuffer[i].popleft() RSSBuffer[i].append(rss[i]) TimeBuffer[i].append(times[i]) # Every plotSkip rows, redraw the plot. Time stamps, relative to the # maximum time stamp, are on the x-axis. counter += 1 if mod(counter, plotSkip) == 0: mintime = 0 for i in range(streams): linePlot[i].set_ydata(RSSBuffer[i]) relTime = array(TimeBuffer[i]) - max(TimeBuffer[i]) linePlot[i].set_xdata(relTime) mintime = min(min(relTime), mintime) axis([mintime, 0, -95, -35]) draw()