2017-04-03 82 views
0

我有一个问题...我已经尝试了一些方法,但它没有工作。我必须做一个实时的数据采集,并将它们绘制在一个界面中......如果您可以建议我采取一种方法来做到这一点......下面的程序在变量“数据”(矩阵)中进行一次数据采集,但我有不断地做,并在同一时间绘制它们......谢谢!Python实时绘图

# Print library info: 
print_library_info() 

# Search for devices: 
libtiepie.device_list.update() 

# Try to open an oscilloscope with block measurement support: 
scp = None 
for item in libtiepie.device_list: 
    if item.can_open(libtiepie.DEVICETYPE_OSCILLOSCOPE): 
     scp = item.open_oscilloscope() 
     if scp.measure_modes & libtiepie.MM_BLOCK: 
      break 
     else: 
      scp = None 

if scp: 
    try: 
     fig = plt.figure() 
     ax = fig.add_subplot(111) 
     k=0 
     while k<20: 
      # Set measure mode: 
      scp.measure_mode = libtiepie.MM_BLOCK 

      # Set sample frequency: 
      scp.sample_frequency = 5e6 # 1 MHz 

      # Set record length: 
      scp.record_length = 1000 # 15000 samples 

      # Set pre sample ratio: 
      scp.pre_sample_ratio = 0 # 0 % 

      # For all channels: 
      for ch in scp.channels: 
       # Enable channel to measure it: 
       ch.enabled = True 

       # Set range: 
       ch.range = 8 # 8 V 

       # Set coupling: 
       ch.coupling = libtiepie.CK_ACV # DC Volt 

      # Set trigger timeout: 
      scp.trigger_time_out = 100e-3 # 100 ms 

      # Disable all channel trigger sources: 
      for ch in scp.channels: 
       ch.trigger.enabled = False 

      # Setup channel trigger: 
      ch = scp.channels[0] # Ch 1 

      # Enable trigger source: 
      ch.trigger.enabled = True 

      # Kind: 
      ch.trigger.kind = libtiepie.TK_RISINGEDGE # Rising edge 

      # Level: 
      ch.trigger.levels[0] = 0.5 # 50 % 

      # Hysteresis: 
      ch.trigger.hystereses[0] = 0.05 # 5 % 

      # Print oscilloscope info: 
      #print_device_info(scp) 

      # Start measurement: 
      scp.start() 

      # Wait for measurement to complete: 
      while not scp.is_data_ready: 
       time.sleep(0.01) # 10 ms delay, to save CPU time 

      # Get data: 
      data = scp.get_data() 




    except Exception as e: 
     print('Exception: ' + e.message) 
     sys.exit(1) 

    # Close oscilloscope: 
    del scp 

else: 
    print('No oscilloscope available with block measurement support!') 
    sys.exit(1) 

回答

0

什么像这样(我以为你是谋害自己的资料):

import joystick as jk 
import time 

class test(jk.Joystick): 
    _infinite_loop = jk.deco_infinite_loop() 
    _callit = jk.deco_callit() 

    @_callit('before', 'init') 
    def _init_data(self, *args, **kwargs): 
     self.t = np.array([]) 
     self.data = np.array([]) 
     # do the hardware initialization here 
     #............. 
     self.range = 8 
     self.record_length = 1000 

    @_callit('after', 'init') 
    def _build_frames(self, *args, **kwargs): 
     self.mygraph = self.add_frame(
        Graph(name="Graph", size=(500, 500), 
          pos=(50, 50), fmt="go-", xnpts=self.record_length, 
          freq_up=10, bgcol="w", xylim=(0,10,0,self.range))) 

    @_callit('before', 'start') 
    def _set_t0(self): 
     # initialize t0 at start-up 
     self._t0 = time.time() 

    @_infinite_loop(wait_time=0.2) 
    def _get_data(self): 
     self.t = self.mygraph.add_datapoint(self.t, time.time()) 
     # new data acquisition here 
     new_data = scp.get_data() 
     self.data = self.graph.add_datapoint(self.data, new_data) 
     self.mygraph.set_xydata(self.t-self._t0, self.data) 

,并开始读取/绘图:

t = test() 
t.start()