Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

New to MATLAB?

Thread Subject:
Real-time processing using serial input

Subject: Real-time processing using serial input

From: Jane T

Date: 20 Oct, 2011 18:35:13

Message: 1 of 2

Hi, up until this morning I had never tried receiving real-time data in Matlab and could use some help.

I'm trying to design some software to receive data from a serial port, check if it's the correct string type, delimit and locate the correct variable, analyse and plot the raw and analysed variables to a Figure. All pretty much real-time.

The string types are of the form:
$GFDTA, 1.4,17,22.8,7029,2011/10/20 12:10:50, NH3OP-1035,1*5C

It's the second parameter, in this case 1.4, that I'm initially interested in.

I have written some code that does what I want for 40 updates from the serial port. I need help updating it to loop 'forever', but only plot the last 40 values.

I think I can loop 'forever' by changing my for loop to a while true loop, but can't figure out how to only plot the last 40 values. I've thought about having an array and everytime removing the first row, adding the new value to the bottom and plotting the whole array. This seems untidy to say the least. I'm also concerned about memory leakage, but at the same time know very little about such things -other than their effect.

Any help would be appreciated, my current code is:

% Define serial object
s1 = serial('COM6','BaudRate',9600,'InputBufferSize',256);

% Open serial object
closeFID = onCleanup(@() delete(s1));

% Open new Figure window, set hold on and define range of x-axis
hold on
set(gca,'xlim',[0 40])

% Initialise variables for the last values of the raw and analysed
% concentrations
lastc = nan;
lastanc = nan;

% Loop through updates from the port
for i = 1:40
    % Receive data packet
    idn = fscanf(s1);

    % Only interested in the strings begining $GFDTA
    if strmatch(idn(2:6),'GFDTA')
        % Delimit string by both commas and asterix'
        delim = regexp(idn,',*','split');
        % Convert the parameter of interest to a number
        c = str2num(strtrim(delim{2}));
        % Plot the new point to the Figure
        plot([i-1 i],[lastc c],'r.-')
        % Update the value for the last raw concentration
        lastc = c;
        % Analyse the raw concentration - current placeholder process, will
        % be more complex and dependent on other values in the input string
        anc = 2*c;
        % Plot the new analysed concentration value
        plot([i-1 i],[lastanc anc],'b.-')
        % Update the value for the last analysed concentration
        lastanc = anc;
        % Pause to allow Figure to update
        % Display raw and analysed concentration readfings to the command
        % window
        disp([c anc]);
% Remove hold from Figure
hold off
% Release the com port


Subject: Real-time processing using serial input

From: Jane T

Date: 24 Oct, 2011 22:06:12

Message: 2 of 2

Ok, I'm sure it's not the most efficient method, but I seem to have manged to achieve what I was trying to do :D

I now have an infinite loop that reads data from a serial input, analyses it and updates a Figure in near real-time.

However, I have now hit another complication that I would appreciate help with. When I have completed recording data, how do I exit the loop? I'm currently typing ctrl + c in the command window, which essentially just crashes the program. Is there a better solution?

Many thanks

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us