Oscilloskope utility – using Ethernet


In [6]:
import matplotlib.pyplot as plt
import sys
import os
import time
import h5py
import numpy as np
import glob
import vxi11

# Step 0:
# Connect oscilloscope via direct Ethernet link
# Step 1:
# Run "Record" on the oscilloscope
# and wait for 508 frames to be acquired.
# Step 2:
# Run this cell to initialize grabbing.


# This will need a rewrite
class TmcDriver:

    def __init__(self, device):
        print("Initializing connection to: " + device)
        self.device = device
        self.instr = vxi11.Instrument(device)
 
    def write(self, command):
        self.instr.write(command);

    def read(self, length = 500):
        return self.instr.read(length)

    def read_raw(self, length = 500):
        return self.instr.read_raw(length)
 
    def getName(self):
        self.write("*IDN?")
        return self.read(300)
    
    def ask(self, command):
        return self.instr.ask(command)
 
    def sendReset(self):
        self.write("*RST")  # Be carefull, this real resets an oscilloscope
        
# Default oscilloscope record timeout [s]
loop_sleep_time = 60
        
# For Ethernet
#osc = TmcDriver("TCPIP::147.231.24.72::INSTR")
osc = TmcDriver("TCPIP::10.1.1.254::INSTR")
print(osc.ask("*IDN?"))


Initializing connection to: TCPIP::10.1.1.254::INSTR
RIGOL TECHNOLOGIES,DS6104,DS6A192700022,00.02.00.SP5

Read repeatedly records from oscilloscope


In [7]:
filename = 1
    
if (filename == 1):
    for f in glob.iglob("./data/*.h5"): # delete all .h5 files 
        print 'Deleting', f
        os.remove(f)
else:
    print 'Not removing old files, as filename {0} is not 1.'.format(filename)


osc.write(':STOP') # start recording
time.sleep(0.5)
    
while True:
    #print('  Enter to continue.')
    #raw_input() Wait for key press

    osc.write(':FUNC:WREC:OPER REC') # start recording
    run_start_time = time.time()
    print '  Capturing...'
    time.sleep(0.5)
    
    while True:
        osc.write(':FUNC:WREC:OPER?') # finish recording?
        reply = osc.read()
        if reply == 'STOP':
            run_time = round(time.time() - run_start_time, 2)
            print('  Subrun finished, capturing for %.2f seconds.' % run_time)
            break
        time.sleep(0.01)

    osc.write(':WAV:SOUR CHAN1')
    osc.write(':WAV:MODE NORM')
    osc.write(':WAV:FORM BYTE')
    osc.write(':WAV:POIN 1400')

    osc.write(':WAV:XINC?')
    xinc = float(osc.read(100))
    print 'XINC:', xinc,
    osc.write(':WAV:YINC?')
    yinc = float(osc.read(100))
    print 'YINC:', yinc,
    osc.write(':TRIGger:EDGe:LEVel?')
    trig = float(osc.read(100))
    print 'TRIG:', trig,
    osc.write(':WAVeform:YORigin?')
    yorig = float(osc.read(100))
    print 'YORIGIN:', yorig,
    osc.write(':WAVeform:XORigin?')
    xorig = float(osc.read(100))
    print 'XORIGIN:', xorig,
    

    osc.write(':FUNC:WREP:FEND?') # get number of last frame
    frames = int(osc.read(100))
    print 'FRAMES:', frames, 'SUBRUN', filename
    
    with h5py.File('./data/data'+'{:02.0f}'.format(filename)+'_'+str(int(round(time.time(),0)))+'.h5', 'w') as hf:       
        hf.create_dataset('FRAMES', data=(frames)) # write number of frames
        hf.create_dataset('XINC', data=(xinc)) # write axis parameters
        hf.create_dataset('YINC', data=(yinc))
        hf.create_dataset('TRIG', data=(trig))
        hf.create_dataset('YORIGIN', data=(yorig))
        hf.create_dataset('XORIGIN', data=(xorig))
        hf.create_dataset('CAPTURING', data=(run_time))
        osc.write(':FUNC:WREP:FCUR 1') # skip to n-th frame
        time.sleep(0.5)
        for n in range(1,frames+1):
            osc.write(':FUNC:WREP:FCUR ' + str(n)) # skip to n-th frame
            time.sleep(0.001)

            osc.write(':WAV:DATA?') # read data
            #time.sleep(0.4)
            wave1 = bytearray(osc.read_raw(500))
            wave2 = bytearray(osc.read_raw(500))
            wave3 = bytearray(osc.read_raw(500))
            #wave4 = bytearray(osc.read(500))
            #wave = np.concatenate((wave1[11:],wave2[:(500-489)],wave3[:(700-489)]))
            wave = np.concatenate((wave1[11:],wave2,wave3[:-1]))
            hf.create_dataset(str(n), data=wave)
    filename = filename + 1


  Capturing...
  Subrun finished, capturing for 591.87 seconds.
