matlab arduino serialport

Read Streaming Data from Arduino Using Serial Port Communication
View MATLAB Command

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")'
ans = 3×1 string
    "COM1"
    "COM3"
    "COM13"

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)
arduinoObj = 
Serialport with properties

                 Port: "COM13"
             BaudRate: 9600
    NumBytesAvailable: 0
      NumBytesWritten: 0

Show all properties

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)
arduinoObj = 
Serialport with properties

                 Port: "COM13"
             BaudRate: 9600
    NumBytesAvailable: 10626
      NumBytesWritten: 0

Show all properties

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.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值