[READ-ONLY] Mirror of https://github.com/jmrplens/MATLAB_Instruments. Laboratory instruments control with MATLAB
experiment instruments laboratory matlab
0

Configure Feed

Select the types of activity you want to include in your feed.

update

Jose M. Requena Plens (Oct 28, 2021, 10:53 AM +0200) 3347c9a1 2b9a0f7c

+236 -22
+4 -4
Oscilloscope Rigol MSO1000Z - DS1000Z/RigolMSO1000Z_Read.m
··· 17 17 18 18 % Check inputs 19 19 arguments 20 - serialNum (1,1) string 20 + serialNum (1,1) string 21 21 Channels (1,:) {mustBeInteger,mustBePositive,mustBeMember(Channels,[1,2,3,4])} 22 22 frames {mustBeInteger,mustBeScalarOrEmpty,mustBePositive} = 2 23 23 end ··· 43 43 HScale = str2double(writeread(MSO, ':TIMebase:MAIN:SCALe?')); 44 44 wavelen = HScale * 12; % Scale*div 45 45 sampleRate = str2double(writeread(MSO, ':ACQuire:SRATe?')); 46 - totalSize = wavelen*sampleRate; % Memory Depth = Sample Rate × Waveform Length 46 + totalSize = floor(wavelen*sampleRate); % Memory Depth = Sample Rate × Waveform Length 47 47 totalFrames = min(ceil(totalSize/frameSize),frames); 48 48 49 49 % Initialize ··· 81 81 writeline(MSO, sprintf(':WAVeform:STOP %d',point+frameSize)) 82 82 frameSizeAux = frameSize; 83 83 else 84 - % If almost all the memory has been read, the last 84 + % If almost all the memory has been read, the last 85 85 % points are received 86 86 writeline(MSO, sprintf(':WAVeform:STOP %d',totalSize)) 87 87 frameSizeAux = totalSize-point; ··· 102 102 if exit; break; end 103 103 frame = frame + 1; 104 104 end 105 - 105 + 106 106 % Store data without Data Header (11) and end newline character (1) 107 107 data = [cellFrames{:}]; 108 108 RAWData(:,c) = data(:);
+167
Oscilloscope Rigol MSO1000Z - DS1000Z/RigolMSO1000Z_Read_RunStop.m
··· 1 + function [Data,t,fs] = RigolMSO1000Z_Read_RunStop(serialNum,Channels,frames) 2 + % Read from Rigol Oscilloscope MSO 1000 Z with run/stop capture. 3 + % 4 + % Example: RigolMSO1000Z_Read_RunStop('DS1ZD181600446',[3,4],4); 5 + % 6 + % Input: 7 + % serialNum: Serial number (char or string), e.g. 'DS1ZD181600446' 8 + % Channels: One-dimension array with channel or channels to read: [1],[2,4],... 9 + % frames: Number of frames to be read from internal memory. 10 + % 11 + % Output 12 + % Data: Array with as many columns as channels received and 249988 samples. 13 + % t: Time array 14 + % fs: Sample rate 15 + % 16 + % Jose Manuel Requena Plens (2021) 17 + 18 + % Check inputs 19 + arguments 20 + serialNum (1,1) string 21 + Channels (1,:) {mustBeInteger,mustBePositive,mustBeMember(Channels,[1,2,3,4])} 22 + frames {mustBeInteger,mustBeScalarOrEmpty,mustBePositive} = 2 23 + end 24 + 25 + %% Load instrument 26 + devlist = visadevlist(); % Get all VISA resources availables 27 + [~,idxdev] = ismember(serialNum,devlist.SerialNumber); % Search the serial number 28 + deviceNr = devlist.ResourceName{idxdev(1)}; % Get resource name 29 + MSO = visadev(deviceNr); % Create VISA Object 30 + 31 + %% Signal data format 32 + writeline(MSO, ':WAVeform:MODE RAW' ); 33 + writeline(MSO, ':WAVeform:FORMat BYTE') 34 + 35 + % Flush data 36 + flush(MSO); pause(0.1) % Wait 100ms 37 + 38 + %% Measure 39 + writeline(MSO, ':RUN'); 40 + HScale = str2double(writeread(MSO, ':TIMebase:MAIN:SCALe?')); 41 + pause(HScale*12*4*2+0.1) % Wait for load signal in oscilloscope 42 + writeline(MSO, ':STOP'); 43 + 44 + % Flush data 45 + flush(MSO); pause(0.1) % Wait 100ms 46 + 47 + %% Read data 48 + 49 + % Data available 50 + % RAW: 250000 (uint8), WORD: 125000 (uint16), ASCII: 15625 (CSV) 51 + frameSize = 250000; 52 + HScale = str2double(writeread(MSO, ':TIMebase:MAIN:SCALe?')); 53 + wavelen = HScale * 12; % Scale*div 54 + sampleRate = str2double(writeread(MSO, ':ACQuire:SRATe?')); 55 + totalSize = floor(wavelen*sampleRate); % Memory Depth = Sample Rate × Waveform Length 56 + totalFrames = min(ceil(totalSize/frameSize),frames); 57 + 58 + % Initialize 59 + cellFrames = cell(1,totalFrames); 60 + RAWData = zeros((frameSize-12)*totalFrames,length(Channels)); 61 + YScale = zeros(1,length(Channels)); 62 + refY = zeros(1,length(Channels)); 63 + oriY = zeros(1,length(Channels)); 64 + exit = false; 65 + 66 + % Read 67 + for c = 1:length(Channels) % Each channel 68 + 69 + % Try attempts 70 + maxatt = 10; 71 + for att = 1:maxatt 72 + try 73 + % Select channel 74 + writeline(MSO, sprintf(':WAVeform:SOURce CHANnel%d',Channels(c)) ); 75 + % Flush data 76 + flush(MSO); pause(0.1) % Wait 100ms 77 + 78 + % Read the waveform data (frames) 79 + frame = 1; 80 + for point = 0:frameSize:totalSize 81 + 82 + % If all the frames have been received, the loop ends 83 + if frame>totalFrames; break; end 84 + 85 + % Initial point for frame N 86 + writeline(MSO, sprintf(':WAVeform:STARt %d',point+13)); 87 + 88 + % Final point for frame N 89 + if (point+frameSize)<=totalSize 90 + writeline(MSO, sprintf(':WAVeform:STOP %d',point+frameSize)) 91 + frameSizeAux = frameSize; 92 + else 93 + % If almost all the memory has been read, the last 94 + % points are received 95 + writeline(MSO, sprintf(':WAVeform:STOP %d',totalSize)) 96 + frameSizeAux = totalSize-point; 97 + addZeros = frameSize-frameSizeAux; 98 + exit = true; 99 + end 100 + 101 + % Receive data 102 + writeline(MSO, ':WAVeform:DATA?'); % Request data 103 + data = read(MSO, frameSizeAux, 'uint8'); % Read data 104 + 105 + % Fill with zeros if the last frame 106 + if exit; data = [data(1:end-1),zeros(1,addZeros+1)]; end 107 + 108 + % Remove Data Header (11) and end newline character (1) and store 109 + cellFrames{frame} = data(12:end-1); % Store frames 110 + 111 + if exit; break; end 112 + frame = frame + 1; 113 + end 114 + 115 + % Store data without Data Header (11) and end newline character (1) 116 + data = [cellFrames{:}]; 117 + RAWData(:,c) = data(:); 118 + 119 + % Flush data and reload channel 120 + flush(MSO); pause(0.1) % Wait 100ms 121 + writeline(MSO, sprintf(':WAVeform:SOURce CHANnel%d',Channels(c)) ); 122 + 123 + % Get Y-axis information 124 + YScale(c) = str2double(writeread(MSO, ':WAVeform:YINCrement?')); 125 + % Reference and origin values to centering data 126 + refY(c) = str2double(writeread(MSO, ':WAVeform:YREFerence?')); 127 + oriY(c) = str2double(writeread(MSO, ':WAVeform:YORigin?')); 128 + 129 + % Reinitialize 130 + exit = false; 131 + cellFrames = cell(1,totalFrames); 132 + 133 + % Exit the attempts loop 134 + break; 135 + 136 + catch 137 + fprintf('Retrying. Attempt %d with channel %d\r\n',att,Channels(c)) 138 + flush(MSO); pause(0.1) % Wait 100ms 139 + clear MSO 140 + MSO = visadev(deviceNr); % Create VISA Object 141 + if att == maxatt 142 + YScale(c) = nan; 143 + refY(c) = nan; 144 + oriY(c) = nan; 145 + fprintf('Empty values with channel %d\r\n',Channels(c)) 146 + break; 147 + end 148 + end 149 + 150 + end 151 + end 152 + 153 + % Flush data 154 + flush(MSO); pause(0.1) % Wait 100ms 155 + 156 + % Samplerate 157 + incX = str2double(writeread(MSO, ':WAVeform:XINCrement?')); % delta T (secs) 158 + fs = str2double(writeread(MSO, ':ACQuire:SRATe?')); % Sa/s 159 + 160 + % Scale data (convert 0-255 values to Measure Units and centering in x-axis) 161 + Data = (RAWData - (refY+oriY) ).*YScale; % Scale and centering 162 + 163 + % Time array (seconds) 164 + t = 0:incX:incX*(size(Data,1)-1); 165 + 166 + % Clear VISA Object 167 + clear MSO
+13 -2
Red Pitaya STEMlab 125-14/RedPitaya_Continuous.m
··· 1 1 function RedPitaya_Continuous(IP,port,outCH,type,f,amp) 2 2 % RED PITAYA STEMlab 125-14 v1.1 3 3 % Comands: https://redpitaya.readthedocs.io/en/latest/appsFeatures/remoteControl/remoteControl.html#list-of-supported-scpi-commands 4 + % 5 + % Example: RedPitaya_Continuous('192.168.1.200',5000,1,'sine',40e3,0.5) 6 + % 7 + % Input: 8 + % IP: IP address 9 + % Port: Connection port 10 + % outCH: Out channel. 1 or 2 11 + % type: Signal type (sine, square, triangle, sawu, sawd, pwm) 12 + % f: Signal frequency (Hz) 13 + % amp: Signal amplitude (V). Default: 1 14 + % 4 15 % Jose Manuel Requena Plens (2021) [joreple@upv.es] 5 16 6 17 % Check inputs ··· 9 20 port (1,1) double 10 21 outCH (1,1) {mustBeInteger,mustBePositive,mustBeMember(outCH,[1,2])} 11 22 type (1,1) string {mustBeMember(type,{'sine','square','triangle','sawu','sawd','pwm'})} 12 - f (1,1) mustBePositive 13 - amp (1,1) {mustBeInRange(amp,-1,1)} 23 + f (1,1) {mustBePositive} 24 + amp (1,1) {mustBeInRange(amp,-1,1)} = 1 14 25 end 15 26 16 27 %% CHANNEL
+15 -3
Red Pitaya STEMlab 125-14/RedPitaya_Pulses.m
··· 1 1 function RedPitaya_Pulses(IP,port,outCH,type,f,pulses,amp) 2 2 % RED PITAYA STEMlab 125-14 v1.1 3 3 % Comands: https://redpitaya.readthedocs.io/en/latest/appsFeatures/remoteControl/remoteControl.html#list-of-supported-scpi-commands 4 + % 5 + % Example: RedPitaya_Pulses('192.168.1.200',5000,1,'sine',40e3,100,0.5) 6 + % 7 + % Input: 8 + % IP: IP address 9 + % Port: Connection port 10 + % outCH: Out channel. 1 or 2 11 + % type: Signal type (sine, square, triangle, sawu, sawd, pwm) 12 + % f: Signal frequency (Hz) 13 + % pulses: Number of pulses. Default: 50 14 + % amp: Signal amplitude (V). Default: 1 15 + % 4 16 % Jose Manuel Requena Plens (2021) [joreple@upv.es] 5 17 6 18 % Check inputs ··· 9 21 port (1,1) double 10 22 outCH (1,1) {mustBeInteger,mustBePositive,mustBeMember(outCH,[1,2])} 11 23 type (1,1) string {mustBeMember(type,{'sine','square','triangle','sawu','sawd','pwm'})} 12 - f (1,1) mustBePositive 13 - pulses (1,1) {mustBeInteger,mustBePositive} 14 - amp (1,1) {mustBeInRange(amp,-1,1)} 24 + f (1,1) {mustBePositive} 25 + pulses (1,1) {mustBeInteger,mustBePositive} = 50 26 + amp (1,1) {mustBeInRange(amp,-1,1)} = 1 15 27 end 16 28 17 29 %% CHANNELS
+37 -13
Red Pitaya STEMlab 125-14/RedPitaya_Pulses_SRCandREC.m
··· 1 - function [signal_num,t,Fs] = RedPitaya_Pulses_SRCandREC(IP,port,outCH,inCH,type,f,pulses,amp) 1 + function [Data,t,fs] = RedPitaya_Pulses_SRCandREC(IP,port,outCH,inCH,type,f,pulses,amp,decimation) 2 2 % RED PITAYA STEMlab 125-14 v1.1 3 3 % Comands: https://redpitaya.readthedocs.io/en/latest/appsFeatures/remoteControl/remoteControl.html#list-of-supported-scpi-commands 4 + % 5 + % Example: RedPitaya_Pulses_SRCandREC('192.168.1.200',5000,1,1,'sine',40e3,100,0.5,64) 6 + % 7 + % Input: 8 + % IP: IP address 9 + % Port: Connection port 10 + % outCH: Output channel. 1 or 2 11 + % inCH: Input channel. 1 or 2 12 + % type: Signal type (sine, square, triangle, sawu, sawd, pwm) 13 + % f: Signal frequency (Hz) 14 + % pulses: Number of pulses. Default: 50 15 + % amp: Signal amplitude (V). Default: 1 16 + % decimation: Decimation value. Sample Rate = 125/decimation. Default: 64 17 + % Available: 1, 8, 64, 1024, 8192, 65536 18 + % 19 + % Output 20 + % Data: Array with as many columns as channels received and 249988 samples. 21 + % t: Time array 22 + % fs: Sample rate 23 + % 4 24 % Jose Manuel Requena Plens (2021) [joreple@upv.es] 5 25 6 26 % Check inputs 7 27 arguments 8 - IP (1,1) string 9 - port (1,1) double 10 - outCH (1,1) {mustBeInteger,mustBePositive,mustBeMember(outCH,[1,2])} 11 - inCH (1,1) {mustBeInteger,mustBePositive,mustBeMember(inCH,[1,2])} 12 - type (1,1) string {mustBeMember(type,{'sine','square','triangle','sawu','sawd','pwm'})} 13 - f (1,1) mustBePositive 14 - pulses (1,1) {mustBeInteger,mustBePositive} 15 - amp (1,1) {mustBeInRange(amp,-1,1)} 28 + IP (1,1) string 29 + port (1,1) double 30 + outCH (1,1) {mustBeInteger,mustBePositive,mustBeMember(outCH,[1,2])} 31 + inCH (1,1) {mustBeInteger,mustBePositive,mustBeMember(inCH,[1,2])} 32 + type (1,1) string {mustBeMember(type,{'sine','square','triangle','sawu','sawd','pwm'})} 33 + f (1,1) {mustBePositive} 34 + pulses (1,1) {mustBeInteger,mustBePositive} = 50 35 + amp (1,1) {mustBeInRange(amp,-1,1)} = 1 36 + decimation (1,1) {mustBeInteger,mustBePositive,mustBeMember(decimation,[1,8,64,1024,8192,65536])} = 64 16 37 end 17 38 18 39 %% CHANNELS ··· 89 110 90 111 %% ACQUISITION 91 112 92 - writeline(tcpIP,'ACQ:DEC 64'); % Decimation of signal received = 64 -> Fs = 1.953MS/s 113 + % Decimation order string 'ACQ:DEC x' 114 + dec_order = sprintf('ACQ:DEC %d',decimation); 115 + 116 + writeline(tcpIP,dec_order); % Decimation of signal received -> Fs = 125/decimation 93 117 writeline(tcpIP,'ACQ:TRIG:LEV 0'); % Trigger level to 0. Trigger by software 94 118 writeline(tcpIP,'ACQ:TRIG:DLY 8192'); % Trigger delay to 0. +-8192 is available 95 119 writeline(tcpIP,'ACQ:AVG ON'); % Enable averaging ··· 118 142 119 143 %% PROCESS DATA 120 144 signal_str = erase(signal_str,["{","}"]); % Clean 121 - signal_num = str2double(split(signal_str',',')); % To num 145 + Data = str2double(split(signal_str',',')); % To num 122 146 123 147 % Sample rate and time array 124 - Fs = 1.953e6; 125 - t = (0:1:length(signal_num)-1)/Fs; 148 + fs = 125/str2double(writeread(tcpIP,'ACQ:DEC?')); 149 + t = (0:1:length(Data)-1)/fs; 126 150 127 151 %% Close connection with Red Pitaya 128 152 clear('tcpIP')