XINC: 2.5e-08 YINC: 0.0015625 TRIG: 0.00516 YORIGIN: -119.0 XORIGIN: -4.199998e-06 FRAMES: 1999 SUBRUN 1
  Capturing...
  Subrun finished, capturing for 348.42 seconds.
XINC: 2.5e-08 YINC: 0.0015625 TRIG: 0.00516 YORIGIN: -119.0 XORIGIN: -4.199998e-06 FRAMES: 1999 SUBRUN 2
  Capturing...
  Subrun finished, capturing for 341.41 seconds.
XINC: 2.5e-08 YINC: 0.0015625 TRIG: 0.00516 YORIGIN: -119.0 XORIGIN: -4.199998e-06 FRAMES: 1999 SUBRUN 3
  Capturing...
  Subrun finished, capturing for 451.53 seconds.
XINC: 2.5e-08 YINC: 0.0015625 TRIG: 0.00516 YORIGIN: -119.0 XORIGIN: -4.199998e-06 FRAMES: 1999 SUBRUN 4
  Capturing...
  Subrun finished, capturing for 389.34 seconds.
XINC: 2.5e-08 YINC: 0.0015625 TRIG: 0.00516 YORIGIN: -119.0 XORIGIN: -4.199998e-06 FRAMES: 1999 SUBRUN 5
  Capturing...
  Subrun finished, capturing for 374.74 seconds.
XINC: 2.5e-08 YINC: 0.0015625 TRIG: 0.00516 YORIGIN: -119.0 XORIGIN: -4.199998e-06 FRAMES: 1999 SUBRUN 6
  Capturing...
  Subrun finished, capturing for 336.54 seconds.
XINC: 2.5e-08 YINC: 0.0015625 TRIG: 0.00516 YORIGIN: -119.0 XORIGIN: -4.199998e-06 FRAMES: 1999 SUBRUN 7
  Capturing...
  Subrun finished, capturing for 298.20 seconds.
XINC: 2.5e-08 YINC: 0.0015625 TRIG: 0.00516 YORIGIN: -119.0 XORIGIN: -4.199998e-06 FRAMES: 1999 SUBRUN 8
  Capturing...
  Subrun finished, capturing for 316.99 seconds.
XINC: 2.5e-08 YINC: 0.0015625 TRIG: 0.00516 YORIGIN: -119.0 XORIGIN: -4.199998e-06 FRAMES: 1999 SUBRUN 9
  Capturing...
  Subrun finished, capturing for 292.04 seconds.
XINC: 2.5e-08 YINC: 0.0015625 TRIG: 0.00516 YORIGIN: -119.0 XORIGIN: -4.199998e-06 FRAMES: 1999 SUBRUN 10
  Capturing...
  Subrun finished, capturing for 286.81 seconds.
XINC: 2.5e-08 YINC: 0.0015625 TRIG: 0.00516 YORIGIN: -119.0 XORIGIN: -4.199998e-06 FRAMES: 1999 SUBRUN 11
  Capturing...
  Subrun finished, capturing for 285.96 seconds.
XINC: 2.5e-08 YINC: 0.0015625 TRIG: 0.00516 YORIGIN: -119.0 XORIGIN: -4.199998e-06 FRAMES: 1999 SUBRUN 12
  Capturing...
  Subrun finished, capturing for 265.95 seconds.
XINC: 2.5e-08 YINC: 0.0015625 TRIG: 0.00516 YORIGIN: -119.0 XORIGIN: -4.199998e-06 FRAMES: 1999 SUBRUN 13
  Capturing...
  Subrun finished, capturing for 241.87 seconds.
XINC: 2.5e-08 YINC: 0.0015625 TRIG: 0.00516 YORIGIN: -119.0 XORIGIN: -4.199998e-06 FRAMES: 1999 SUBRUN 14
  Capturing...
  Subrun finished, capturing for 229.23 seconds.
XINC: 2.5e-08 YINC: 0.0015625 TRIG: 0.00516 YORIGIN: -119.0 XORIGIN: -4.199998e-06 FRAMES: 1999 SUBRUN 15
  Capturing...
  Subrun finished, capturing for 239.67 seconds.
XINC: 2.5e-08 YINC: 0.0015625 TRIG: 0.00516 YORIGIN: -119.0 XORIGIN: -4.199998e-06 FRAMES: 1999 SUBRUN 16
  Capturing...
  Subrun finished, capturing for 232.44 seconds.
