Commits

Anonymous committed 677166d

recv function now accepts size, added a method to enable fastframe, commented code cleanup

  • Participants
  • Parent commits 9f84ec0

Comments (0)

Files changed (1)

 
         self.sock.sendall(msg)
 
-    def recv(self):
+    def clear(self):
+        self.send('*cls')
+
+    def recv(self, size=None):
         buffer = ''
 
-        while True:
-            buffer += self.sock.recv(1024)
+        if size is None:
+            while True:
+                buffer += self.sock.recv(4096)
 
-            if buffer.endswith('\n'):
-                break
+                if buffer.endswith('\n'):
+                    break
+        else:
+            while len(buffer) < size:
+                buffer += self.sock.recv(min(size-len(buffer),4096))
 
         return buffer
 
     def query(self, msg):
         """Sends a query to the oscilloscope and returns the response."""
-        if not msg.endswith('\n'):
-            msg += '\n'
+        self.send(msg)
+        return self.recv().strip()
 
-        self.send(msg)
-
-        return self.recv().strip()
+    def enable_fastframe(self, count):
+        self.send('horizontal:fastframe:state 1')
+        self.send('horizontal:fastframe:count %i' % count)
 
     def set_sequence_mode(self):
         """Sets the oscilloscope in single acquisition mode."""
 
         if dtype is None:
             dtype = get_dtype(self.get_preamble(channel))
-            # encoding = self.query('data:encdg?')[:3]
-
-            # if encoding == 'RIB':
-            #     dtype = '>i'
-            # elif encoding == 'RPB':
-            #     dtype = '>u'
-            # elif encoding == 'SRI':
-            #     dtype = '<i'
-            # elif encoding == 'SRP':
-            #     dtype = '<u'
-            # else:
-            #     raise ValueError('unkown encoding: %s' % encoding)
-
-            # dtype += self.query('data:width?').strip()
-
-
 
         self.send('curve?\n')
 
 
         y = int(self.sock.recv(int(x[1])))
 
-        buffer = ''
-
-        while len(buffer) < y:
-            buffer += self.sock.recv(y-len(buffer))
-
-        waveform = np.fromstring(buffer,dtype)
+        waveform = np.fromstring(self.recv(y),dtype)
 
         # messages end with a newline
         eom = self.sock.recv(1024)
 
         if eom != '\n':
-            print 'x = ', x
-            print 'y = ', y
-            print eom
-
-        assert eom == '\n'
+            raise Exception("eom != '\n'")
 
         self.send(header)
 
 
     scope = TekScope(setup.scope_ip, setup.port)
 
+    scope.clear()
+
     root, ext = os.path.splitext(args[0])
 
     if ext == '':
         ext = '.hdf5'
 
-    #print (root, ext)
-
     for run in range(options.nruns):
         if options.nruns > 1:
             fileid = str(run).zfill(int(log10(options.nruns))+1)
 
         filename = root + fileid + ext
 
-        #print filename
-
         t0 = time.time()
 
         with h5py.File(filename, 'w') as f:
 
             active_channels = scope.get_active_channels()
 
-            dtypes = {}
             for channel in active_channels:
                 preamble = scope.get_preamble(channel)
 
-                #dtypes[channel] = get_dtype(preamble)
-
                 dataset = f.create_dataset('channel%i' % channel, (options.nevents, preamble['NR_PT']), dtype=get_dtype(preamble), chunks=(max(1,min(100, options.nevents//100)), preamble['NR_PT']), compression='gzip')
 
                 for key, value in preamble.iteritems():
             fastframe_state = int(scope.query('horizontal:fastframe:state?'))
             fastframe_count = int(scope.query('horizontal:fastframe:count?'))
 
-            #print 'fastframe_state = ', fastframe_state
-
             i = 0
             while i < options.nevents:
                 print '\rsaving event: %i' % (i+1),