serialread.py

#!/usr/bin/env python 

""" 
    Language: Python
 
    Serial Reader: reads an array of A/D values from the datalogger,
    saves and plots the array.

    Eric Ayars
"""

import serial
import scipy as sp
import sys
import Gnuplot

FileName = sys.argv[1]

# Open serial communications
port = serial.Serial('/dev/tty.usbserial-FTE4W7UD', 9600)
port.setDTR(True)
port.flushInput()

# read data interval
dt = 0.001*float(port.readline(eol='\n'))
print "Time interval: %f" % dt

# read number of points
N = int(port.readline(eol='\n'))
print "Number of points: %d" % N

# set up storage space
Data = sp.zeros([N,4])

# read data array
for j in range(N):
    Data[j,0] = j*dt
    line = port.readline(eol='\n').split('\t')
    Data[j,1] = int(line[0])
    Data[j,2] = int(line[1])
    Data[j,3] = int(line[2])

port.close()

# now save the data
file = open(FileName, 'w')
# write file header
file.write("# Time\traw data\n" )
# write data
for i in range(N):
    file.write("%6.3f\t%d\t%d\t%d\n" % (Data[j,0], Data[j,1], Data[j,2], Data[j,3]))
file.close()

# Now plot the data, also.
gp = Gnuplot.Gnuplot(persist=True)
gp('set xlabel "Time (s)"')
gp('set ylabel "Acceleration (A/D units)"')
gp('set border 3')
gp('set tics out')
gp('set xtics nomirror')
gp('set ytics nomirror')
X = Gnuplot.Data(Data[:,0], Data[:,1], with='lines')
Y = Gnuplot.Data(Data[:,0], Data[:,2], with='lines')
Z = Gnuplot.Data(Data[:,0], Data[:,3], with='lines')
gp.plot(X,Y,Z)

Generated by GNU enscript 1.6.4.