From 4a374e022dd35f59e34bd6cb0e5192c55b384b00 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20K=C3=A4berich?= Date: Wed, 15 Sep 2021 18:20:44 +0200 Subject: [PATCH] additional SCPI example for trace data reading --- .../UserManual/SCPI_Examples/libreVNA.py | 17 +++++++ .../SCPI_Examples/retrieve_trace_data.py | 49 +++++++++++++++++++ 2 files changed, 66 insertions(+) create mode 100644 Documentation/UserManual/SCPI_Examples/retrieve_trace_data.py diff --git a/Documentation/UserManual/SCPI_Examples/libreVNA.py b/Documentation/UserManual/SCPI_Examples/libreVNA.py index 233c3e3..2365b98 100644 --- a/Documentation/UserManual/SCPI_Examples/libreVNA.py +++ b/Documentation/UserManual/SCPI_Examples/libreVNA.py @@ -80,3 +80,20 @@ class libreVNA: self.sock.sendall(query.encode()) self.sock.send(b"\n") return self.__read_response() + + @staticmethod + def parse_trace_data(data): + ret = [] + # Remove brackets (order of data implicitly known) + data = data.replace(']','').replace('[','') + values = data.split(',') + if int(len(values) / 3) * 3 != len(values): + # number of values must be a multiple of three (frequency, real, imaginary) + raise Exception("Invalid input data: expected tuples of three values each") + for i in range(0, len(values), 3): + freq = float(values[i]) + real = float(values[i+1]) + imag = float(values[i+2]) + ret.append((freq, complex(real, imag))) + return ret + diff --git a/Documentation/UserManual/SCPI_Examples/retrieve_trace_data.py b/Documentation/UserManual/SCPI_Examples/retrieve_trace_data.py new file mode 100644 index 0000000..dd043a9 --- /dev/null +++ b/Documentation/UserManual/SCPI_Examples/retrieve_trace_data.py @@ -0,0 +1,49 @@ +#!/usr/bin/env python3 + +import time +from libreVNA import libreVNA + +# Create the control instance +vna = libreVNA('localhost', 19542) + +# Quick connection check (should print "LibreVNA-GUI") +print(vna.query("*IDN?")) + +# Make sure we are connecting to a device (just to be sure, with default settings the LibreVNA-GUI auto-connects) +vna.cmd(":DEV:CONN") +dev = vna.query(":DEV:CONN?") +if dev == "Not connected": + print("Not connected to any device, aborting") + exit(-1) +else: + print("Connected to "+dev) + +# Simple trace data extraction + +# switch to VNA mode, setup the sweep parameters +print("Setting up the sweep...") +vna.cmd(":DEV:MODE VNA") +vna.cmd(":VNA:SWEEP FREQUENCY") +vna.cmd(":VNA:STIM:LVL -10") +vna.cmd(":VNA:ACQ:IFBW 100") +vna.cmd(":VNA:ACQ:AVG 1") +vna.cmd(":VNA:ACQ:POINTS 501") +vna.cmd(":VNA:FREQuency:START 2000000000") +vna.cmd(":VNA:FREQuency:STOP 3500000000") + +# wait for the sweep to finish +print("Waiting for the sweep to finish...") +while vna.query(":VNA:ACQ:FIN?") == "FALSE": + time.sleep(0.1) + +# grab the data of trace S11 +print("Reading trace data...") +data = vna.query(":VNA:TRACE:DATA? S11") + +# Returned data is just a string containing all the measurement points. +# Parsing the data returns a list containing frequency/complex tuples +S11 = vna.parse_trace_data(data) + +for x in S11: + print(x) +