···11+function [Data,t,fs] = RigolMSO1000Z_Read_RunStop(serialNum,Channels,frames)
22+% Read from Rigol Oscilloscope MSO 1000 Z with run/stop capture.
33+%
44+% Example: RigolMSO1000Z_Read_RunStop('DS1ZD181600446',[3,4],4);
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 and 249988 samples.
1313+% t: Time array
1414+% fs: Sample rate
1515+%
1616+% Jose Manuel Requena Plens (2021)
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+%% Measure
3939+writeline(MSO, ':RUN');
4040+HScale = str2double(writeread(MSO, ':TIMebase:MAIN:SCALe?'));
4141+pause(HScale*12*4*2+0.1) % Wait for load signal in oscilloscope
4242+writeline(MSO, ':STOP');
4343+4444+% Flush data
4545+flush(MSO); pause(0.1) % Wait 100ms
4646+4747+%% Read data
4848+4949+% Data available
5050+% RAW: 250000 (uint8), WORD: 125000 (uint16), ASCII: 15625 (CSV)
5151+frameSize = 250000;
5252+HScale = str2double(writeread(MSO, ':TIMebase:MAIN:SCALe?'));
5353+wavelen = HScale * 12; % Scale*div
5454+sampleRate = str2double(writeread(MSO, ':ACQuire:SRATe?'));
5555+totalSize = floor(wavelen*sampleRate); % Memory Depth = Sample Rate × Waveform Length
5656+totalFrames = min(ceil(totalSize/frameSize),frames);
5757+5858+% Initialize
5959+cellFrames = cell(1,totalFrames);
6060+RAWData = zeros((frameSize-12)*totalFrames,length(Channels));
6161+YScale = zeros(1,length(Channels));
6262+refY = zeros(1,length(Channels));
6363+oriY = zeros(1,length(Channels));
6464+exit = false;
6565+6666+% Read
6767+for c = 1:length(Channels) % Each channel
6868+6969+ % Try attempts
7070+ maxatt = 10;
7171+ for att = 1:maxatt
7272+ try
7373+ % Select channel
7474+ writeline(MSO, sprintf(':WAVeform:SOURce CHANnel%d',Channels(c)) );
7575+ % Flush data
7676+ flush(MSO); pause(0.1) % Wait 100ms
7777+7878+ % Read the waveform data (frames)
7979+ frame = 1;
8080+ for point = 0:frameSize:totalSize
8181+8282+ % If all the frames have been received, the loop ends
8383+ if frame>totalFrames; break; end
8484+8585+ % Initial point for frame N
8686+ writeline(MSO, sprintf(':WAVeform:STARt %d',point+13));
8787+8888+ % Final point for frame N
8989+ if (point+frameSize)<=totalSize
9090+ writeline(MSO, sprintf(':WAVeform:STOP %d',point+frameSize))
9191+ frameSizeAux = frameSize;
9292+ else
9393+ % If almost all the memory has been read, the last
9494+ % points are received
9595+ writeline(MSO, sprintf(':WAVeform:STOP %d',totalSize))
9696+ frameSizeAux = totalSize-point;
9797+ addZeros = frameSize-frameSizeAux;
9898+ exit = true;
9999+ end
100100+101101+ % Receive data
102102+ writeline(MSO, ':WAVeform:DATA?'); % Request data
103103+ data = read(MSO, frameSizeAux, 'uint8'); % Read data
104104+105105+ % Fill with zeros if the last frame
106106+ if exit; data = [data(1:end-1),zeros(1,addZeros+1)]; end
107107+108108+ % Remove Data Header (11) and end newline character (1) and store
109109+ cellFrames{frame} = data(12:end-1); % Store frames
110110+111111+ if exit; break; end
112112+ frame = frame + 1;
113113+ end
114114+115115+ % Store data without Data Header (11) and end newline character (1)
116116+ data = [cellFrames{:}];
117117+ RAWData(:,c) = data(:);
118118+119119+ % Flush data and reload channel
120120+ flush(MSO); pause(0.1) % Wait 100ms
121121+ writeline(MSO, sprintf(':WAVeform:SOURce CHANnel%d',Channels(c)) );
122122+123123+ % Get Y-axis information
124124+ YScale(c) = str2double(writeread(MSO, ':WAVeform:YINCrement?'));
125125+ % Reference and origin values to centering data
126126+ refY(c) = str2double(writeread(MSO, ':WAVeform:YREFerence?'));
127127+ oriY(c) = str2double(writeread(MSO, ':WAVeform:YORigin?'));
128128+129129+ % Reinitialize
130130+ exit = false;
131131+ cellFrames = cell(1,totalFrames);
132132+133133+ % Exit the attempts loop
134134+ break;
135135+136136+ catch
137137+ fprintf('Retrying. Attempt %d with channel %d\r\n',att,Channels(c))
138138+ flush(MSO); pause(0.1) % Wait 100ms
139139+ clear MSO
140140+ MSO = visadev(deviceNr); % Create VISA Object
141141+ if att == maxatt
142142+ YScale(c) = nan;
143143+ refY(c) = nan;
144144+ oriY(c) = nan;
145145+ fprintf('Empty values with channel %d\r\n',Channels(c))
146146+ break;
147147+ end
148148+ end
149149+150150+ end
151151+end
152152+153153+% Flush data
154154+flush(MSO); pause(0.1) % Wait 100ms
155155+156156+% Samplerate
157157+incX = str2double(writeread(MSO, ':WAVeform:XINCrement?')); % delta T (secs)
158158+fs = str2double(writeread(MSO, ':ACQuire:SRATe?')); % Sa/s
159159+160160+% Scale data (convert 0-255 values to Measure Units and centering in x-axis)
161161+Data = (RAWData - (refY+oriY) ).*YScale; % Scale and centering
162162+163163+% Time array (seconds)
164164+t = 0:incX:incX*(size(Data,1)-1);
165165+166166+% Clear VISA Object
167167+clear MSO
+13-2
Red Pitaya STEMlab 125-14/RedPitaya_Continuous.m
···11function 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+%
55+% Example: RedPitaya_Continuous('192.168.1.200',5000,1,'sine',40e3,0.5)
66+%
77+% Input:
88+% IP: IP address
99+% Port: Connection port
1010+% outCH: Out channel. 1 or 2
1111+% type: Signal type (sine, square, triangle, sawu, sawd, pwm)
1212+% f: Signal frequency (Hz)
1313+% amp: Signal amplitude (V). Default: 1
1414+%
415% Jose Manuel Requena Plens (2021) [joreple@upv.es]
516617% Check inputs
···920 port (1,1) double
1021 outCH (1,1) {mustBeInteger,mustBePositive,mustBeMember(outCH,[1,2])}
1122 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)}
2323+ f (1,1) {mustBePositive}
2424+ amp (1,1) {mustBeInRange(amp,-1,1)} = 1
1425end
15261627%% CHANNEL
+15-3
Red Pitaya STEMlab 125-14/RedPitaya_Pulses.m
···11function 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+%
55+% Example: RedPitaya_Pulses('192.168.1.200',5000,1,'sine',40e3,100,0.5)
66+%
77+% Input:
88+% IP: IP address
99+% Port: Connection port
1010+% outCH: Out channel. 1 or 2
1111+% type: Signal type (sine, square, triangle, sawu, sawd, pwm)
1212+% f: Signal frequency (Hz)
1313+% pulses: Number of pulses. Default: 50
1414+% amp: Signal amplitude (V). Default: 1
1515+%
416% Jose Manuel Requena Plens (2021) [joreple@upv.es]
517618% Check inputs
···921 port (1,1) double
1022 outCH (1,1) {mustBeInteger,mustBePositive,mustBeMember(outCH,[1,2])}
1123 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)}
2424+ f (1,1) {mustBePositive}
2525+ pulses (1,1) {mustBeInteger,mustBePositive} = 50
2626+ amp (1,1) {mustBeInRange(amp,-1,1)} = 1
1527end
16281729%% CHANNELS
+37-13
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)
11+function [Data,t,fs] = RedPitaya_Pulses_SRCandREC(IP,port,outCH,inCH,type,f,pulses,amp,decimation)
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+%
55+% Example: RedPitaya_Pulses_SRCandREC('192.168.1.200',5000,1,1,'sine',40e3,100,0.5,64)
66+%
77+% Input:
88+% IP: IP address
99+% Port: Connection port
1010+% outCH: Output channel. 1 or 2
1111+% inCH: Input channel. 1 or 2
1212+% type: Signal type (sine, square, triangle, sawu, sawd, pwm)
1313+% f: Signal frequency (Hz)
1414+% pulses: Number of pulses. Default: 50
1515+% amp: Signal amplitude (V). Default: 1
1616+% decimation: Decimation value. Sample Rate = 125/decimation. Default: 64
1717+% Available: 1, 8, 64, 1024, 8192, 65536
1818+%
1919+% Output
2020+% Data: Array with as many columns as channels received and 249988 samples.
2121+% t: Time array
2222+% fs: Sample rate
2323+%
424% Jose Manuel Requena Plens (2021) [joreple@upv.es]
525626% Check inputs
727arguments
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)}
2828+ IP (1,1) string
2929+ port (1,1) double
3030+ outCH (1,1) {mustBeInteger,mustBePositive,mustBeMember(outCH,[1,2])}
3131+ inCH (1,1) {mustBeInteger,mustBePositive,mustBeMember(inCH,[1,2])}
3232+ type (1,1) string {mustBeMember(type,{'sine','square','triangle','sawu','sawd','pwm'})}
3333+ f (1,1) {mustBePositive}
3434+ pulses (1,1) {mustBeInteger,mustBePositive} = 50
3535+ amp (1,1) {mustBeInRange(amp,-1,1)} = 1
3636+ decimation (1,1) {mustBeInteger,mustBePositive,mustBeMember(decimation,[1,8,64,1024,8192,65536])} = 64
1637end
17381839%% CHANNELS
···8911090111%% ACQUISITION
911129292-writeline(tcpIP,'ACQ:DEC 64'); % Decimation of signal received = 64 -> Fs = 1.953MS/s
113113+% Decimation order string 'ACQ:DEC x'
114114+dec_order = sprintf('ACQ:DEC %d',decimation);
115115+116116+writeline(tcpIP,dec_order); % Decimation of signal received -> Fs = 125/decimation
93117writeline(tcpIP,'ACQ:TRIG:LEV 0'); % Trigger level to 0. Trigger by software
94118writeline(tcpIP,'ACQ:TRIG:DLY 8192'); % Trigger delay to 0. +-8192 is available
95119writeline(tcpIP,'ACQ:AVG ON'); % Enable averaging
···118142119143%% PROCESS DATA
120144signal_str = erase(signal_str,["{","}"]); % Clean
121121-signal_num = str2double(split(signal_str',',')); % To num
145145+Data = str2double(split(signal_str',',')); % To num
122146123147% Sample rate and time array
124124-Fs = 1.953e6;
125125-t = (0:1:length(signal_num)-1)/Fs;
148148+fs = 125/str2double(writeread(tcpIP,'ACQ:DEC?'));
149149+t = (0:1:length(Data)-1)/fs;
126150127151%% Close connection with Red Pitaya
128152clear('tcpIP')