• RE: Simultaneous acquisition of all channels from oscilloscope TBS2000B

    I installed Tekscope and got it with a simple operation.
    Thank you very much.
  • Simultaneous acquisition of all channels from oscilloscope TBS2000B

    To read 3 channels of oscilloscope information on Matlab using TBS2000B, I have written the following .m file based on past forum posts
    I am able to save the data to a csv file while drawing the graph.

    https://forum.tek.com/viewtopic.php?t=139326#p282409

    However, when I run the code, I often have problems with the oscilloscope screen freezing up or running too slowly anyway!
    Also, there is a bug that the sine wave, which should be invisible on the screen, is somehow visible on the Matlab figure!

    I think it is possible that the problem is stuck around the point where the information from Ch1~3 is put together and decomposed.
    If there is a template for the matlab file to get the multi-channel information, I would appreciate it if you could let me know.

    -------------------------------------------------------
    clear all
    close all
    instrreset;

    %% Preparation for Communication with Oscilloscope
    visa_brand = 'ni';
    visa_address = 'USB0::0x0699::0x03C7::C010948::INSTR';

    % Open instrument with a default buffer size
    osc = visa(visa_brand, visa_address);
    fopen(osc);

    % Get the current record length
    record = str2double(query(osc, 'hor:reco?'));

    % Close the instrument to set the buffer size
    fclose(osc);

    % Calculate required buffer size and set it
    required_buffer = record * 2; % Adjust as needed
    osc.InputBufferSize = required_buffer;
    osc.OutputBufferSize = required_buffer;

    % Reopen the instrument
    fopen(osc);

    % Create a folder to save data if it doesn't exist
    if ~exist('data_all', 'dir')
        mkdir('data_all');
    end

    % Set the file name
    current_time = datetime('now', 'Format', 'yyyyMMdd_HHmmss');
    csv_filename = sprintf('./data_all/%s.csv', current_time);

    % Write the first row of the CSV file
    titles = {'Timestamp', 'Voltage_CH1', 'Voltage_CH2', 'Voltage_CH3'};
    fid = fopen(csv_filename, 'w');
    fprintf(fid, '%s,%s,%s,%s\n', titles{:});
    fclose(fid);
    % Get scaling values
    x_incr = str2double(query(osc, "wfmo:xincr?"));
    x_zero = 0;
    y_incr = str2double(query(osc, "wfmo:ymult?"));
    y_off = str2double(query(osc, "wfmo:yoff?")); 
    y_zero = str2double(query(osc, "wfmo:yzero?")); 
        
    % Set DATA:SOURCE for CH1 and get scaling values
    fwrite(osc, 'data:source CH1');
    y_incr_ch1 = str2double(query(osc, "wfmo:ymult?"));
    y_off_ch1 = str2double(query(osc, "wfmo:yoff?"));

    % Set DATA:SOURCE for CH2 and get scaling values
    fwrite(osc, 'data:source CH2');
    y_incr_ch2 = str2double(query(osc, "wfmo:ymult?"));
    y_off_ch2 = str2double(query(osc, "wfmo:yoff?"));

    % Set DATA:SOURCE for CH3 and get scaling values
    fwrite(osc, 'data:source CH3');
    y_incr_ch3 = str2double(query(osc, "wfmo:ymult?"));
    y_off_ch3 = str2double(query(osc, "wfmo:yoff?"));

    % Set DATA:SOURCE for multiple channels
    fwrite(osc, 'data:source CH1,CH2,CH3');

    while(1)
        tic

        % Request sample data for all channels
        fwrite(osc, 'curve?');    

        % Read binary block header
        fread(osc, 1); % Discard '#' character
        num_digits = char(fread(osc, 1)); % Number of digits that specify the number of bytes
        num_digits = str2double(num_digits);

        % Ensure num_digits is valid
        if isnan(num_digits) || num_digits <= 0
            warning('Invalid number of digits received from the oscilloscope.');
            continue; % Skip this iteration
        end

        % Read the actual number of bytes
        bytes = char(fread(osc, num_digits)');
        num_bytes = str2double(bytes);

        % Display for debugging purposes
        fprintf('Number of bytes to read: %d\n', num_bytes);

        % Check if num_bytes is a valid number
        if isnan(num_bytes) || num_bytes <= 0
            warning('Invalid number of bytes received from the oscilloscope.');
            continue; % Skip this iteration
        end

        % Read digital values into sample matrix
        all_samples = fread(osc, num_bytes, 'int8');  



        % Calculate the size of data for each channel
        bytes_per_channel = floor(num_bytes / 3);

        % Separate data for each channel
        samples_ch1 = all_samples(1:bytes_per_channel);
        samples_ch2 = all_samples(bytes_per_channel+1:2*bytes_per_channel);
        samples_ch3 = all_samples(2*bytes_per_channel+1:end);

        % Scale and transpose data for each channel with its own scaling
        volt_ch1 = (samples_ch1 * y_incr_ch1) + y_off_ch1;
        volt_ch1 = transpose(volt_ch1-mean(volt_ch1));

        volt_ch2 = (samples_ch2 * y_incr_ch2) + y_off_ch2;
        volt_ch2 = transpose(volt_ch2-mean(volt_ch2));

        volt_ch3 = (samples_ch3 * y_incr_ch3) + y_off_ch3;
        volt_ch3 = transpose(volt_ch3-mean(volt_ch3));

        % Check and adjust the length of each channel's data
        min_length = min([length(volt_ch1), length(volt_ch2), length(volt_ch3)]);
        volt_ch1 = volt_ch1(1:min_length);
        volt_ch2 = volt_ch2(1:min_length);
        volt_ch3 = volt_ch3(1:min_length);


        % Append time and save data
        x_zero = 0;
        x_range = record * x_incr;
        x_max = x_range + x_zero;
        % Adjust time array based on the minimum length
        time = linspace(x_zero, x_max, record);
        time = time(1:min_length); % Adjust the length of time array
        temp_data = [time', volt_ch1', volt_ch2', volt_ch3'];
        dlmwrite(csv_filename, temp_data, '-append', 'delimiter', ',');
        temp_data = []; % Clear array

        % Plotting the voltage data
        plot(time, volt_ch1, time, volt_ch2, time, volt_ch3);
        xlabel('Time (s)'); 
        ylabel('Voltage (V)');
        title('Oscilloscope Voltage Data');
        grid on;
        legend('CH1', 'CH2', 'CH3');
        drawnow; % Update the plot in real time

        toc
    end

    %% Close instrument
    fclose(osc); % Close connection
    delete(osc); % Remove the ICT object
    clear osc; % Remove the local MATLAB variable