
%% Basic commands for controling the Valon synthesizer from matlab

% Software Requirements:
% 1) Matlab with intrument tool box
% 2) National instruments visa (or other visa driver supported by matlab)

%% Identify Synthesiser visa ID
% Run this section

input('Disconnect Synthesizer, enter to continue');
vhwinfo = instrhwinfo('visa','ni');
char(vhwinfo.AvailableSerialPorts)

% ans =
%   3×6 char array
%     'ASRL1 '
%     'ASRL4 '
%     'ASRL10'

input('Connect Synthesizer, enter to continue');
vhwinfo = instrhwinfo('visa','ni');
char(vhwinfo.AvailableSerialPorts)

% ans =
%   4×6 char array
%     'ASRL1 '
%     'ASRL4 '
%     'ASRL6 '
%     'ASRL10'

%% Manualy create visa ID string. 
% Create id from new entry in list above and run this section
% On this system the new visa resorce is ASRL6.

% Configuration notes:
%
% 1) Set terminator to '>cr'. The sysnthesizer return string has several
% cr's and returns '>cr' at the end. Setting the terminator to '>rc
% ensures that the entire input buffer is read.
%
% 2) BaudRate is 115200
%
% 3) Set the timeout to ~1 sec. The systhesizer seems to take a while to
% respond sometimes.

id = 'ASRL6::INSTR';
synth_fp = visa('ni',id);
synth_fp.Terminator = {double('>') 'CR'}; 
synth_fp.BaudRate = 9600;   
synth_fp.Timeout = 2;
fopen(synth_fp);
% if synth_fp.Status == 'closed'
%     disp(
rtn = query(synth_fp,'ID')

% rtn =
% 
%     'ID
%      Valon Technology, 5009, 12201631, version 1.0   Build: Jul  4 2019  13:11:04
%      
%      -2->'

%% Execute commands and parse return strings if desired
query(synth_fp,'F 1000')

% ans =
% 
%     'F 1000
%      F 1000 MHz; // Act 1000 MHz
%      
%      -2->'

%% Close the synthesizer file pointer
fclose(synth_fp);
