Commit bb9ad97f authored by Peter Jansweijer's avatar Peter Jansweijer

rewritten for Python3 (as well as for Python 2)

parent 67c9acf6
......@@ -22,27 +22,26 @@ along with this program. If not, see <http://www.gnu.org/licenses/>
-------------------------------------------------------------------------------
Usage:
LeCroy8254.py <IP#> [-c=<1,2,3,4>] [-a=<averages>] [-o=<dir>]
LeCroy8254.py <IP#> [-c <1,2,3,4>] [-a <averages>] [-o <dir>]
LeCroy8254.py -h | --help
IP IP number of the Oscilloscope
(for example: 192.168.32.243 which is its DevNet IP number)
Options:
-c <1,2,3,4> channel number
-a <number> number of averages [default: 1]
-o <dir> optional directory for output file storage [default: "data/"]
-h --help Show this screen.
--version Show version.
-c=<1,2,3,4> channel number
-a=<number> number of averages [default: 1]
-o=<dir> optional directory for output file storage [default: "data/"]
"""
from docopt import docopt
import os
import sys
import time
import scipy
import struct
import pdb
#TJP: installed from web python-vxi Alex
import vxi11
......@@ -54,8 +53,10 @@ import matplotlib.pyplot as plt
def get_waveforms(scope, channels=[1,2,3,4],num_avg=1,output_dir="data"):
"""
!!! For python 3.x !!!
Measure and save LeCroy WaveRunner 8254M-MS waveforms
scope -- instance of python-vxi connected to LeCroy8254 oscilloscope
channels -- channels that are going to be measured for example '1,2'
num_avg -- the number of averiges taken by the oscilloscope before
......@@ -129,7 +130,15 @@ def get_waveforms(scope, channels=[1,2,3,4],num_avg=1,output_dir="data"):
#print(cnt)
#cnt = cnt +1
# add trailing slash if not present
output_dir = os.path.join(output_dir,'')
if os.path.exists(output_dir) != True:
os.mkdir(output_dir)
print("Output directory does not exist => created: "+output_dir)
timestamp = time.localtime()
filename = output_dir+time.strftime(format("%y%m%d_%H_%M_%S"),timestamp)+"_LeCroy8254_bin"
print("save LeCroy8254 Waveform into file:",filename)
file_header = "#WaveformData:LeCroy8254\n"
file_header += "#version:0.2\n"
......@@ -142,16 +151,70 @@ def get_waveforms(scope, channels=[1,2,3,4],num_avg=1,output_dir="data"):
channel_descriptor = []
channel_data = []
# Detect which python version is used:
python_version = sys.version_info[0]
# Python 3.x uses type <bytes> instead of <str> (Pyhton 2.x)
# scope.read() returns <str>
# scope.read_raw() returns <bytes>
# returned data = <bytes>
if python_version == 3:
for chan in channels.split(','):
ch_preamble_bytes = (b'#preamble:\n')
scope.write((src_str+str(chan)+":INSPECT? WAVEDESC")+"\n")
ch_preamble_bytes += scope.read_raw()
ch_preamble_bytes += (b'#preamble_end:\n')
channel_preamble.append(ch_preamble_bytes)
ch_descriptor_bytes = (b'#waveform_desc:\n')
scope.write(src_str+str(chan)+":WAVEFORM? DESC")
ch_descriptor_bytes += scope.read_raw()
channel_descriptor.append(ch_descriptor_bytes)
ch_data_bytes = (b'#waveform_data:\n')
scope.write(src_str+str(chan)+":WAVEFORM? DAT1")
ch_data_bytes += scope.read_raw()
channel_data.append(ch_data_bytes)
# Python 3 write bytes
file=open(filename,"wb")
# Write out file_header, followed by all preambles,
# followed by all descriptors, followed by all channel_data
file.write(file_header.encode()) # encode() <str> into <bytes>
data = file_header.encode()
for i in channel_preamble:
data += i
file.write(i)
for i in channel_descriptor:
data += i
file.write(i)
for i in channel_data:
data += i
file.write(i)
file.close()
# Python 2.x uses type <str>
# scope.read() returns <unicode>
# scope.read_raw() returns <str>
# returned data = <list> with items type <str>
elif python_version == 2:
for chan in channels.split(','):
channel_preamble.append("#preamble:\n")
channel_preamble.append(scope.ask(src_str+str(chan)+":INSPECT? WAVEDESC")+"\n")
scope.write((src_str+str(chan)+":INSPECT? WAVEDESC")+"\n")
channel_preamble.append(scope.read())
channel_preamble.append("#preamble_end:\n")
channel_descriptor.append("#waveform_desc:\n")
channel_descriptor.append(scope.ask_raw(src_str+str(chan)+":WAVEFORM? DESC"))
scope.write(src_str+str(chan)+":WAVEFORM? DESC")
channel_descriptor.append(scope.read_raw())
channel_data.append("#waveform_data:\n")
channel_data.append(scope.ask_raw(src_str+str(chan)+":WAVEFORM? DAT1"))
scope.write(src_str+str(chan)+":WAVEFORM? DAT1")
channel_data.append(scope.read_raw())
# Write out file_header, followed by all preambles, followed by all channel_data
# Write out file_header, followed by all preambles,
# followed by all descriptors, followed by all channel_data
data = [file_header]
for i in range(len(channel_preamble)):
data.append(channel_preamble[i])
......@@ -160,19 +223,12 @@ def get_waveforms(scope, channels=[1,2,3,4],num_avg=1,output_dir="data"):
for i in range(len(channel_data)):
data.append(channel_data[i])
# add trailing slash if not present
output_dir = os.path.join(output_dir,'')
if os.path.exists(output_dir) != True:
os.mkdir(output_dir)
print("Output directory does not exist => created: "+output_dir)
filename = output_dir+time.strftime(format("%y%m%d_%H_%M_%S"),timestamp)+"_LeCroy8254_bin"
print("save LeCroy8254 Waveform into file:",filename)
# Python 2 write string
file=open(filename,"w")
for i in data:
file.write(i)
file.close()
return data, filename
############################################################################
......@@ -700,35 +756,37 @@ def osc_init(scope, time_base = 50.0e-9):
##
## If run from commandline, we can test the library
##
"""
Usage:
LeCroy8254.py <IP#> [-c <1,2,3,4>] [-a <averages>] [-o <dir>]
LeCroy8254.py -h | --help
IP IP number of the instrument [default 192.168.32.243]
Options:
-c <1,2,3,4> channel number
-a <number> number of averages [default: 1]
-o <dir> optional directory for output file storage [default: "data/"]
-h --help Show this screen.
"""
if __name__ == "__main__":
arguments = docopt(__doc__,version='LeCroy WaveRunner 8254M-MS version 01')
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("IP", type=str, help="IP number of the instrument", default="192.168.32.243")
parser.add_argument("-c", type=str, help="channel number", default='1,2')
parser.add_argument("-a", type=int, help="number of averages", default=1)
parser.add_argument("-o", type=str, help="optional directory for output file storag", default="data/")
args = parser.parse_args()
if len(sys.argv) >= 2: # just IP number
scope = vxi11.Instrument(sys.argv[1])
#scope = vxi11.Instrument("192.168.32.243")
IP = args.IP
channels = args.c
num_avg = args.a
output_dir = args.o
scope = vxi11.Instrument(IP)
print(scope.ask("*IDN?"))
# Returns 'LECROY,WAVERUNNER8254M-MS,LCRY4253N20398,8.1.0'
# Note that for LeCroy oscilloscopes LXI needs to be enabled (with proper
# IP address configured). See menu "Utilities" -> "Utilities Setup" -> "Remote" tab
channels = '1'
#record_len = 1000
num_avg = 1
output_dir = "data"
if len(sys.argv) >= 3: # There are more arguments...
for i in range(1,len(sys.argv)):
option = sys.argv[i].split('=')
if option[0] == '-c': # set channels
channels=option[1]
#print("channels:",channels)
#if arg_value[:2] == '-l': # set record length
# record_len=int(arg_value[2:])
# print(record_len)
if option[0] == '-a': # set number of averages
num_avg=int(option[1])
if option[0] == '-o': # set output directory
output_dir=option[1]
print ("channels:",channels, "num_avg", num_avg)
......@@ -761,4 +819,4 @@ if __name__ == "__main__":
plt.show()
sys.exit()
sys.exit()
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment