Skip to content

Commit 8163b21

Browse files
committed
Add file transfer example for AFG31k
Added example for file transfer for the AFG31k and updated readme to match.
1 parent 28a4034 commit 8163b21

File tree

2 files changed

+119
-0
lines changed

2 files changed

+119
-0
lines changed

Instrument_Examples/AFG31000/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,5 @@ Example for 2 channel double pulse on the AFG31k using remote commands. Creates
1212
* **[Sending and Saving Arbitrary Waveforms](./Send_and_Save_Arb_Waveform_Example.py/)**
1313
Creates a sine waveform, sends it to the AFG31000's edit memory 1, and saves the waveform as a .tfwx file to the AFG31k's internal memory.
1414

15+
* **[Transfering Waveforms to the AFG31k](./Transfer_Waveform_File.py/)**
16+
Transfer an AFG31k waveform file (.tfwx) over a remote communication to an AFG31k (requires AFG31k firmware 1.6.5 or above). Note waveform file to transfer must be in the same directory as this Python script.
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
"""
2+
3+
***********************************************************
4+
*** Copyright 2025 Tektronix, Inc. ***
5+
*** See www.tek.com/sample-license for licensing terms. ***
6+
***********************************************************
7+
8+
Transfer_Waveform_File.py
9+
10+
Transfer an AFG31k waveform file (.tfwx) over a remote communication to an AFG31k (requires AFG31k firmware 1.6.5 or above). Note waveform file to transfer must be in the same directory as this Python script.
11+
"""
12+
13+
import pyvisa
14+
import sys
15+
from pathlib import Path
16+
17+
def read_waveform_file(file_path):
18+
"""
19+
read_waveform_file reads the waveform file contents that is specified at file_path
20+
21+
:param file_path: file path of waveform file
22+
:return: contents of waveform file
23+
"""
24+
try:
25+
with open(file_path, 'rb') as file: # read waveform file
26+
return file.read()
27+
except FileNotFoundError: # error if waveform file not found
28+
print(f"Error: File not found at {file_path}")
29+
sys.exit(1)
30+
except Exception as e: # error if issue reading waveform file
31+
print(f"Error reading the waveform file: {e}")
32+
sys.exit(1)
33+
34+
35+
def transfer_waveform_file(instrument, filename, file_contents):
36+
"""
37+
transfer_waveform_file transfers a waveform file (.tfwx) to an AFG31k
38+
39+
:param instrument: PyVISA resource object
40+
:param filename: filename of waveform file
41+
:param file_contents: contents of waveform file
42+
:return: none
43+
"""
44+
45+
chunk_size = 1048575 # size of each binary data chunk in bytes (max allowed is 1048575 bytes)
46+
total_size = len(file_contents) # get number of bytes in file
47+
num_chunks = (total_size // chunk_size) + (1 if total_size % chunk_size != 0 else 0) # get number of chunks required to send entire file
48+
command = f"DATA:FILE \"{filename}\",{total_size}," # SCPI command to transfer file
49+
50+
# send wavefrom file
51+
for i in range(num_chunks):
52+
53+
# get binary chunk from file contents
54+
start = i * chunk_size
55+
end = start + chunk_size
56+
chunk = file_contents[start:end]
57+
58+
print(f"Sending chunk {i+1}/{num_chunks}, size: {len(chunk)} bytes")
59+
instrument.write_binary_values(command, chunk, datatype='b') # transfer waveform
60+
61+
if(i == 0 or i ==1): # check for errors with DATA:FILE command
62+
error = instrument.query("SYST:ERROR?")
63+
if(error[0] != "0"):
64+
raise Exception(f"DATA:FILE command error, {error}")
65+
66+
# check if waveform file was saved succesfully
67+
savedFiles = instrument.query("MMEMory:CATalog?")
68+
if f"{filename}" not in savedFiles:
69+
print(f"All Saved Files:\n{savedFiles}")
70+
raise Exception("Waveform file transfer failed")
71+
72+
print("File sent successfully.")
73+
74+
75+
def main():
76+
try:
77+
# configure and connect to an AFG31k
78+
rm = pyvisa.ResourceManager()
79+
instrument_address = "" # instrument resource string
80+
instrument = None # instrument connection session
81+
instrument = rm.open_resource(instrument_address, write_termination='\n')
82+
instrument.timeout = 25000
83+
if "SOCKET" in instrument_address:
84+
instrument.write_termination = "\n"
85+
instrument.read_termination = "\n"
86+
instrument.send_end = True
87+
88+
instrument.write("*RST") # reset AFG to default state
89+
instrument.write("*CLS") # clear error queue
90+
91+
filename = "example.tfwx" # name waveform file to send to AFG31k
92+
waveform_file_path = str(Path(__file__).parent) + "\\" + filename # complete file path to waveform file (waveform file must be in same directroy as Python script)
93+
waveform_file_contents = read_waveform_file(waveform_file_path) # get file contents
94+
transfer_waveform_file(instrument, filename, waveform_file_contents) # transfer waveform file
95+
96+
except pyvisa.VisaIOError as e: # errors for instrument communcation
97+
print(f"VISA communication error: {e}")
98+
except Exception as e:
99+
print(f"An error occurred: {e}")
100+
finally:
101+
if(instrument != None): # check if connection was ever successfully established
102+
try:
103+
# print errors from error queue
104+
errors = instrument.query("SYST:ERROR?")
105+
while(errors[0] != "0"):
106+
print(errors)
107+
errors = instrument.query("SYST:ERROR?")
108+
109+
instrument.clear() # clear connection
110+
except Exception as e:
111+
print(f"An error occurred: {e}")
112+
finally:
113+
instrument.close() # close VISA session
114+
rm.close() # close resource manager session
115+
116+
if __name__ == "__main__":
117+
main()

0 commit comments

Comments
 (0)