This is my first time working with the u3. I am trying to write a streaming program in Python that gets analog readings from FIO4 (called AIN4 when configured to be analog, I believe) and convert them to voltages. I am going to then plot those voltages versus time, and eventually frequency.
My goal is to be able to continuously stream data and plot it versus time. I am only reading one channel, so I believe I can read at 50k/sec. I have tried to use the low level command d.getFeedback(u3.AIN(4)), but I get an error that the stream has already been started.
I chose to convert all of the unsigned values outside the loop in order to minimize the number of packets lost.
I am having trouble appending an array with the readings. With the current code, I get a KeyError:
Traceback (most recent call last):File "C:/Users/_____/PycharmProjects/New1/src/Streaming Program.py", line 59, in <module>rawDataList.append(r['AIN4'])KeyError: 'AIN4'
Below is my code, with remnants of old code now commented just in case.
# We need the date and time in order to plot our data
from datetime import datetime
#importing matplotlib in order to plot what we get
import matplotlib.pyplot as plt
import matplotlib.animation as animation
#import the u3
# Open the U3
d = u3.U3()
# Set the FIO4 to analog (and all channels below) which is AIN4 later for low level commands.
# MAX_REQUESTS is the number of packets to be read.
MAX_REQUESTS = 75
# For applying the proper calibration for readings from unsigned values to voltages
# Set the steam parameters. 1 channel, positive channel #4, negative channel #31 = singled ended, resolution 3 -> higher noise but faster sampling
# Sample rate = scanrate * numchannels, max ~50k samples/s. Samples per packet = 25 for data transfer efficiency. packetsPerRequest =48
print "Configuring U3 Stream..."
d.streamConfig(NumChannels=1, PChannels=, NChannels=, Resolution = 3 , ScanFrequency = 51000, SamplesPerPacket = 25)
# To prevent the program from crashing when encountering an exception, we use a try statement
start = datetime.now() # print the scan start time
print "Started stream at " , datetime.now(), '\n'
missed = 0 # initialize counters
dataCount = 0
packetCount = 0
for r in d.streamData(convert=False):
if r is not None:
#packetStartTime = datetime.now()
#miniRuntime = (packetStartTime - start).seconds + float((packetStartTime - start).microseconds) / 1000000
# Our stop condition
if dataCount >= MAX_REQUESTS:
if r['errors'] != 0:
print "Error: %s ; " % r['errors'], datetime.now()
if r['numPackets'] != d.packetsPerRequest:
print "----- UNDERFLOW : %s : " % r['numPackets'], datetime.now()
if r['missed'] != 0:
missed += r['missed']
print "+++ Missed ", r['missed']
#ain4bits = r['AIN4']
#Comment out these prints and do something with r
#print "Average of", (r['AIN4'])
#print 8*sum(r['AIN4'])/len(r['AIN4']), ",", miniruntime
# h1, = plt.plot(,)
#def update_line(h1, new_data):
dataCount += 1
packetCount += r['numPackets']
# Got no data back from our read.
# This only happens if your stream isn't faster than the
# the USB read timeout, ~1 sec.
print "No data, timeout", datetime.now()
print "".join(i for i in traceback.format_exc())
processedDataList = d.processStreamData(rawDataList)
averageVoltage = sum(processedDataList)/len(processedDataList)
stop = datetime.now()
print '\n' , "Stream stopped at", datetime.now(), '\n'
sampleTotal = packetCount * d.streamSamplesPerPacket
scanTotal = sampleTotal / 1 # sampleTotal / NumChannels
print "%s requests with %s packets per request with %s samples per packet = %s samples total." % (
dataCount, (float(packetCount) / dataCount), d.streamSamplesPerPacket, sampleTotal)
print "%s samples were lost due to errors." % missed
print "%f percent of total samples were lost" % (float(missed) * 100 / sampleTotal)
sampleTotal -= missed
print "Adjusted number of samples = %s" % sampleTotal
runTime = (stop - start).seconds + float((stop - start).microseconds) / 1000000
print "The experiment took %s seconds." % runTime
print "Scan Rate : %s scans / %s seconds = %s Hz" % (scanTotal, runTime, float(scanTotal) / runTime)
print "Sample Rate : %s samples / %s seconds = %s Hz" % (sampleTotal, runTime, float(sampleTotal) / runTime)
print "The average voltage was %s volts over the scan period" % (averageVoltage)
Thank you in advance for your time.