我试图在'实时'来自Arduino的Matlab。我有以下代码来获取数据:
clear all; close all;clc;delete(instrfind({'port'},{'COM12'}));
puerto=serial('COM12');
puerto.BaudRate=115200;
fopen(puerto);
for i=1:1000
a=fscanf(puerto, '%d');
v(i)=a;
++i;
end
fclose(puerto);
delete(puerto);
但是,有时我会收到错误说明以下内容(参考变量a):
在作业A(I)= B中,B和I中的元素数必须相同。
(第8行)
中的错误V(I)= A;
所以我有3个问题:
答案 0 :(得分:0)
尝试这样的事情。使用串行端口的BytesAvailableFcn
。
See Documentation
function serialTest()
lineHandle = plot(now,0);
hold on;
delete(instrfind({'port'},{'COM12'}));
puerto=serial('COM12');
puerto.InputBufferSize = 200; %Set to an appropriate number of bytes see docs
puerto.BaudRate=115200;
puerto.BytesAvailableFcnCount = 100; %Set to an appropriate number see docs.
set(puerto,'BytesAvailableFcn',{@bytesFcnCallback,lineHandle});
drawnow
fopen(puerto);
function bytesFcnCallback(puerto,evtData,lineHandle)
temp = fgetl(puerto); %Read a single line from the com port.
C = textscan(temp,'%f');
lineHandle(1).YData= [lineHandle(1).YData C{1}];
lineHandle(1).XData(end+1) = now;
编辑:更新以显示如何不断更新绘图。