%% Read Streaming Data from Arduino Using Serial Port Communication
% This example shows how to enable callbacks to read streaming ASCII terminated data from an Arduino® Due using the serialport interface.
%
%% Load Program on the Arduino
% Plug in an Arduino Due to your computer.
% Load the following program on the Arduino Due using the Arduino IDE. This program writes out continuous points of a sine wave, followed by the "Carriage Return" and "Linefeed" terminators.
% /*
%  SineWavePoints
%
%  Write sine wave points to the serial port, followed by the Carriage Return and LineFeed terminator.
%  */
%
% int i = 0;
%
% // The setup routine runs once when you press reset:
% void setup() {
%   // Initialize serial communication at 9600 bits per second:
%   Serial.begin(9600);
% }
%
% // The loop routine runs over and over again forever:
% void loop() {
%   // Write the sinewave points, followed by the terminator "Carriage Return" and "Linefeed".
%   Serial.print(sin(i*50.0/360.0));
%   Serial.write(13);
%   Serial.write(10);
%   i += 1;
% }
%
%% Establish a Connection to the Arduino
% Create a serialport instance to connect to your Arduino Due.
% Find the serial port that the Arduino is connected to. You can identify the port from the Arduino IDE.

serialportlist("available")'

% Connect to the Arduino Due by creating a serialport object using the port and baud rate specified in the Arduino code.

arduinoObj = serialport("COM13",9600)

%
%% Prepare the serialport Object to Start Streaming Data
% Configure the serialport object by clearing old data and configuring its properties.
% Set the Terminator property to match the terminator that you specified in the Arduino code.

configureTerminator(arduinoObj,"CR/LF");

% Flush the serialport object to remove any old data.

flush(arduinoObj);

% Prepare the UserData property to store the Arduino data. The Data field of the struct saves the sine wave value and the Count field saves the x-axis value of the sine wave.

arduinoObj.UserData = struct("Data",[],"Count",1)

% Create a callback function readSineWaveData that reads the first 1000 ASCII terminated sine wave data points and plots the result.
% function readSineWaveData(src, ~)
%
% % Read the ASCII data from the serialport object.
% data = readline(src);
%
% % Convert the string data to numeric type and save it in the UserData
% % property of the serialport object.
% src.UserData.Data(end+1) = str2double(data);
%
% % Update the Count value of the serialport object.
% src.UserData.Count = src.UserData.Count + 1;
%
% % If 1001 data points have been collected from the Arduino, switch off the
% % callbacks and plot the data.
% if src.UserData.Count > 1001
%     configureCallback(src, "off");
%     plot(src.UserData.Data(2:end));
% end
% end
% Set the BytesAvailableFcnMode property to "terminator" and the BytesAvailableFcn property to @readSineWaveData. The callback function readSineWaveData is triggered when a new sine wave data (with the terminator) is available to be read from the Arduino.

configureCallback(arduinoObj,"terminator",@readSineWaveData);

% The callback function opens the MATLAB figure window with a plot of the first 1000 sine wave data points.
%
% Copyright 2019 The MathWorks, Inc.