diff --git a/PicoVNA106.m b/PicoVNA106.m new file mode 100644 index 0000000..9f65205 --- /dev/null +++ b/PicoVNA106.m @@ -0,0 +1,152 @@ +classdef PicoVNA106 + properties + v; + end + + methods + function obj = PicoVNA106() + obj.v = tcpclient('localhost', 5025); + obj.v.Timeout = 10; %default + end + + function sweep(obj) + obj.sendCommand('INIT'); + readline(obj.v); + end + + function p = getPowerLevel(obj) + % Outputs power in dBm + obj.clearBuffer(); + pstr = obj.sendCommand('SENS:LEV?'); + splt = strsplit(pstr, ' '); + p = str2double(splt{1}); + end + + function setPowerLevel(obj, p) + % In dBm + obj.sendCommand(['SENS:LEV ' num2str(p)]); + obj.clearBuffer(); + end + + function setContinuousSweep(obj) + obj.sendCommand('INIT:CONT:ALL'); + end + + function [S11, S12, S21, S22] = getSParams(obj) + % Function does not initiate a sweep, it only gathers the data + % in the buffer. + obj.clearBuffer(); + obj.sendCommand('CALC:DATA S11,POLAR'); + S11temp = readbinblock(obj.v, 'double'); + S11 = S11temp(1:2:end) + 1i*S11temp(2:2:end); + + obj.sendCommand('CALC:DATA S21,POLAR'); + S21temp = readbinblock(obj.v, 'double'); + S21 = S21temp(1:2:end) + 1i*S21temp(2:2:end); + + obj.sendCommand('CALC:DATA S12,POLAR'); + S12temp = readbinblock(obj.v, 'double'); + S12 = S12temp(1:2:end) + 1i*S12temp(2:2:end); + + obj.sendCommand('CALC:DATA S22,POLAR'); + S22temp = readbinblock(obj.v, 'double'); + S22 = S22temp(1:2:end) + 1i*S22temp(2:2:end); + end + + function f = getFreqAxis(obj) + obj.clearBuffer() + freqStart = obj.sendCommand('SENS:FREQ:STAR?'); + freqStart = obj.freqStrConvert(freqStart); + freqStop = obj.sendCommand('SENS:FREQ:STOP?'); + freqStop = obj.freqStrConvert(freqStop); + numPoints = obj.sendCommand('SENS:SWE:POIN?'); + f = linspace(freqStart, freqStop, numPoints); + end + + function S = readToSparamsObject(obj) + w = warning('off', 'all'); + S = SPARAMS(); + w = warning('on', 'all'); + [S.S11, S.S12, S.S21, S.S22] = obj.getSParams(); + S.S11 = S.S11'; S.S12 = S.S12'; S.S21 = S.S21'; S.S22 = S.S22'; + S.f = obj.getFreqAxis(); + S.f = S.f'; + S.setZ0(50); + S.setNumPorts(2); + end + + function clearBuffer(obj) + w = warning('off', 'all'); + obj.v.Timeout = 0.2; + resp = 1; + while ~isempty(resp) + try + resp = readline(obj.v); + catch + disp('No response from VNA when clearing buffer'); + end + end + obj.v.Timeout = 10; + w = warning('on', 'all'); + end + + function freq = freqStrConvert(obj, freqStr) + freqStr = lower(freqStr); + strSplt = split(freqStr, ' '); + switch lower(char(strSplt(2))) + case 'khz' + scale = 1e3; + case 'mhz' + scale = 1e6; + case 'ghz' + scale = 1e9; + end + freq = str2double(strSplt{1})*scale; + end + + function setLocal(obj) + obj.sendCommand('SYST:LOC'); + end + + function retVal = sendCommand(obj, command) + if contains(command, '?') == 1 + writeline(obj.v, command); + retVal = readline(obj.v); + else + writeline(obj.v, command); + retVal = 0; + end + end + + function reset(obj) + obj.sendCommand('*RST'); + end + + function selfTest(obj) + retStr = obj.sendCommand('*TST?'); + try ret = str2double(retStr); + catch + error('Non-numerical response') + end + if(ret == 0) + disp(['No errors found in self-test, code:' num2str(ret)]); + else + disp(['Error code: ' (ret)]); + end + end + + function id = getID(obj) + id = obj.sendCommand('*IDN?'); + end + + function bool = deInit(obj) + if(isvalid(obj.v) == 1) + clear obj.v; + bool = 1; + else + bool = 0; + end + + end + end +end \ No newline at end of file