698 views|5 replies

1237

Posts

66

Resources
The OP
 

[DigiKey "Smart Manufacturing, Non-stop Happiness" Creative Competition] 3. Send data from the serial port to MATLAB [Copy link]

 

First, we will send the serial port data to MATLAB for subsequent analysis, and then study faster methods such as Ethernet.

Matlab test code

%stopasync(arduinoObj);
%fclose(arduinoObj);
%delete(arduinoObj);

arduinoObj = serialport("COM6",115200,ByteOrder="big-endian")
%configureTerminator(arduinoObj,"CR/LF");
flush(arduinoObj);
%准备 UserData 属性来存储 Arduino 数据。结构体的 Data 字段保存正弦波值,Count 字段保存正弦波的 x 轴值。
arduinoObj.UserData = struct("Data",[],"Count",1)
%创建一个回调函数 readSineWaveData,它读取前 1000 个以 ASCII 字符结尾的正弦波数据点并绘制结果。
%configureCallback(arduinoObj,"terminator",@readSineWaveData);
configureCallback(arduinoObj,"byte",256*4,@readSineWaveData)
function readSineWaveData(src, ~)
% Read the ASCII data from the serialport object.
data = read(src,256,"uint32");
disp(num2str(data));
% 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 > 52
%configureCallback(src, "off");
%plot(src.UserData.Data(2:end));
%end
end

The serial communication of the 2023 version of MATLAB is very convenient to use. It is much smarter than before. You can directly select the data size.

This post is from DigiKey Technology Zone

Latest reply

Thanks for the technical sharing provided by the host. I will collect and study it first and then express my personal opinion.   Details Published on 2024-7-3 17:10
 
 

6570

Posts

0

Resources
2
 

What are the conveniences of serial communication in the 2023 version of Matlab?

This post is from DigiKey Technology Zone
 
 
 

725

Posts

4

Resources
3
 

Thanks for the technical sharing provided by the host. I will collect and study it first and then express my personal opinion.

This post is from DigiKey Technology Zone
 
 
 

725

Posts

4

Resources
4
 

Thanks for the technical sharing provided by the host. I will collect and study it first and then express my personal opinion.

This post is from DigiKey Technology Zone
 
 
 

407

Posts

7

Resources
5
 

MATLAB can be used like this. This is the first time I have seen it. The OP is really good. He knows everything.

This post is from DigiKey Technology Zone
 
 
 

413

Posts

0

Resources
6
 

Thanks for the technical sharing provided by the host. I will collect and study it first and then express my personal opinion.

This post is from DigiKey Technology Zone
 
 
 

Guess Your Favourite
Just looking around
Find a datasheet?

EEWorld Datasheet Technical Support

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号
快速回复 返回顶部 Return list