Code:
#************************************************************************
# $Author: ehawkins $
# $Date: 2004/07/09 17:32:39 $
#
# $Log: gen3.py,v $
# Revision 1.1 2004/07/09 17:32:39 ehawkins
# Initial check-in of Python monitor code
#
#************************************************************************
from Tkinter import *
import pickle
import time
import re
import serial
import numarray
import csv
WINDOW = 600
NSAMPLES = 254
MAX = 1040
CHANNELS = 2
FRAMINGBYTES = 4
def OpenSerial():
global ser
ser = serial.Serial()
if ser.isOpen():
ser.close()
ser.baudrate = 9600
ser.port = 'COM1'
ser.open()
ser.setDTR()
def CloseSerial():
try:
if ser.isOpen():
ser.close()
except:
print "WARNING: Exception raised: ser.close() attempted before ser defined"
else:
print "WARNING: ser.close() has issues"
root = Tk()
root.title('GEN3')
root.bind('q','exit')
canvas = Canvas(root, width=WINDOW, height=WINDOW, background='white')
canvas.create_text(.1*WINDOW,WINDOW-.02*WINDOW,text="",font=("Helvetica", 12),tags="v00",fill="#00b000")
canvas.create_text(.1*WINDOW,WINDOW-.1*WINDOW,text="",font=("Helvetica", 12),tags="v10",fill="#b00000")
canvas.create_text(.30*WINDOW,WINDOW-.02*WINDOW,text="",font=("Helvetica", 12),tags="v0n",fill="#00b000")
canvas.create_text(.30*WINDOW,WINDOW-.1*WINDOW,text="",font=("Helvetica", 12),tags="v1n",fill="#b00000")
canvas.create_text(.50*WINDOW,WINDOW-.02*WINDOW,text="",font=("Helvetica", 12),tags="v0-",fill="#00b000")
canvas.create_text(.50*WINDOW,WINDOW-.1*WINDOW,text="",font=("Helvetica", 12),tags="v1-",fill="#b00000")
canvas.create_text(.65*WINDOW,WINDOW-.02*WINDOW,text="",font=("Helvetica", 12),tags="v0c",fill="#00b000")
canvas.create_text(.65*WINDOW,WINDOW-.1*WINDOW,text="",font=("Helvetica", 12),tags="v1c",fill="#b00000")
OpenSerial()
for test in range(10):
line2d = []
path = numarray.zeros((CHANNELS, (NSAMPLES*2 + 2)))
imp = numarray.zeros((CHANNELS, NSAMPLES))
diff = numarray.zeros((CHANNELS, NSAMPLES))
byte = numarray.zeros((FRAMINGBYTES))
cap = numarray.zeros((CHANNELS))
print "Finding framing ..."
while not (((byte[0] == 69) & (byte[1] == 114) & (byte[2] == 105) & (byte[3] == 99))|((byte[0] == 78) & (byte[1] == 101) & (byte[2] == 97) & (byte[3] == 116))):
#while not ((byte[0] == 69) & (byte[1] == 114) & (byte[2] == 105) & (byte[3] == 99)):
for i in range(FRAMINGBYTES-1):
byte[i] = byte[i+1]
byte[FRAMINGBYTES-1] = ord(ser.read())
if byte[0] == 69:
channel = 1
color = "#b00000"
else:
channel = 0
color = "#00b000"
print byte
print "Getting channel " + str(channel) + " data ..."
for datacnt in range(NSAMPLES):
x = datacnt * 2
y = x + 1
lo = ord(ser.read())
hi = ord(ser.read())
value = (hi << 8) + lo
imp[channel][datacnt] = (value)
path[channel][x] = (WINDOW-(value*WINDOW/float(MAX)))
path[channel][y] = (WINDOW-(datacnt*WINDOW/float(NSAMPLES)))
if not (datacnt == 0): diff[channel][datacnt] = imp[channel][datacnt - 1] - value
if diff[channel][datacnt] < 0: diff[channel][datacnt] = 0
print imp[channel]
print value
for i in range(NSAMPLES*2+2):
line2d.insert(0,path[channel][i])
canvas.delete("path"+str(channel))
canvas.create_line(line2d,tag="path"+str(channel),fill=color)
canvas.itemconfigure("v"+str(channel)+"0",text="CH[%i][0]: %d"% (channel, imp[channel][-1]))
canvas.itemconfigure("v"+str(channel)+"-",text="dV/dt: %d"%(imp[channel][NSAMPLES-2]-imp[channel][NSAMPLES-1]))
canvas.itemconfigure("v"+str(channel)+"n",text="CH[%i][-1]: %d"% (channel, imp[channel][0]))
canvas.itemconfigure("v"+str(channel)+"c",text="C: %d"% (cap[channel]))
canvas.itemconfigure("v"+str(channel)+"c",text="C: %d"% ((imp[channel][0]-imp[channel][-1])/(imp[channel][NSAMPLES-2]-imp[channel][NSAMPLES-1])))
canvas.pack()
print "CH: %d, %d, c: %d"% (channel, NSAMPLES-2, imp[channel][NSAMPLES-2])
print "CH: %d, %d, c: %d"% (channel, NSAMPLES-1, imp[channel][NSAMPLES-1])
print "Writing channel " + str(channel) + " data ..."
grep = re.compile(' |:')
fNAME = grep.sub("_", str(time.ctime())) + "_CH" + str(channel) + ".csv"
FILE = open(fNAME, "w")
FILE.write("Initial Voltage, Final Voltage, dV/dt, Cap\n")
for i in range(NSAMPLES):
FILE.write("%u, %u, %u, %u\n" % (line2d[i*2], imp[channel][NSAMPLES-i-1], diff[channel][NSAMPLES-i-1],(imp[channel][0]-imp[channel][-1])/(imp[channel][NSAMPLES-2]-imp[channel][NSAMPLES-1])))
FILE.close()
CloseSerial()