Commit 5cc95244 authored by Peter Jansweijer's avatar Peter Jansweijer

made pyhton 2 and 3 compatible

parent 633a7db6
......@@ -22,7 +22,7 @@ along with this program. If not, see <http://www.gnu.org/licenses/>
-------------------------------------------------------------------------------
Usage:
Keysight_DSO_S_254A.py <IP#> [-c 1234] [-a<averages>] [-o <dir>] [-m <number>]
Keysight_DSO_S_254A.py <IP#> [-c 1234] [-a<averages>] [-o <dir>] [-m <measurments>]
Keysight_DSO_S_254A.py -h | --help
IP IP number of the Oscilloscope
......@@ -44,10 +44,12 @@ import scipy
import struct
import datetime
#TJP: installed from web python-vxi Alex
#TJP: installed from web python-vxi Alex
import vxi11
import matplotlib.pyplot as plt
plt.rcParams['lines.linewidth'] = 1
############################################################################
......@@ -67,8 +69,8 @@ def get_waveforms(scope, channels=[1,2,3,4], num_avg=1, output_dir="data"):
scope -- instance of python-vxi connected to the oscilloscope
channels -- channels that are going to be measured for example '1,2'
num_avg -- the number of averiges taken by the oscilloscope
output_dir -- name of the directory where measured waveform files wil be stored
num_avg -- the number of averages taken by the oscilloscope
output_dir -- name of the directory where measured waveform files will be stored
the file output format is as described below:
......@@ -118,8 +120,17 @@ def get_waveforms(scope, channels=[1,2,3,4], num_avg=1, output_dir="data"):
# scope.write(":TRIGGER:SWEEP SINGLE")
# scope.write(":RUN")
# 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()
timestamp = datetime.datetime.now() #micro secounds timing
timestamp = datetime.datetime.now() #micro seconds timing
#filename=output_dir+time.strftime(format("%y%m%d_%H_%M_%S"),timestamp)+"_scope_keysight_dso_s_254A_bin"
filename = output_dir+timestamp.strftime("%y%m%dT%H:%M:%S.%f")+"_scope_keysight_dso_s_254A_bin"
print("save waveform into file:",filename)
file_header = "#WaveformData:Keysight DSO-S 254A\n"
file_header += "#version:0.2\n"
......@@ -139,34 +150,67 @@ def get_waveforms(scope, channels=[1,2,3,4], num_avg=1, output_dir="data"):
channel_preamble = []
channel_data = []
for chan in channels:
scope.write(":WAVeform:SOURce CHANnel"+str(chan))
channel_preamble.append("#preamble:\n")
channel_preamble.append(scope.ask(":WAVeform:PREamble?")+"\n")
channel_data.append("#waveform:\n")
channel_data.append(scope.ask_raw(":WAVeform:DATA?")+"\n")
# Write out file_header, followed by all preambles, followed by all channel_data
data = [file_header]
for i in range(len(channel_preamble)):
data.append(channel_preamble[i])
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)+"_scope_keysight_dso_s_254A_bin"
filename = output_dir+timestamp.strftime("%y%m%dT%H:%M:%S.%f")+"_scope_keysight_dso_s_254A_bin"
print("save waveform into file:",filename)
file=open(filename,"w")
for i in data:
file.write(i)
file.close()
# 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:
scope.write(":WAVeform:SOURce CHANnel"+str(chan))
ch_preamble_bytes = (b'#preamble:\n')
scope.write((":WAVeform:PREamble?")+"\n")
ch_preamble_bytes += scope.read_raw()
ch_preamble_bytes += (b'#preamble_end:\n')
channel_preamble.append(ch_preamble_bytes)
ch_data_bytes = (b'#waveform:\n')
scope.write((":WAVeform:DATA?")+"\n")
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_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:
scope.write(":WAVeform:SOURce CHANnel"+str(chan))
channel_preamble.append("#preamble:\n")
channel_preamble.append(scope.ask(":WAVeform:PREamble?")+"\n")
channel_data.append("#waveform:\n")
channel_data.append(scope.ask_raw(":WAVeform:DATA?")+"\n")
# Write out file_header, followed by all preambles, followed by all channel_data
data = [file_header]
for i in range(len(channel_preamble)):
data.append(channel_preamble[i])
for i in range(len(channel_data)):
data.append(channel_data[i])
# Python 2 write string
file=open(filename,"w")
for i in data:
file.write(i)
file.close()
return data, filename
############################################################################
......@@ -297,7 +341,7 @@ def check_waveforms(waveform_data):
"""
This function checks for the consistency of the captured waveform.
waveform_data -- <type 'dict'> waveform_data (as returned by funtion "file_to_waveform")
waveform_data -- <type 'dict'> waveform_data (as returned by function "file_to_waveform")
returns: number of points (of the first waveform found)
"""
......@@ -309,7 +353,7 @@ def check_waveforms(waveform_data):
first = False
channel = ch
points = waveform_data[ch]["preamble"]["points"]
print("Info: Record Length is", points,"sampels")
print("Info: Record Length is", points,"samples")
count = waveform_data[ch]["preamble"]["count"]
x_inc = waveform_data[ch]["preamble"]["x_increment"]
print("Info: Sample Period is", x_inc)
......@@ -327,7 +371,7 @@ def check_waveforms(waveform_data):
print("### WARNING! Different time base sample interval!")
print(" Channel:",channel,x_inc)
print(" Channel:",ch,waveform_data[ch]["preamble"]["x_increment"])
print(" You may want to check the 'Interploation' setting in the 'pre-Processing' tab of the oscilloscopes channel setup")
print(" You may want to check the 'Interpolation' setting in the 'pre-Processing' tab of the oscilloscopes channel setup")
if waveform_data[ch]["preamble"]["x_display_range"] != timebase:
print("### WARNING! Different time base!")
print(" Channel:",channel,timebase)
......@@ -571,6 +615,7 @@ if __name__ == "__main__":
# forwarded to function average_edge_to_sfd
scope.write(":TIMebase:DELay 0")
for i in range(0, args.measurements):
d,filename = get_waveforms(scope, channels=args.channels, num_avg=args.num_avg, output_dir=args.output_dir)
wf_data = file_to_waveform(filename)
......
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