XINC: 2.5e-08 YINC: 0.0015625 TRIG: 0.00516 YORIGIN: -119.0 XORIGIN: -4.199998e-06 FRAMES: 1999 SUBRUN 17
  Capturing...
  Subrun finished, capturing for 230.19 seconds.
XINC: 2.5e-08 YINC: 0.0015625 TRIG: 0.00516 YORIGIN: -119.0 XORIGIN: -4.199998e-06 FRAMES: 1999 SUBRUN 18
  Capturing...
  Subrun finished, capturing for 218.72 seconds.
XINC: 2.5e-08 YINC: 0.0015625 TRIG: 0.00516 YORIGIN: -119.0 XORIGIN: -4.199998e-06 FRAMES: 1999 SUBRUN 19
  Capturing...
  Subrun finished, capturing for 221.03 seconds.
XINC: 2.5e-08 YINC: 0.0015625 TRIG: 0.00516 YORIGIN: -119.0 XORIGIN: -4.199998e-06 FRAMES: 1999 SUBRUN 20
  Capturing...
  Subrun finished, capturing for 210.50 seconds.
XINC: 2.5e-08 YINC: 0.0015625 TRIG: 0.00516 YORIGIN: -119.0 XORIGIN: -4.199998e-06 FRAMES: 1999 SUBRUN 21
  Capturing...
  Subrun finished, capturing for 217.59 seconds.
XINC: 2.5e-08 YINC: 0.0015625 TRIG: 0.00516 YORIGIN: -119.0 XORIGIN: -4.199998e-06 FRAMES: 1999 SUBRUN 22
  Capturing...
  Subrun finished, capturing for 226.18 seconds.
XINC: 2.5e-08 YINC: 0.0015625 TRIG: 0.00516 YORIGIN: -119.0 XORIGIN: -4.199998e-06 FRAMES: 1999 SUBRUN 23
  Capturing...
  Subrun finished, capturing for 223.24 seconds.
XINC: 2.5e-08 YINC: 0.0015625 TRIG: 0.00516 YORIGIN: -119.0 XORIGIN: -4.199998e-06 FRAMES: 1999 SUBRUN 24
  Capturing...
  Subrun finished, capturing for 230.08 seconds.
XINC: 2.5e-08 YINC: 0.0015625 TRIG: 0.00516 YORIGIN: -119.0 XORIGIN: -4.199998e-06 FRAMES: 1999 SUBRUN 25
  Capturing...
  Subrun finished, capturing for 239.83 seconds.
XINC: 2.5e-08 YINC: 0.0015625 TRIG: 0.00516 YORIGIN: -119.0 XORIGIN: -4.199998e-06 FRAMES: 1999 SUBRUN 26
  Capturing...
  Subrun finished, capturing for 290.55 seconds.
XINC: 2.5e-08 YINC: 0.0015625 TRIG: 0.00516 YORIGIN: -119.0 XORIGIN: -4.199998e-06 FRAMES: 1999 SUBRUN 27
  Capturing...
  Subrun finished, capturing for 323.94 seconds.
XINC: 2.5e-08 YINC: 0.0015625 TRIG: 0.00516 YORIGIN: -119.0 XORIGIN: -4.199998e-06 FRAMES: 1999 SUBRUN 28
  Capturing...
  Subrun finished, capturing for 381.20 seconds.
XINC: 2.5e-08 YINC: 0.0015625 TRIG: 0.00516 YORIGIN: -119.0 XORIGIN: -4.199998e-06 FRAMES: 1999 SUBRUN 29
  Capturing...
  Subrun finished, capturing for 576.43 seconds.
XINC: 2.5e-08 YINC: 0.0015625 TRIG: 0.00516 YORIGIN: -119.0 XORIGIN: -4.199998e-06 FRAMES: 1999 SUBRUN 30
  Capturing...
  Subrun finished, capturing for 252.35 seconds.
XINC: 2.5e-08 YINC: 0.0015625 TRIG: 0.00516 YORIGIN: -119.0 XORIGIN: -4.199998e-06 FRAMES: 870 SUBRUN 31
Exception socket.error: (22, 'Invalid argument') in <bound method Instrument.__del__ of <vxi11.vxi11.Instrument object at 0x7ff22805ddd0>> ignored
---------------------------------------------------------------------------
KeyboardInterrupt                         Traceback (most recent call last)
<ipython-input-7-0744ab7ec4a7> in <module>()
     67         time.sleep(0.5)
     68         for n in range(1,frames+1):
