首先实现将串口数据发送到matlab进行后续分析,后续再研究通过以太网等更加高速的方法。
matlab测试代码
%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
2023版本matlab的串口通信使用非常方便,比以前的智能多了,可以直接选数据大小端
MATLAB可以这样用,还是第一次看到啊,还是楼主厉害,啥都会