···11+function RigolMSO1000Z_ChannelsConfig(serialNum,Channels,VScale,VOffset,ProbeRatio,HScale,HOffset)
22+% Channels settings. Rigol Oscilloscope MSO 1000 Z.
33+%
44+% Examples:
55+% Full config for channels 3 and 4:
66+% RigolMSO1000Z_ChannelsConfig('DS1ZD181600446',[3,4],[1,0.125],[0,0],[10,1],0.02,0);
77+% Only vertical settings for channels 1,3,4:
88+% RigolMSO1000Z_ChannelsConfig('DS1ZD181600446',[1,3,4],[1,0.125,0.5],[0,0,0],[10,1,10]);
99+% Only horizontal setting:
1010+% RigolMSO1000Z_ChannelsConfig('DS1ZD181600446',1,[],[],[],0.02,0);
1111+%
1212+% Input:
1313+% serialNum: Serial number (char or string), e.g. 'DS1ZD181600446'
1414+% Channels: Channel or channels number to be configured. Array with
1515+% one element for each channel, e.g. [1] or [2,3] or [1,2,3,4].
1616+% VScale: Vertical scale for each channel. Array with same length
1717+% as ReadChannels.
1818+% VOffset: Vertical offset for each channel. Array with same length
1919+% as ReadChannels.
2020+% ProbeRatio: Probe ratio for each channel. Array with same length
2121+% as ReadChannels.
2222+% HScale: Horizontal scale. Only one element/value.
2323+% HOffset: Horizontal offset. Only one element/value.
2424+%
2525+% Jose Manuel Requena Plens (2021) [joreple@upv.es]
2626+2727+% Check inputs
2828+arguments
2929+ serialNum (1,1) string
3030+ Channels (1,:) {mustBeInteger,mustBePositive,mustBeMember(Channels,[1,2,3,4])}
3131+ VScale (1,:) = []
3232+ VOffset (1,:) = []
3333+ ProbeRatio (1,:) = []
3434+ HScale {mustBeScalarOrEmpty} = []
3535+ HOffset {mustBeScalarOrEmpty} = []
3636+end
3737+3838+%% Load instrument
3939+devlist = visadevlist(); % Get all VISA resources availables
4040+[~,idxdev] = ismember(serialNum,devlist.SerialNumber); % Search the serial number
4141+deviceNr = devlist.ResourceName{idxdev(1)}; % Get resource name
4242+MSO = visadev(deviceNr); % Create VISA Object
4343+4444+%% Enable channels
4545+% Enable only selected read channels
4646+for c = 1:4
4747+ if any(Channels==c)
4848+ % Enable
4949+ writeline(MSO, sprintf(':CHANnel%d:DISPlay ON',c))
5050+ else
5151+ % Disable
5252+ writeline(MSO, sprintf(':CHANnel%d:DISPlay OFF',c))
5353+ end
5454+end
5555+5656+%% Vertical scale
5757+5858+% Set probe ratio
5959+if ~isempty(ProbeRatio)
6060+ for c = 1:length(Channels)
6161+ % Set ratio to available nearest value
6262+ ratioAvail = [0.01,0.02,0.05,0.1,0.2,0.5,1,2,5,10,20,50,100,200,500,1000];
6363+ [~,idx] = min(abs(ratioAvail-ProbeRatio(c)));
6464+ Pratio(c) = ratioAvail(idx); %#ok<*AGROW>
6565+6666+ % Set
6767+ writeline(MSO, sprintf(':CHANnel%d:PROBe %f',Channels(c),Pratio(c)))
6868+ end
6969+end
7070+7171+% Vertical scale (amplitude)
7272+for c = 1:length(Channels)
7373+7474+ % Vertical scale
7575+ if ~isempty(VScale)
7676+ % Set to available nearest value
7777+ Pratio = str2double(writeread(MSO, sprintf(':CHANnel%d:PROBe?',Channels(c))));
7878+ if Pratio <= 1
7979+ Vavail = repmat([1,2,5],4,1).*[1e-3; 1e-2; 1e-1; 1];
8080+ Vavail = [Vavail(:);10];
8181+ else
8282+ Vavail = repmat([1,2,5],4,1).*[1e-2; 1e-1; 1; 1e1];
8383+ Vavail = [Vavail(:);100];
8484+ end
8585+ [~,idx] = min(abs(Vavail-VScale(c)));
8686+ VSca(c) = Vavail(idx);
8787+8888+ % Set
8989+ writeline(MSO, sprintf(':CHANnel%d:SCALe %f',Channels(c),VSca(c)));
9090+ end
9191+9292+ % Vertical offset
9393+ if ~isempty(VOffset)
9494+ writeline(MSO, sprintf(':CHANnel%d:OFFSet %f',Channels(c),VOffset(c)));
9595+ end
9696+end
9797+9898+%% Horizontal scale (time)
9999+100100+% Horizontal scale
101101+if ~isempty(HScale)
102102+ % Set to available nearest value
103103+ Tavail = repmat([5,10,20],10,1).*[1e-9;1e-8;1e-7;1e-6;1e-5;1e-4;1e-3;1e-2;1e-1;1];
104104+ Tavail = [Tavail(:);50];
105105+ [~,idx] = min(abs(Tavail-HScale));
106106+ HSca = Tavail(idx);
107107+ % Set
108108+ writeline(MSO, sprintf(':TIMebase:MAIN:SCALe %f',HSca));
109109+end
110110+111111+% Horizontal offset
112112+if ~isempty(HOffset)
113113+ % Limits: -Screen/2 to 1s or -Screen/2 to 5000s
114114+ if HOffset<(-6*HSca); HOffset = -6*HSca; end
115115+ writeline(MSO, sprintf(':TIMebase:MAIN:OFFSet %f',HOffset));
116116+end
117117+118118+% Clear VISA Object
119119+clear MSO
···11+function [Data,t,fs] = RigolMSO1000Z_Read(serialNum,Channels,frames)
22+% Read from Rigol Oscilloscope MSO 1000 Z.
33+%
44+% Example: RigolMSO1000Z_Read('DS1ZD181600446',[3,4],2);
55+%
66+% Input:
77+% serialNum: Serial number (char or string), e.g. 'DS1ZD181600446'
88+% Channels: One-dimension array with channel or channels to read: [1],[2,4],...
99+% frames: Number of frames to be read from internal memory.
1010+%
1111+% Output
1212+% Data: Array with as many columns as channels received.
1313+% t: Time array (seconds).
1414+% fs: Sample rate (Sa/s or Hz).
1515+%
1616+% Jose Manuel Requena Plens (2021) [joreple@upv.es]
1717+1818+% Check inputs
1919+arguments
2020+ serialNum (1,1) string
2121+ Channels (1,:) {mustBeInteger,mustBePositive,mustBeMember(Channels,[1,2,3,4])}
2222+ frames {mustBeInteger,mustBeScalarOrEmpty,mustBePositive} = 2
2323+end
2424+2525+%% Load instrument
2626+devlist = visadevlist(); % Get all VISA resources availables
2727+[~,idxdev] = ismember(serialNum,devlist.SerialNumber); % Search the serial number
2828+deviceNr = devlist.ResourceName{idxdev(1)}; % Get resource name
2929+MSO = visadev(deviceNr); % Create VISA Object
3030+3131+%% Signal data format
3232+writeline(MSO, ':WAVeform:MODE RAW' );
3333+writeline(MSO, ':WAVeform:FORMat BYTE')
3434+3535+% Flush data
3636+flush(MSO); pause(0.1) % Wait 100ms
3737+3838+%% Read data
3939+4040+% Data available
4141+% RAW: 250000 (uint8), WORD: 125000 (uint16), ASCII: 15625 (CSV)
4242+frameSize = 250000;
4343+HScale = str2double(writeread(MSO, ':TIMebase:MAIN:SCALe?'));
4444+wavelen = HScale * 12; % Scale*div
4545+sampleRate = str2double(writeread(MSO, ':ACQuire:SRATe?'));
4646+totalSize = wavelen*sampleRate; % Memory Depth = Sample Rate × Waveform Length
4747+totalFrames = min(ceil(totalSize/frameSize),frames);
4848+4949+% Initialize
5050+cellFrames = cell(1,totalFrames);
5151+RAWData = zeros((frameSize-12)*totalFrames,length(Channels));
5252+YScale = zeros(1,length(Channels));
5353+refY = zeros(1,length(Channels));
5454+oriY = zeros(1,length(Channels));
5555+exit = false;
5656+5757+% Read
5858+for c = 1:length(Channels) % Each channel
5959+6060+ % Try attempts
6161+ maxatt = 10;
6262+ for att = 1:maxatt
6363+ try
6464+ % Select channel
6565+ writeline(MSO, sprintf(':WAVeform:SOURce CHANnel%d',Channels(c)) );
6666+ % Flush data
6767+ flush(MSO); pause(0.1) % Wait 100ms
6868+6969+ % Read the waveform data (frames)
7070+ frame = 1;
7171+ for point = 0:frameSize:totalSize
7272+7373+ % If all the frames have been received, the loop ends
7474+ if frame>totalFrames; break; end
7575+7676+ % Initial point for frame N
7777+ writeline(MSO, sprintf(':WAVeform:STARt %d',point+13));
7878+7979+ % Final point for frame N
8080+ if (point+frameSize)<=totalSize
8181+ writeline(MSO, sprintf(':WAVeform:STOP %d',point+frameSize))
8282+ frameSizeAux = frameSize;
8383+ else
8484+ % If almost all the memory has been read, the last
8585+ % points are received
8686+ writeline(MSO, sprintf(':WAVeform:STOP %d',totalSize))
8787+ frameSizeAux = totalSize-point;
8888+ addZeros = frameSize-frameSizeAux;
8989+ exit = true;
9090+ end
9191+9292+ % Receive data
9393+ writeline(MSO, ':WAVeform:DATA?'); % Request data
9494+ data = read(MSO, frameSizeAux, 'uint8'); % Read data
9595+9696+ % Fill with zeros if the last frame
9797+ if exit; data = [data(1:end-1),zeros(1,addZeros+1)]; end
9898+9999+ % Remove Data Header (11) and end newline character (1) and store
100100+ cellFrames{frame} = data(12:end-1); % Store frames
101101+102102+ if exit; break; end
103103+ frame = frame + 1;
104104+ end
105105+106106+ % Store data without Data Header (11) and end newline character (1)
107107+ data = [cellFrames{:}];
108108+ RAWData(:,c) = data(:);
109109+110110+ % Flush data and reload channel
111111+ flush(MSO); pause(0.1) % Wait 100ms
112112+ writeline(MSO, sprintf(':WAVeform:SOURce CHANnel%d',Channels(c)) );
113113+114114+ % Get Y-axis information
115115+ YScale(c) = str2double(writeread(MSO, ':WAVeform:YINCrement?'));
116116+ % Reference and origin values to centering data
117117+ refY(c) = str2double(writeread(MSO, ':WAVeform:YREFerence?'));
118118+ oriY(c) = str2double(writeread(MSO, ':WAVeform:YORigin?'));
119119+120120+ % Reinitialize
121121+ exit = false;
122122+ cellFrames = cell(1,totalFrames);
123123+124124+ % Exit the attempts loop
125125+ break;
126126+127127+ catch
128128+ fprintf('Retrying. Attempt %d with channel %d\r\n',att,Channels(c))
129129+ flush(MSO); pause(0.1) % Wait 100ms
130130+ clear MSO
131131+ MSO = visadev(deviceNr); % Create VISA Object
132132+ if att == maxatt
133133+ YScale(c) = nan;
134134+ refY(c) = nan;
135135+ oriY(c) = nan;
136136+ fprintf('Empty values with channel %d\r\n',Channels(c))
137137+ break;
138138+ end
139139+ end
140140+141141+ end
142142+end
143143+144144+% Flush data
145145+flush(MSO); pause(0.1) % Wait 100ms
146146+147147+% Samplerate
148148+incX = str2double(writeread(MSO, ':WAVeform:XINCrement?')); % delta T (secs)
149149+fs = str2double(writeread(MSO, ':ACQuire:SRATe?')); % Sa/s
150150+151151+% Scale data (convert 0-255 values to Measure Units and centering in x-axis)
152152+Data = (RAWData - (refY+oriY) ).*YScale; % Scale and centering
153153+154154+% Time array (seconds)
155155+t = 0:incX:incX*(size(Data,1)-1);
156156+157157+% Clear VISA Object
158158+clear MSO
···11+function RigolMSO1000Z_WaitForTrigger(serialNum,TriggerChannel,TriggerLevel)
22+% Set trigger of Rigol Oscilloscope MSO 1000 Z.
33+%
44+% Example:
55+% RigolMSO1104Z_WaitForTrigger('DS1ZD181600446',3,-0.02);
66+%
77+% Input:
88+% serialNum: Serial number (char or string), e.g. 'DS1ZD181600446'
99+% TriggerChannel: Channel of trigger source.
1010+% TriggerLevel: Trigger voltage level.
1111+%
1212+% Jose Manuel Requena Plens (2021) [joreple@upv.es]
1313+1414+% Check inputs
1515+arguments
1616+ serialNum (1,1) string
1717+ TriggerChannel (1,1) {mustBeInteger,mustBeMember(TriggerChannel,[1,2,3,4])}
1818+ TriggerLevel {mustBeScalarOrEmpty,mustBeReal} = -0.02
1919+end
2020+2121+%% Load instrument
2222+devlist = visadevlist(); % Get all VISA resources availables
2323+[~,idxdev] = ismember(serialNum,devlist.SerialNumber); % Search the serial number
2424+deviceNr = devlist.ResourceName{idxdev(1)}; % Get resource name
2525+MSO = visadev(deviceNr); % Create VISA Object
2626+2727+% Flush data
2828+flush(MSO); pause(0.1) % Wait 100ms
2929+3030+%% Acquire settings
3131+writeline(MSO, ':ACQuire:MDEPth AUTO' );
3232+writeline(MSO, ':WAVeform:MODE RAW' );
3333+writeline(MSO, ':WAVeform:MODE RAW' );
3434+3535+%% Trigger setup
3636+writeline(MSO, ':TRIGger:MODE EDGE' );
3737+writeline(MSO, ':TRIGger:COUPling DC')
3838+writeline(MSO, ':TRIGger:SWEep SINGle' );
3939+writeline(MSO, ':TRIGger:HOLDoff 0.0000002');
4040+writeline(MSO, ':TRIGger:NREJect 1' );
4141+writeline(MSO, sprintf(':TRIGger:EDGe:SOURce CHANnel%d',TriggerChannel));
4242+writeline(MSO, ':TRIGger:EDGe:SLOPe POSitive' );
4343+4444+% Level
4545+% Limits: (± 5 × VerticalScale from the screen center) - OFFSet
4646+offset = str2double(writeread(MSO, sprintf(':CHANnel%d:OFFSet?',TriggerChannel)));
4747+scale = str2double(writeread(MSO, sprintf(':CHANnel%d:SCALe?',TriggerChannel)));
4848+if TriggerLevel > (5*scale-offset); TriggerLevel = 5*scale-offset; end
4949+if TriggerLevel < (-5*scale-offset); TriggerLevel = -5*scale-offset; end
5050+% Set level
5151+writeline(MSO, sprintf(':TRIGger:EDGe:LEVel %f',TriggerLevel));
5252+5353+% Flush data
5454+flush(MSO); pause(0.1) % Wait 100ms
5555+5656+% Check trigger (if enabled and wait/run, break)
5757+while 1
5858+ writeline(MSO, ':SINGle')
5959+ % Flush data
6060+ flush(MSO); pause(0.2) % Wait 200ms
6161+ status = writeread(MSO, ':TRIGger:STATus?' );
6262+ if strcmp(status,'WAIT') || strcmp(status,'RUN')
6363+ break;
6464+ end
6565+ pause(0.5)
6666+end
6767+6868+% Clear VISA Object
6969+clear MSO
+74
Red Pitaya STEMlab 125-14/RedPitaya_Continuous.m
···11+function RedPitaya_Continuous(IP,port,outCH,type,f,amp)
22+% RED PITAYA STEMlab 125-14 v1.1
33+% Comands: https://redpitaya.readthedocs.io/en/latest/appsFeatures/remoteControl/remoteControl.html#list-of-supported-scpi-commands
44+% Jose Manuel Requena Plens (2021) [joreple@upv.es]
55+66+% Check inputs
77+arguments
88+ IP (1,1) string
99+ port (1,1) double
1010+ outCH (1,1) {mustBeInteger,mustBePositive,mustBeMember(outCH,[1,2])}
1111+ type (1,1) string {mustBeMember(type,{'sine','square','triangle','sawu','sawd','pwm'})}
1212+ f (1,1) mustBePositive
1313+ amp (1,1) {mustBeInRange(amp,-1,1)}
1414+end
1515+1616+%% CHANNEL
1717+% Output channel
1818+SOURout = ['SOUR',num2str(outCH)];
1919+OUTPUT = ['OUTPUT',num2str(outCH)];
2020+2121+%% SIGNAL
2222+% Type
2323+switch lower(type)
2424+ case 'sine'
2525+ SIGNAL = 'SINE';
2626+ case 'square'
2727+ SIGNAL = 'SQUARE';
2828+ case 'triangle'
2929+ SIGNAL = 'TRIANGLE';
3030+ case 'sawu' % sawtooth up
3131+ SIGNAL = 'SAWU';
3232+ case 'sawd' % sawtooth down
3333+ SIGNAL = 'SAWD';
3434+ case 'pwm'
3535+ SIGNAL = 'PWM'; % Duty cycle set with: 'SOURx:DCYC 0.2'
3636+ otherwise
3737+ error('Type signal error. Available: {sine, square, triangle, sawu, sawd, pwm}.')
3838+end
3939+% Frecuency
4040+FREQUENCY = num2str(f);
4141+% Amplitude (limits -1 to 1 Volt)
4242+if amp > 1; amp = 1; end; if amp < -1; amp = -1; end
4343+AMPLITUDE = num2str(amp);
4444+4545+4646+%% CONNECTION
4747+tcpIP = tcpclient(IP, port); % Create connection
4848+configureTerminator(tcpIP,"LF","CR/LF"); % Set terminator for write and read
4949+flush(tcpIP); % Clear write/read buffers
5050+5151+%% RESET HARDWARE
5252+writeline(tcpIP,'GEN:RST'); % Reset generator order
5353+5454+%% GENERATE SIGNAL
5555+% Function of output signal order string 'SOURx:FUNC AAAA'
5656+funct_order = [SOURout,':FUNC ',SIGNAL];
5757+% Frequency of output signal order string 'SOURx:FREQ:FIX xxxx'
5858+f_order = [SOURout,':FREQ:FIX ',FREQUENCY];
5959+% Amplitude of output signal order string 'SOURx:VOLT x'
6060+a_order = [SOURout,':VOLT ',AMPLITUDE];
6161+% Offset amplitude 'SOURx:VOLT:OFFS xxx'
6262+a_order_off = [SOURout,':VOLT:OFFS ',num2str(0)]; % To 0 volts
6363+% Channel on order string 'OUTPUTx:STATE ON'
6464+on_order = [OUTPUT,':STATE ON'];
6565+6666+% Send orders
6767+writeline(tcpIP,funct_order); % Set function of output signal
6868+writeline(tcpIP,f_order); % Set frequency of output signal
6969+writeline(tcpIP,a_order); % Set amplitude of output signal
7070+writeline(tcpIP,a_order_off); % Set offset amplitude of output signal
7171+writeline(tcpIP,on_order); % Power on output channel
7272+7373+%% Close connection with Red Pitaya
7474+clear('tcpIP')
+96
Red Pitaya STEMlab 125-14/RedPitaya_Pulses.m
···11+function RedPitaya_Pulses(IP,port,outCH,type,f,pulses,amp)
22+% RED PITAYA STEMlab 125-14 v1.1
33+% Comands: https://redpitaya.readthedocs.io/en/latest/appsFeatures/remoteControl/remoteControl.html#list-of-supported-scpi-commands
44+% Jose Manuel Requena Plens (2021) [joreple@upv.es]
55+66+% Check inputs
77+arguments
88+ IP (1,1) string
99+ port (1,1) double
1010+ outCH (1,1) {mustBeInteger,mustBePositive,mustBeMember(outCH,[1,2])}
1111+ type (1,1) string {mustBeMember(type,{'sine','square','triangle','sawu','sawd','pwm'})}
1212+ f (1,1) mustBePositive
1313+ pulses (1,1) {mustBeInteger,mustBePositive}
1414+ amp (1,1) {mustBeInRange(amp,-1,1)}
1515+end
1616+1717+%% CHANNELS
1818+% Output channel
1919+SOURout = ['SOUR',num2str(outCH)];
2020+OUTPUT = ['OUTPUT',num2str(outCH)];
2121+2222+%% SIGNAL
2323+% Type
2424+switch lower(type)
2525+ case 'sine'
2626+ SIGNAL = 'SINE';
2727+ case 'square'
2828+ SIGNAL = 'SQUARE';
2929+ case 'triangle'
3030+ SIGNAL = 'TRIANGLE';
3131+ case 'sawu' % sawtooth up
3232+ SIGNAL = 'SAWU';
3333+ case 'sawd' % sawtooth down
3434+ SIGNAL = 'SAWD';
3535+ case 'pwm'
3636+ SIGNAL = 'PWM'; % Duty cycle set with: 'SOURx:DCYC 0.2'
3737+ otherwise
3838+ error('Type signal error. Available: {sine, square, triangle, sawu, sawd, pwm}.')
3939+end
4040+% Frecuency
4141+FREQUENCY = num2str(f,'%.2f');
4242+% Amplitude (limits -1 to 1 Volt)
4343+if amp > 1; amp = 1; end; if amp < -1; amp = -1; end
4444+AMPLITUDE = num2str(amp);
4545+% Number of pulses
4646+PULSES = num2str(pulses);
4747+4848+4949+%% CONNECTION
5050+tcpIP = tcpclient(IP, port); % Create connection
5151+configureTerminator(tcpIP,"LF","CR/LF"); % Set terminator for write and read
5252+flush(tcpIP); % Clear write/read buffers
5353+5454+%% RESET HARDWARE
5555+writeline(tcpIP,'GEN:RST'); % Reset generator order
5656+5757+%% GENERATE SIGNAL
5858+5959+% Function of output signal order string 'SOURx:FUNC AAAA'
6060+funct_order = [SOURout,':FUNC ',SIGNAL];
6161+% Frequency of output signal order string 'SOURx:FREQ:FIX xxxx'
6262+f_order = [SOURout,':FREQ:FIX ',FREQUENCY];
6363+% Amplitude of output signal order string 'SOURx:VOLT x'
6464+a_order = [SOURout,':VOLT ',AMPLITUDE];
6565+% Offset amplitude 'SOURx:VOLT:OFFS xxx'
6666+a_order_off = [SOURout,':VOLT:OFFS ',num2str(0)]; % To 0 volts
6767+% Phase 'SOURx:PHAS xx'
6868+ph_order = [SOURout,':PHAS ',num2str(0)]; % To 0
6969+% Pulses of signal order string 'SOURx:BURS:NCYC x'
7070+puls_order = [SOURout,':BURS:NCYC ',PULSES];
7171+% Pulses of signal order string 'SOURx:BURS:STAT BURST' % {'BURST','CONTINOUS'}
7272+pstat_order = [SOURout,':BURS:STAT ','BURST'];
7373+% Channel on order string 'OUTPUTx:STATE ON'
7474+on_order = [OUTPUT,':STATE ON'];
7575+7676+% Send orders
7777+writeline(tcpIP,funct_order); % Set function of output signal
7878+writeline(tcpIP,f_order); % Set frequency of output signal
7979+writeline(tcpIP,a_order); % Set amplitude of output signal
8080+writeline(tcpIP,a_order_off); % Set offset amplitude of output signal
8181+writeline(tcpIP,ph_order); % Set phase of output signal
8282+writeline(tcpIP,puls_order); % Set pulses of sine wave
8383+writeline(tcpIP,pstat_order); % Set burst mode
8484+writeline(tcpIP,on_order); % Power on output channel
8585+8686+8787+%% START MEASURE (generation & acquisition)
8888+writeline(tcpIP,'ACQ:START'); % Acquisition
8989+pause(1); % Wait for load buffer
9090+writeline(tcpIP,'ACQ:TRIG AWG_PE');
9191+writeline(tcpIP,[SOURout,':TRIG:IMM']); % Set generator trigger to immediately
9292+9393+%% Close connection with Red Pitaya
9494+clear('tcpIP')
9595+9696+end
+130
Red Pitaya STEMlab 125-14/RedPitaya_Pulses_SRCandREC.m
···11+function [signal_num,t,Fs] = RedPitaya_Pulses_SRCandREC(IP,port,outCH,inCH,type,f,pulses,amp)
22+% RED PITAYA STEMlab 125-14 v1.1
33+% Comands: https://redpitaya.readthedocs.io/en/latest/appsFeatures/remoteControl/remoteControl.html#list-of-supported-scpi-commands
44+% Jose Manuel Requena Plens (2021) [joreple@upv.es]
55+66+% Check inputs
77+arguments
88+ IP (1,1) string
99+ port (1,1) double
1010+ outCH (1,1) {mustBeInteger,mustBePositive,mustBeMember(outCH,[1,2])}
1111+ inCH (1,1) {mustBeInteger,mustBePositive,mustBeMember(inCH,[1,2])}
1212+ type (1,1) string {mustBeMember(type,{'sine','square','triangle','sawu','sawd','pwm'})}
1313+ f (1,1) mustBePositive
1414+ pulses (1,1) {mustBeInteger,mustBePositive}
1515+ amp (1,1) {mustBeInRange(amp,-1,1)}
1616+end
1717+1818+%% CHANNELS
1919+% Output channel
2020+SOURout = ['SOUR',num2str(outCH)];
2121+OUTPUT = ['OUTPUT',num2str(outCH)];
2222+% Input channel
2323+SOURin = ['SOUR',num2str(inCH)];
2424+2525+%% SIGNAL
2626+% Type
2727+switch lower(type)
2828+ case 'sine'
2929+ SIGNAL = 'SINE';
3030+ case 'square'
3131+ SIGNAL = 'SQUARE';
3232+ case 'triangle'
3333+ SIGNAL = 'TRIANGLE';
3434+ case 'sawu' % sawtooth up
3535+ SIGNAL = 'SAWU';
3636+ case 'sawd' % sawtooth down
3737+ SIGNAL = 'SAWD';
3838+ case 'pwm'
3939+ SIGNAL = 'PWM'; % Duty cycle set with: 'SOURx:DCYC 0.2'
4040+ otherwise
4141+ error('Type signal error. Available: {sine, square, triangle, sawu, sawd, pwm}.')
4242+end
4343+% Frecuency
4444+FREQUENCY = num2str(f,'%.2f');
4545+% Amplitude (limits -1 to 1 Volt)
4646+if amp > 1; amp = 1; end; if amp < -1; amp = -1; end
4747+AMPLITUDE = num2str(amp);
4848+% Number of pulses
4949+PULSES = num2str(pulses);
5050+5151+5252+%% CONNECTION
5353+tcpIP = tcpclient(IP, port); % Create connection
5454+configureTerminator(tcpIP,"LF","CR/LF"); % Set terminator for write and read
5555+flush(tcpIP); % Clear write/read buffers
5656+5757+%% RESET HARDWARE
5858+writeline(tcpIP,'GEN:RST'); % Reset generator order
5959+writeline(tcpIP,'ACQ:RST'); % Reset acquisition order
6060+6161+%% GENERATE SIGNAL
6262+6363+% Function of output signal order string 'SOURx:FUNC AAAA'
6464+funct_order = [SOURout,':FUNC ',SIGNAL];
6565+% Frequency of output signal order string 'SOURx:FREQ:FIX xxxx'
6666+f_order = [SOURout,':FREQ:FIX ',FREQUENCY];
6767+% Amplitude of output signal order string 'SOURx:VOLT x'
6868+a_order = [SOURout,':VOLT ',AMPLITUDE];
6969+% Offset amplitude 'SOURx:VOLT:OFFS xxx'
7070+a_order_off = [SOURout,':VOLT:OFFS ',num2str(0)]; % To 0 volts
7171+% Phase 'SOURx:PHAS xx'
7272+ph_order = [SOURout,':PHAS ',num2str(0)]; % To 0
7373+% Pulses of signal order string 'SOURx:BURS:NCYC x'
7474+puls_order = [SOURout,':BURS:NCYC ',PULSES];
7575+% Pulses of signal order string 'SOURx:BURS:STAT BURST' % {'BURST','CONTINOUS'}
7676+pstat_order = [SOURout,':BURS:STAT ','BURST'];
7777+% Channel on order string 'OUTPUTx:STATE ON'
7878+on_order = [OUTPUT,':STATE ON'];
7979+8080+% Send orders
8181+writeline(tcpIP,funct_order); % Set function of output signal
8282+writeline(tcpIP,f_order); % Set frequency of output signal
8383+writeline(tcpIP,a_order); % Set amplitude of output signal
8484+writeline(tcpIP,a_order_off); % Set offset amplitude of output signal
8585+writeline(tcpIP,ph_order); % Set phase of output signal
8686+writeline(tcpIP,puls_order); % Set pulses of sine wave
8787+writeline(tcpIP,pstat_order); % Set burst mode
8888+writeline(tcpIP,on_order); % Power on output channel
8989+9090+%% ACQUISITION
9191+9292+writeline(tcpIP,'ACQ:DEC 64'); % Decimation of signal received = 64 -> Fs = 1.953MS/s
9393+writeline(tcpIP,'ACQ:TRIG:LEV 0'); % Trigger level to 0. Trigger by software
9494+writeline(tcpIP,'ACQ:TRIG:DLY 8192'); % Trigger delay to 0. +-8192 is available
9595+writeline(tcpIP,'ACQ:AVG ON'); % Enable averaging
9696+writeline(tcpIP,'ACQ:GET:DATA:UNITS RAW'); % Acquired data units {'RAW','VOLTS'}
9797+writeline(tcpIP,'ACQ:GET:DATA:FORMAT ASCII'); % Acquired data format {'BIN','ASCII'}
9898+9999+100100+%% START MEASURE (generation & acquisition)
101101+writeline(tcpIP,'ACQ:START'); % Acquisition
102102+pause(1); % Wait for load buffer
103103+writeline(tcpIP,'ACQ:TRIG AWG_PE');
104104+writeline(tcpIP,[SOURout,':TRIG:IMM']); % Set generator trigger to immediately
105105+106106+%% Wait for trigger
107107+while 1
108108+ trig_rsp = strtrim(writeread(tcpIP,'ACQ:TRIG:STAT?'));
109109+ if trig_rsp == "TD" % Trigger Disabled
110110+ break
111111+ end
112112+end
113113+114114+%% READ
115115+writeline(tcpIP,['ACQ:',SOURin,':DATA?']); % Request acquisition data
116116+signal_str = readline(tcpIP); % Read acquisition data
117117+writeline(tcpIP,'ACQ:STOP'); % Stop acquisition
118118+119119+%% PROCESS DATA
120120+signal_str = erase(signal_str,["{","}"]); % Clean
121121+signal_num = str2double(split(signal_str',',')); % To num
122122+123123+% Sample rate and time array
124124+Fs = 1.953e6;
125125+t = (0:1:length(signal_num)-1)/Fs;
126126+127127+%% Close connection with Red Pitaya
128128+clear('tcpIP')
129129+130130+end