commit 9599810278d70770c2cbef36431effdfa40ae286 Author: LongHairedHacker Date: Tue Jan 21 21:43:51 2020 +0100 Initial commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..355d8ef --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +.virtenv +__pycache__ diff --git a/ssa3032tmc.py b/ssa3032tmc.py new file mode 100644 index 0000000..f3773c5 --- /dev/null +++ b/ssa3032tmc.py @@ -0,0 +1,47 @@ +#!/bin/env python3 + +import usbtmc + +READ_CHUNK_SIZE = 256 + +class SSA3023(object): + def __init__(self): + self.instr = usbtmc.Instrument(0xf4ec, 0x1300) + #TODO: Check if ID number matches + #id_str = self.instr.ask("*IDN?") + + self.instr.write(":FORMat:TRACe:DATA ASCii") + + def get_start_freq(self): + return float(self.instr.ask(":SENSe:FREQuency:STARt?")) + + def get_stop_freq(self): + return float(self.instr.ask(":SENSe:FREQuency:STOP?")) + + def get_ref_level(self): + return float(self.instr.ask(":DISPlay:WINDow:TRACe:Y:SCALe:RLEVel?")) + + def get_norm_ref_level(self): + return float(self.instr.ask(":DISPlay:WINDow:TRACe:Y:NRLevel?")) + + def is_displayed_trace(self, num): + if num < 1 or num > 4: + raise ValueError("%d is not a valid trace %d", num) + res = self.instr.ask(":TRACe%d:MODE?" % num) + + return res != "BLANk" + + def get_trace(self, num): + if num < 1 or num > 4: + raise ValueError("%d is not a valid trace %d", num) + + self.instr.write(":TRACe:DATA? %d" % num) + data = b'' + while not data.endswith(b'\n'): + chunk = self.instr.read_raw(num=READ_CHUNK_SIZE) + data = data + chunk + + data = data.decode("utf-8").split(',')[:-1] + data = map(lambda x: float(x), data) + + return list(data) diff --git a/trace-grab.py b/trace-grab.py new file mode 100644 index 0000000..3227ac7 --- /dev/null +++ b/trace-grab.py @@ -0,0 +1,47 @@ +#!/usr/bin/env python3 + +import usbtmc + +import numpy as np +import matplotlib.pyplot as plt +from matplotlib.ticker import EngFormatter + +from ssa3032tmc import SSA3023 + +def main(): + + ssa = SSA3023() + + start_freq = ssa.get_start_freq() + stop_freq = ssa.get_stop_freq() + norm_level = ssa.get_norm_ref_level() + + fig = plt.figure() + ax = fig.add_subplot(111) + formatterHz = EngFormatter(unit='Hz') + ax.xaxis.set_major_formatter(formatterHz) + + ax.set_xlim(start_freq, stop_freq) + ax.grid() + + min_level = norm_level + + for i in [1,2,3,4]: + if ssa.is_displayed_trace(i): + samples = np.array(ssa.get_trace(i)) + min_level = min(np.min(samples), min_level) + freqs = np.linspace(start_freq, stop_freq, len(samples)) + ax.plot(freqs, samples, label="Trace %d" % i) + + ax.set_ylim(min_level - 10, norm_level) + ax.legend() + plt.show() + + + + + plt.show() + + +if __name__ == '__main__': + main() diff --git a/trace-live.py b/trace-live.py new file mode 100644 index 0000000..3c7392b --- /dev/null +++ b/trace-live.py @@ -0,0 +1,47 @@ +#!/usr/bin/env python3 + +import usbtmc + +import numpy as np +import matplotlib.pyplot as plt +from matplotlib.ticker import EngFormatter + +from ssa3032tmc import SSA3023 + +def main(): + + ssa = SSA3023() + + fig = plt.figure() + ax = fig.add_subplot(111) + + + while True: + ax.clear() + start_freq = ssa.get_start_freq() + stop_freq = ssa.get_stop_freq() + norm_level = ssa.get_norm_ref_level() + + formatterHz = EngFormatter(unit='Hz') + ax.xaxis.set_major_formatter(formatterHz) + + ax.set_xlim(start_freq, stop_freq) + ax.grid() + + min_level = norm_level + + for i in [1,2,3,4]: + if ssa.is_displayed_trace(i): + samples = np.array(ssa.get_trace(i)) + min_level = min(np.min(samples), min_level) + freqs = np.linspace(start_freq, stop_freq, len(samples)) + ax.plot(freqs, samples, label="Trace %d" % i) + + ax.set_ylim(min_level - 10, norm_level) + ax.legend() + + plt.pause(0.250) + + +if __name__ == '__main__': + main()