############################################### ## Logs serial data from the USB Bit Whacker ## Scott Hendrickson ## Aug 28, 2007 ############################################### from serial import * # This program relies on: # Python Serial Port Extension for Win32, Linux, BSD, Jython # serial driver for win32 # by Chris Liechti # (Distributed with Enthought Python) import sys, time, csv def command(c): buf = '' tty.write(c) while (tty.inWaiting() > 0): buf = tty.read(tty.inWaiting()) return buf def volts(x): return x*5./1024. ## Main # Initialize the serial port tty = Serial('COM6',9600,timeout=0) tty.databits = 8 tty.open() print tty print "Please initialize the UBW-PIC device. Press return when ready...\n" wait = sys.stdin.readline() print 'USB Port Volt Meter V1.0 (2007-08-26)' ## UBW Firmware D V1.3 is the data acquisition device # prints the version print command('V\r') + '\n' # set A = 11000000 B = 0, C = 255 inputs and analog count to 2 (an0, an1) command('C,3,0,255,2\r') # Tell the UBW to send an analog reading every 80 ms command('T,80,1\r') # open the output file writer = csv.writer(open('volt_results.csv','wb')) reading = [] count = 0 # Start logging set this to the number of intervals to record # (500*80 ms = 40 sec) while (count<500): while (tty.inWaiting() > 0): buf = tty.read(tty.inWaiting()) reading = buf.split(',') if len(reading) > 1: v1 = volts(int(reading[1].strip('\n\r'))) v2 = volts(int(reading[2].strip('\n\r'))) # print readings to screen sys.stdout.write('\r (%.2f, %.2f) Volts' % (v1,v2)) # log voltage readings writer.writerow([time.clock(),v1,v2]) count += 1 reading = [] print '\nDone.'