-
Notifications
You must be signed in to change notification settings - Fork 3
/
UploadArb.py
executable file
·125 lines (92 loc) · 3.47 KB
/
UploadArb.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#!/usr/bin/env python
import visa
from time import sleep
import argparse
import struct
import numpy as np
import csv
import os
#parse arguments
parser = argparse.ArgumentParser(description='Upload an arbitrary waveform to an Agilent 33600 AWG')
parser.add_argument('-f','--filename', help='File containing arbitrary waveform', default="./test.dat", required=True)
parser.add_argument('-a','--address', help="Address of device", default="142.104.60.122", required=False)
parser.add_argument('-v','--pulseheight', help="Pulse height of arb", default="0.1", required=False)
parser.add_argument('-m','--macro', help="Generate a macro for loading this arb", action='store_true', required=False)
parser.add_argument('-d','--delimiter', help="Input file delimiter", default=" ", required=False)
args = parser.parse_args()
#can't pass '\t' in at the command line
if args.delimiter=="tab":
args.delimiter="\t"
#remove file extension
name=os.path.splitext(os.path.basename(args.filename))[0]
#if the arb name is longer than 12, truncate it (maximum length allowed by SCPI interface)
if len(name) > 12:
name=name[:12]
print("Arb name truncated to "+name)
samplePeriod=0
num=0
tlast=-1
#load the arbitrary waveform
arb=[]
with open(args.filename,'r') as f:
reader=csv.reader(f,delimiter=args.delimiter)
for t,p in reader:
arb.append(float(p))
if tlast != -1:
samplePeriod=samplePeriod+(float(t)-float(tlast)) #get difference between subsequent time bins
num=num+1
tlast=t
#get sample rate
samplePeriod=samplePeriod/num
sRate=str(1/samplePeriod)
#scale signal between 1 and -1
sig = np.asarray(arb, dtype='f4')/max(arb)
#load the VISA resource manager
rm = visa.ResourceManager('@py')
#connect to the device
inst = rm.open_resource("TCPIP::"+args.address+"::INSTR")
print(inst.query("*IDN?"))
#sent start control message
message="Uploading\nArbitrary\nWaveform"
inst.write("DISP:TEXT '"+message+"'")
#create a directory on device (will generate error if the directory exists, but we can ignore that)
inst.write("MMEMORY:MDIR \"INT:\\remoteAdded\"")
#set byte order
inst.write('FORM:BORD SWAP')
#clear volatile memory
inst.write('SOUR1:DATA:VOL:CLE')
#write arb to device
inst.write_binary_values('SOUR1:DATA:ARB '+name+',', sig, datatype='f', is_big_endian=False)
#wait until that command is done
inst.write('*WAI')
#name the arb
inst.write('SOUR1:FUNC:ARB '+name)
#set sample rate, voltage
inst.write('SOUR1:FUNC:ARB:SRAT ' + sRate)
inst.write('SOUR1:VOLT:OFFS 0')
inst.write('SOUR1:FUNC ARB')
inst.write('SOUR1:VOLT '+args.pulseheight)
#save arb to device internal memory
inst.write('MMEM:STOR:DATA "INT:\\remoteAdded\\'+name+'.arb"')
#clear message
inst.write("DISP:TEXT ''")
#check for error messages
instrument_err = "error"
while instrument_err != '+0,"No error"\n':
inst.write('SYST:ERR?')
instrument_err = inst.read()
if instrument_err[:4] == "-257": #directory exists message, don't display
continue;
if instrument_err[:2] == "+0": #no error
continue;
print(instrument_err)
#close device
inst.close()
#generate a macro to load this arb using AgilentControl.py
if args.macro:
macroFile = "load_"+name+".awg"
with open(macroFile, 'w') as f:
f.write("# Macro generated by UploadArb.py \n")
f.write("MMEMORY:LOAD:DATA1 \"INT:\\remoteAdded\\"+name+".arb\"\n")
f.write("SOURCE1:FUNCTION ARB\n")
f.write("SOURCE1:FUNCtion:ARBitrary \"INT:\\remoteAdded\\"+name+".arb\"\n")