import logging import threading import time from datetime import datetime import u6 import traceback # Event e signals between threads e = threading.Event() # Max number of requests to be read MAX_REQUESTS = 1 # Initialize U6 and configure data stream d = u6.U6() d.getCalibrationData() d.streamConfig( NumChannels = 1, ChannelNumbers = [0], ChannelOptions = [0], SettlingFactor = 1, ResolutionIndex = 1, ScanFrequency = 100 ) # Set DAC0 to 1.5V DAC0_REG = 5000 d.writeRegister(DAC0_REG, 1.5) time.sleep(1) def wait_then_change_dac(e): # when event e is set, wait 500ms then change DAC value for 100ms e.wait() time.sleep(0.5) d.writeRegister(DAC0_REG, 3.5) print "------- Attempted to change DAC --------" time.sleep(0.1) d.writeRegister(DAC0_REG, 1.5) # Start data logging and try toggling DAC0 dac = threading.Thread(target=wait_then_change_dac, args=(e,)) dac.start() try: d.streamStart() e.set() print "e is set" dataCount = 0 for r in d.streamData(): if r is not None: if dataCount >= MAX_REQUESTS: break if r['errors'] != 0: print "Errors" if r['numPackets'] != d.packetsPerRequest: print "Underflow" if r['missed'] != 0: print "Missed" for i in r['AIN0']: print i dataCount += 1 else: print "No data" except: print "".join(i for i in traceback.format_exc()) finally: d.streamStop() print "Stream stopped" d.close()