Mem-Plot Data Serial Pada Matlab

serial_plot_matlab

Gambar di atas didapat dengan simulasi mikrokontroler dengan proteus -> virtual serial port emulator ->Matlab

Bagaimana memplot data yang di akuisisi oleh mikrokontroler lalu ditampilkan di Matlab?

Missal data yang dikirim dari mikro adalah sebagai berikut.

while (1)
      {
      // Place your code here    
       for(i=0;i<360;i+=60) 
       {        
            sin_wave = 100*sin(i * 0.0174532925);
            printf("data %d %f 100 \n\r",i,sin_wave);
       }   
      }

Kode di matlab untuk menampilkan grafik seperti gambar di atas.

clc
clear all
close all

%*******************inisialisasi serialport*****************************
delete(instrfindall); 
S1 = serial ('COM3', 'Baudrate', 9600, 'DataBits', 8, 'Parity','none'); 
fopen(S1);
figure,
time_t = 1;
for zz=1:1000
    clf; % clear current figure (refresh the polt)
    t = [];
    buffer_data = [];
    ii=0;
    while(ii<10)
        linedata = fgetl(S1);
        data_dec = textscan(linedata,'data %d %f %d');
        dat_A  = data_dec{1};
        dat_B  = data_dec{2};
        dat_C  = data_dec{3};
        t = time_t:time_t + 9;
        buffer_data = [buffer_data; dat_A dat_B dat_C]; 
        ukur = size(buffer_data);
        ii = ukur(1);
    end
    plot(t, buffer_data(:,1),'r-');hold on
    plot(t, buffer_data(:,2),'g-');hold on
    plot(t, buffer_data(:,3),'b-');hold off
    axis([time_t,time_t + 9,-120,360]);    
    time_t = time_t + 10;
    drawnow;
end

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s