bancada-motores/PZ4000/Programas/PZ4000.py
Pedro Jose Romero Gombau 3d8f36d8e0 Diseño 3D
2025-05-22 14:56:46 +02:00

79 lines
2.4 KiB
Python

import pyvisa
import matplotlib.pyplot as plt
# Initialize VISA resource manager
rm = pyvisa.ResourceManager()
analyzer_address = None
print(rm.list_resources())
rm.list_resources()
# Automatically detect the analyzer
for device in rm.list_resources():
try:
inst = rm.open_resource(device)
idn_response = inst.query("*IDN?")
if "PZ4000" in idn_response: # Adjust based on actual model identifier
analyzer_address = device
break
inst.close()
except Exception:
pass
# Proceed if analyzer found
if analyzer_address:
analyzer = rm.open_resource(analyzer_address)
# Set waveform data format and byte order
analyzer.write(":WAVEFORM:FORMAT FLOAT")
analyzer.write(":WAVEFORM:BYTEORDER MSB")
# Query sample rate to calculate the actual time duration of each block
sample_rate = float(analyzer.query(":WAVEFORM:SRATE?"))
block_size = 50 # Number of points per block
update_interval = block_size / sample_rate # Calculate update time based on sample rate
start = 0
end = start + block_size
# Set up plotting
plt.ion()
fig, ax = plt.subplots()
time_data = []
wave_data = []
try:
while True:
# Configure the waveform start and end points for the sliding window
analyzer.write(f":WAVEFORM:START {start}")
analyzer.write(f":WAVEFORM:END {end}")
# Retrieve waveform data
raw_data = analyzer.query_binary_values(":WAVEFORM:SEND?", datatype='f', container=list)
# Update time and waveform data for plotting
time_data = [i / sample_rate for i in range(len(raw_data))]
wave_data = raw_data
# Update plot
ax.clear()
ax.plot(time_data, wave_data, label="Instantaneous Value")
ax.set_title("Instantaneous Waveform")
ax.set_xlabel("Time (s)")
ax.set_ylabel("Amplitude")
ax.legend()
plt.draw()
plt.pause(update_interval) # Use the calculated interval to sync with the data capture time
# Increment the start and end points for the next block (sliding window)
start += block_size
end += block_size
except KeyboardInterrupt:
print("Measurement stopped.")
finally:
analyzer.close()
plt.ioff()
plt.show()
else:
print("Analyzer not found.")