---> 69             osc.write(':FUNC:WREP:FCUR ' + str(n)) # skip to n-th frame
     70             time.sleep(0.001)
     71 

<ipython-input-6-676b3daf194f> in write(self, command)
     26 
     27     def write(self, command):
---> 28         self.instr.write(command);
     29 
     30     def read(self, length = 500):

/usr/local/lib/python2.7/dist-packages/python_vxi11-0.9-py2.7.egg/vxi11/vxi11.pyc in write(self, message, encoding)
    745             return
    746 
--> 747         self.write_raw(str(message).encode(encoding))
    748 
    749     def read(self, num=-1, encoding = 'utf-8'):

/usr/local/lib/python2.7/dist-packages/python_vxi11-0.9-py2.7.egg/vxi11/vxi11.pyc in write_raw(self, data)
    677                 self._lock_timeout_ms,
    678                 flags,
--> 679                 block
    680             )
    681 

/usr/local/lib/python2.7/dist-packages/python_vxi11-0.9-py2.7.egg/vxi11/vxi11.pyc in device_write(self, link, timeout, lock_timeout, flags, data)
    409         return self.make_call(DEVICE_WRITE, params,
    410                 self.packer.pack_device_write_parms,
--> 411                 self.unpacker.unpack_device_write_resp)
    412 
    413     def device_read(self, link, request_size, timeout, lock_timeout, flags, term_char):

/usr/local/lib/python2.7/dist-packages/python_vxi11-0.9-py2.7.egg/vxi11/rpc.pyc in make_call(self, proc, args, pack_func, unpack_func)
    176         if pack_func:
    177             pack_func(args)
--> 178         self.do_call()
    179         if unpack_func:
    180             result = unpack_func()

/usr/local/lib/python2.7/dist-packages/python_vxi11-0.9-py2.7.egg/vxi11/rpc.pyc in do_call(self)
    267         sendrecord(self.sock, call)
    268         while True:
--> 269             reply = recvrecord(self.sock)
    270             u = self.unpacker
    271             u.reset(reply)

/usr/local/lib/python2.7/dist-packages/python_vxi11-0.9-py2.7.egg/vxi11/rpc.pyc in recvrecord(sock)
    244     last = 0
    245     while not last:
--> 246         last, frag = recvfrag(sock)
    247         record.extend(frag)
    248     return bytes(record)

/usr/local/lib/python2.7/dist-packages/python_vxi11-0.9-py2.7.egg/vxi11/rpc.pyc in recvfrag(sock)
    227 
    228 def recvfrag(sock):
--> 229     header = sock.recv(4)
    230     if len(header) < 4:
    231         raise EOFError

KeyboardInterrupt: 

Read repeatedly records from oscilloscope

This should be run after the initialization step. Timeout at the end should be enlarged if not all 508 frames are transferred.


In [18]:
filename = 1
run_start_time = time.time()
    
if (filename == 1):
    for f in glob.iglob("./data/*.h5"): # delete all .h5 files 
        print 'Deleting', f
        os.remove(f)
else:
    print 'Not removing old files, as filename {0} is not 1.'.format(filename)

while True:
    osc.write(':WAV:SOUR CHAN1')
    osc.write(':WAV:MODE NORM')
    osc.write(':WAV:FORM BYTE')
    osc.write(':WAV:POIN 1400')

    osc.write(':WAV:XINC?')
    xinc = float(osc.read(100))
    print 'XINC:', xinc,
    osc.write(':WAV:YINC?')
    yinc = float(osc.read(100))
    print 'YINC:', yinc,
    osc.write(':TRIGger:EDGe:LEVel?')
    trig = float(osc.read(100))
    print 'TRIG:', trig,
    osc.write(':WAVeform:YORigin?')
    yorig = float(osc.read(100))
    print 'YORIGIN:', yorig,
    osc.write(':WAVeform:XORigin?')
    xorig = float(osc.read(100))
    print 'XORIGIN:', xorig,
    

    osc.write(':FUNC:WREP:FEND?') # get number of last frame
    frames = int(osc.read(100))
    print 'FRAMES:', frames, 'SUBRUN', filename
    
    # This is not good if the scaling is different and frames are for example just 254
    # if (frames < 508):
    #    loop_sleep_time += 10

    with h5py.File('./data/data'+'{:02.0f}'.format(filename)+'.h5', 'w') as hf:       
        hf.create_dataset('FRAMES', data=(frames)) # write number of frames
        hf.create_dataset('XINC', data=(xinc)) # write axis parameters
        hf.create_dataset('YINC', data=(yinc))
        hf.create_dataset('TRIG', data=(trig))
        hf.create_dataset('YORIGIN', data=(yorig))
        hf.create_dataset('XORIGIN', data=(xorig))
        osc.write(':FUNC:WREP:FCUR 1') # skip to n-th frame
        time.sleep(0.5)
        for n in range(1,frames+1):
            osc.write(':FUNC:WREP:FCUR ' + str(n)) # skip to n-th frame
            time.sleep(0.001)

            osc.write(':WAV:DATA?') # read data
            #time.sleep(0.4)
            wave1 = bytearray(osc.read_raw(500))
            wave2 = bytearray(osc.read_raw(500))
            wave3 = bytearray(osc.read_raw(500))
            #wave4 = bytearray(osc.read(500))
            #wave = np.concatenate((wave1[11:],wave2[:(500-489)],wave3[:(700-489)]))
            wave = np.concatenate((wave1[11:],wave2,wave3[:-1]))
            hf.create_dataset(str(n), data=wave)
    filename = filename + 1
    osc.write(':FUNC:WREC:OPER REC') # start recording
    #print('  Subrun finished, sleeping for %.0f seconds.' % loop_sleep_time)
    run_start_time = time.time()
    #time.sleep(loop_sleep_time) # delay for capturing
    
    print('  Subrun finished, Enter to continue.')
    #raw_input()
    time.sleep(100) # delay for capturing
    #print(' We were waiting for ', time.time() - run_start_time())


