PLz help in debugging.

3 views (last 30 days)
LoRa Data Monitoring
LoRa Data Monitoring on 1 Apr 2015
Hi All,
Here below is my code, that reads Accelerometer data from COM4 and needs to build a graph. Please help me to debug this.
//Matlab program to read data through serial port//
function data = serial_read()
serialObj = serial('COM40');
%set(serialObj, 'InputBufferSize', 256); %set(serialObj, 'FlowControl', 'none'); set(serialObj, 'BaudRate', 9600); set(serialObj, 'Parity', 'none'); set(serialObj, 'DataBits', 8); set(serialObj, 'StopBit', 1); set(serialObj, 'Timeout',10); %clc;
disp(get(serialObj,'Name')); prop(1)=(get(serialObj,'BaudRate')); prop(2)=(get(serialObj,'DataBits')); prop(3)=(get(serialObj, 'StopBit')); %prop(4)=(get(serialObj, 'InputBufferSize'));
disp(['Port Setup Done!!',num2str(prop)]);
fopen(serialObj); %t=1; %disp('Running'); %x=0;
c = onCleanup(@()loc_cleanup(serialObj));
while(1) [data,~,~] = scanstr(serialObj); %figure(1); subplot(3,1,1) stem(data{1}); %figure(2); subplot(3,1,2) stem(data{2}); %figure(3); subplot(3,1,3) stem(data{3});
disp('data received is :'); disp(data); holdon drawnow pause(0.000001); end end
%x =[x a];
%plot(x); %axis auto; %grid on;
functionloc_cleanup(serialObj) fclose(serialObj); delete(serialObj); closeall;
end //to plot //
functiondat= serial_read_plot
%Clean up functions to make sure when the code is started we are t a steady %state to run the code a= instrfindall; %find all open serial ports from last instances delete(a); %delete the objects. clearall; clc; closeall;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% serialPort = 'COM40'; %Change Serial as needed according to the Comport % on the arduino seen on the pc %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% plotTitle = 'Serial Data'; xLabel = 'Time (s)'; yLabel = 'Data'; plotGrid = 'on'; scrollWidth = 10; delay = .01; % Fs=1000; % T=1/Fs; % L=1000; % N=2^nextpow2(L); % t=(0:L-1)*T;
%Init time as well as data to zero. time = 0; data = 0; data1= 0; data2= 0; data3= 0; data4= 0; data5= 0; count = 0;
figure(1); % Create one figure and plot to the same.
plotGraph = plot(time,data,'-ro',... 'LineWidth',1,... 'MarkerEdgeColor','r',... 'MarkerFaceColor',[.49 1 .63],... 'MarkerSize',2);
holdon; %The data from last plot is retained.
plotGraph1 = plot(time,data1,'-mo',... 'LineWidth',1,... 'MarkerEdgeColor','m',... 'MarkerFaceColor',[.49 1 .63],... 'MarkerSize',2);
holdon; %The data from last plot is retained.
plotGraph2 = plot(time,data2,'-co',... 'LineWidth',1,... 'MarkerEdgeColor','c',... 'MarkerFaceColor',[.49 1 .63],... 'MarkerSize',2);
holdon; %The data from last plot is retained. figure(2);
plotGraph3 = plot(time,data3,'-ro',... 'LineWidth',1,... 'MarkerEdgeColor','r',... 'MarkerFaceColor',[.49 1 .63],... 'MarkerSize',2);
holdon; %The data from last plot is retained.
plotGraph4 = plot(time,data4,'-mo',... 'LineWidth',1,... 'MarkerEdgeColor','m',... 'MarkerFaceColor',[.49 1 .63],... 'MarkerSize',2);
holdon; %The data from last plot is retained.
plotGraph5 = plot(time,data5,'-co',... 'LineWidth',1,... 'MarkerEdgeColor','c',... 'MarkerFaceColor',[.49 1 .63],... 'MarkerSize',2);
holdon; %The data from last plot is retained.
title(plotTitle,'FontSize',25); xlabel(xLabel,'FontSize',15); ylabel(yLabel,'FontSize',15); grid(plotGrid);
holdon;
s = serial(serialPort); disp('Port is opened now'); fopen(s);
tic
whileishandle(plotGraph) whileishandle(plotGraph1) whileishandle(plotGraph2) whileishandle(plotGraph3) whileishandle(plotGraph4) whileishandle(plotGraph5)
dat = fgetl(s);%Read Data from Serial as Float disp(dat); % decdata=str2double(dat); % y=decdata; % Y=fft(y,N)/L; % f=Fs/2*linspace(0,1,N/2); % figure(3); % plot(f,2*abs(Y(1:1))) % title('ssa'); % hold on;
[a_component,b_component,c_component,d_component,e_component,f_component] = iExtractABCDEF(dat);
%%%%%% Uncomment only if u need to see individual sensor datas. %%%%%%%%%%% % disp(' '); % disp(a_component); % disp(','); % % disp(' '); % disp(b_component); % disp(' '); % disp(c_component); % disp(' '); % disp(d_component); % disp(' '); % disp(e_component); % disp(' '); % disp(f_component); % %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% count = count + 1; %increment to keep track of samples. time(count) = toc; %#ok data(count) = str2double(a_component);%#ok data1(count) = str2double(b_component);%#ok data2(count) = str2double(c_component);%#ok data3(count) = str2double(d_component);%#ok data4(count) = str2double(e_component);%#ok data5(count) = str2double(f_component);%#ok
%Grow and shrink time axis when the data is coming in and update the %Value of the sensor in the Y-Axis. if(scrollWidth> 0) set(plotGraph, 'XData',time(time > time(count)-scrollWidth),'YData',data(time > time(count)-scrollWidth)); set(plotGraph1,'XData',time(time > time(count)-scrollWidth),'YData',data1(time > time(count)-scrollWidth)); set(plotGraph2,'XData',time(time > time(count)-scrollWidth),'YData',data2(time > time(count)-scrollWidth)); set(plotGraph3,'XData',time(time > time(count)-scrollWidth),'YData',data3(time > time(count)-scrollWidth)); set(plotGraph4,'XData',time(time > time(count)-scrollWidth),'YData',data4(time > time(count)-scrollWidth)); set(plotGraph5,'XData',time(time > time(count)-scrollWidth),'YData',data5(time > time(count)-scrollWidth)); else set(plotGraph, 'XData',time,'YData',data); set(plotGraph1,'XData',time,'YData',data1); set(plotGraph2,'XData',time,'YData',data2); set(plotGraph3,'XData',time,'YData',data3); set(plotGraph4,'XData',time,'YData',data4); set(plotGraph5,'XData',time,'YData',data5); end
count = count + 1; pause(delay); end end end end end end end
function [a_component,b_component,c_component,d_component,e_component,f_component] = iExtractABCDEF(dat) [a_component, remain] = strtok(dat, ','); [b_component, remain] = strtok(remain, ','); [c_component, remain] = strtok(remain, ','); [d_component, remain] = strtok(remain, ','); [e_component, remain] = strtok(remain, ','); [f_component, ~] = strtok(remain, ','); end

Answers (0)

Categories

Find more on Line Plots in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!