0

I would like to store data received from the serial continuously in order to plot them in a graph. I've tried collecting the data in a while loop

while (get(serial, 'BytesAvailable')~=0)
    storeAndPlot()
end

but the program is not able to execute other tasks in the meanwhile.

In this question they use a timer to specify the delay, in seconds, between executions of function. This could be a solution using a short period, such as 0.1, to invoke a getDataFromSerialFunction() and update the graph.

Is there a kind of callback function to invoke once the data is available from the serial?

Community
  • 1
  • 1
UserK
  • 884
  • 3
  • 17
  • 40
  • 1
    well, I dont know anything about serial; but perhaps using a property-value-change-listener could help-> [undocumented_matlab: property-value-change-listener](http://undocumentedmatlab.com/blog/property-value-change-listeners) – Lucius II. Sep 02 '14 at 11:24
  • 1
    you could try putting a `pause(0.1)` or a drawnow inside the while loop. Setting up a listener would be the right way to do it though. If you have the data acquisition toolbox see [here](http://www.mathworks.com/help/daq/ref/dataavailable.html) for a listener function that might help. – Trogdor Sep 02 '14 at 13:48
  • @LuciusDomitiusAhenobarbus, I respectfully disagree: "pause(n) pauses execution for n seconds before continuing, where n is any nonnegative real number." [source](http://www.mathworks.com/help/matlab/ref/pause.html) – Trogdor Sep 02 '14 at 14:25
  • @Trogdor: Gosh you are right :o I delete that comment, otherwise it is just confusing... – Lucius II. Sep 02 '14 at 14:51
  • @Trogdor: I've tried to add the listener to serial object: `xbee = serial('COM3','baudrate',57600,'terminator','CR','tag','Quad');` using: `addlistener(xbee,'DataAvailable', @storeData);` but I got the following error: `First argument provided is not valid for addlistener. (Check its type or validity)` – UserK Sep 02 '14 at 21:40

1 Answers1

1

Ok I've used the timer object as suggested in this question.

global timerXbee;

% When the connect to serial button is pressed
timerXbee = timer('ExecutionMode','FixedRate','Period',0.1,'TimerFcn',{@storeDataFromSerial});
start(timerXbee);
% Polling
fprintf(xbee,'M') ; 
disp ('Connection established.');

In the Callback function

function storeDataFromSerial(obj,event,handles)
   try
        while (get(xbee, 'BytesAvailable')~=0 && tenzo == true)
            % read until terminator
            sentence = fscanf( xbee, '%s'); 
                %decodes "sentence" seperated (delimted) by commas
                decode(sentence);

                % Gets Magnetometer and Estimated Kalman estimated angles
                Tdata = [ Tdata(2:end) ; theta ];
                Pdata = [ Pdata(2:end) ; pitch ];
                Ydata = [ Ydata(2:end) ; yaw ]; 
                EKXdata = [ EKXdata(2:end) ; kr ];
                EKYdata = [ EKYdata(2:end) ; kp ];
            end
        end
   end
end  

When the disconnect button is pressed just stop the timer and delete it

stop(timerXbee);
delete(timerXbee);

Rock & Roll!

Community
  • 1
  • 1
UserK
  • 884
  • 3
  • 17
  • 40