Hepsko

code after fixes

May 23rd, 2022 (edited)
285
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.69 KB | None | 0 0
  1. from pynput import keyboard
  2. import u6
  3. from time import sleep
  4. import matplotlib.pyplot as plt
  5.  
  6.  
  7. d = u6.U6()  # initialize the interface; assumes a single U6 is plugged in to a USB port
  8. d.configU6()  # set default configuration
  9. d.configIO()  # ask for analog inputs
  10.  
  11. d.streamConfig(
  12.     NumChannels=3,
  13.     ChannelOptions=[7, 0, 0],
  14.     ChannelNumbers=[0, 1, 2],
  15.     ResolutionIndex=0,
  16.     ScanFrequency=1000)
  17.  
  18. d.packetsPerRequest = 1  # you can adjust this value to get more or less data
  19.  
  20. #set DAC0 to 4.0 voltage
  21. DAC0 = 5000
  22. d.writeRegister(DAC0, 4.0)
  23. break_program = False
  24. x = []
  25. y = []
  26. def show_result(i, ain0):
  27.     print("Iteration: " + str(i))
  28.     print("AIN0(Diff): " + str(round( ain0, 3)))
  29.     print("")
  30.  
  31.  
  32. def on_press(key):
  33.     global break_program
  34.     if key == keyboard.Key.esc:
  35.         break_program = True
  36.         return False
  37.  
  38.  
  39. # Try to measure a data set.
  40. def measure():
  41.     try:
  42.         d.streamStart()
  43.         for r in d.streamData():
  44.             if r is not None:
  45.                 if r['errors'] or r['numPackets'] != d.packetsPerRequest or r['missed']:
  46.                     print("error")
  47.                 break
  48.     finally:
  49.         d.streamStop()
  50.     return r
  51.  
  52.  
  53. # Write a set of data to a file "data.txt"
  54. def writeData(r, i):
  55.     print(d.getAIN(2))
  56.     result = d.getAIN(positiveChannel=0,  resolutionIndex=12, gainIndex=2, settlingFactor=0, differential=True)
  57.     show_result(i, result)
  58.     f2.write(str(result) + '\t\n')
  59.     f2.close()
  60.     f2.close()
  61.  
  62.  
  63.  
  64.  
  65. with keyboard.Listener(on_press=on_press) as listener:
  66.     iterator = 0
  67.     while not break_program:
  68.         writeData(measure(), iterator)
  69.         iterator = iterator + 1
  70.     listener.join()
  71.  
Add Comment
Please, Sign In to add comment