【问题标题】:Reading data from Arduino in Matlab through serial port通过串口从 Matlab 中的 Arduino 读取数据
【发布时间】:2018-03-29 07:31:13
【问题描述】:

大家好,我是 matlab 新手。我想通过串口读取arduino的数据输出。数据以一定的时间间隔输出,用逗号/空格分隔变量。如何在 Matlab 上读取数据并绘制变量?

串口是这样的:

1.1 3.2

1.2 3.1

1.3 3.3 ...

感谢您的帮助。


这是我只读取一个变量的数据而没有任何空格的代码。如何修改它以读取多个变量的数据?

close all; 
clear all;
clc; 
fclose('all'); 
delete(instrfindall);

%User Defined Properties 
s = serial('COM5', 'baudrate', 9600);
plotTitle='HCSR04';
xLabel='Time (s)';
yLabel='Distance (cm)';
plotGrid = 'on';                
delay = .01;

%Define Function Variables
time = 0;
distance = 0;
count = 0;

%Set up Plot
plotGraph = plot(time,distance);             
title(plotTitle,'FontSize',25);
xlabel(xLabel,'FontSize',15);
ylabel(yLabel,'FontSize',15);
grid(plotGrid);

fopen(s);
tic

while ishandle(plotGraph)
        dist = str2num(fscanf(s))
        count = count + 1;    
        time(count) = toc;
        distance(count) = dist;
        set(plotGraph,'XData',time,'YData',distance);
        axis([time(count)-10 time(count) 0 10]);
        pause(delay);
end
fclose(s);
clear all;
close all;

我试过这个,但似乎不起作用。

close all; 
clear all;
clc; 
fclose('all'); 
delete(instrfindall);

%User Defined Properties 
s = serial('COM3', 'baudrate', 9600);
plotTitle='Sensor Test';
xLabel='Time (s)';
yLabel='Distance (cm)';
plotGrid = 'on';                
delay = .01;

%Define Function Variables
time = 0;
distance1 = 0;
distance2 = 0;
count = 0;

%Set up Plot
plotGraph(1) = plot(time,distance1);
plotGraph(2) = plot(time,distance2);
title(plotTitle,'FontSize',25);
xlabel(xLabel,'FontSize',15);
ylabel(yLabel,'FontSize',15);
grid(plotGrid);

fopen(s);
tic

while ishandle(plotGraph)
        str = fscanf(s);
        dist = textscan(str,'%f %f');
        count = count + 1;    
        time(count) = toc;
        dist1(count) = dist(1);
        dist2(count) = dist(2);
        set(plotGraph(1),'XData',time,'YData',dist1);
        set(plotGraph(2),'XData',time,'YData',dist2); 
        axis([time(count)-10 time(count) 0 10]);
        pause(delay);
end
fclose(s);
clear all;
close all;

【问题讨论】:

    标签: matlab arduino


    【解决方案1】:

    您需要更改从 arduino 获取数据的位置。您的代码中的这一行:

    dist = str2num(fscanf(s))
    

    它从您的串行对象s 获取数据。你可以把它改成这样:

    str = fscanf(s);
    dist = textscan(str,'%f %f');
    

    现在您将dist 作为一个变量,其中包含两个数字。要将它们放在两个单独的变量中,您可以这样做:

    distance1(count) = dist(1);
    distance2(count) = dist(2);
    

    要将它们放入您的绘图中,您现在需要两条线,因此您的距离变量开始为

    distance = [0 0];
    

    并将它们放入您使用的图表中

    set(plotGraph(1),'XData',time,'YData',distance1);
    set(plotGraph(2),'XData',time,'YData',distance2);
    

    【讨论】:

    • 我收到以下错误:str = '5.20 5.18 ' 下标索引必须是实数正整数或逻辑数。 VL53L0X 中的错误(第 33 行)distance1(count) = dist(1);
    • 读取错误。它说您的索引不是真正的正整数。您从 0 开始计数,然后您必须先增加 1,然后才能将其用作索引。将此行向上移动两行count = count + 1; 您是 Matlab 新手。它具有基于 1 的索引。这可能有点令人困惑。
    • 仍然没有工作...没有错误但没有出现图表。我编程太差了……
    • 奇怪。也许用plotGraph=plot(time,[distance1,distance2]);替换plotGraph(1) = plot(time,distance1);plotGraph(2) = plot(time,distance2);
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-25
    • 1970-01-01
    相关资源
    最近更新 更多