Deleting ./data/data01.h5
XINC: 3e-08 YINC: 0.0008 TRIG: 0.00597 YORIGIN: -73.0 XORIGIN: -3.000001e-06 FRAMES: 1016 SUBRUN 1
  Subrun finished, Enter to continue.
XINC: 3e-08 YINC: 0.0008 TRIG: 0.00597 YORIGIN: -73.0 XORIGIN: -3.000001e-06 FRAMES: 1016 SUBRUN 2
  Subrun finished, Enter to continue.
XINC: 3e-08 YINC: 0.0008 TRIG: 0.00597 YORIGIN: -73.0 XORIGIN: -3.000001e-06 FRAMES: 1016 SUBRUN 3
  Subrun finished, Enter to continue.
XINC: 3e-08 YINC: 0.0008 TRIG: 0.00597 YORIGIN: -73.0 XORIGIN: -3.000001e-06 FRAMES: 1016 SUBRUN 4
  Subrun finished, Enter to continue.
XINC: 3e-08 YINC: 0.0008 TRIG: 0.00597 YORIGIN: -73.0 XORIGIN: -3.000001e-06 FRAMES: 1016 SUBRUN 5
  Subrun finished, Enter to continue.
XINC: 3e-08 YINC: 0.0008 TRIG: 0.00597 YORIGIN: -73.0 XORIGIN: -3.000001e-06 FRAMES: 1016 SUBRUN 6
  Subrun finished, Enter to continue.
XINC: 3e-08 YINC: 0.0008 TRIG: 0.00597 YORIGIN: -73.0 XORIGIN: -3.000001e-06 FRAMES: 1016 SUBRUN 7
  Subrun finished, Enter to continue.
XINC: 3e-08 YINC: 0.0008 TRIG: 0.00597 YORIGIN: -73.0 XORIGIN: -3.000001e-06 FRAMES: 1016 SUBRUN 8
  Subrun finished, Enter to continue.
XINC: 3e-08 YINC: 0.0008 TRIG: 0.00597 YORIGIN: -73.0 XORIGIN: -3.000001e-06 FRAMES: 1016 SUBRUN 9
  Subrun finished, Enter to continue.
XINC: 3e-08 YINC: 0.0008 TRIG: 0.00597 YORIGIN: -73.0 XORIGIN: -3.000001e-06 FRAMES: 1016 SUBRUN 10
  Subrun finished, Enter to continue.
XINC: 3e-08 YINC: 0.0008 TRIG: 0.00597 YORIGIN: -73.0 XORIGIN: -3.000001e-06 FRAMES: 1016 SUBRUN 11
  Subrun finished, Enter to continue.
---------------------------------------------------------------------------
KeyboardInterrupt                         Traceback (most recent call last)
<ipython-input-18-8b524669744f> in <module>()
     70     print('  Subrun finished, Enter to continue.')
     71     #raw_input()
---> 72     time.sleep(100) # delay for capturing
     73     #print(' We were waiting for ', time.time() - run_start_time())
     74 

KeyboardInterrupt: 

Stopwatch for timing the first loop


In [ ]:
first_run_start_time = time.time()
raw_input()
loop_sleep_time = time.time() - first_run_start_time + 15
print loop_sleep_time

In [9]:
loop_sleep_time=60

In [ ]: