我试图通过RS232将来自basys 2的二进制数据样本发送到带有COM4电缆的matlab,但是MATLAB给了我“矩形空矩阵的不正确分配”。错误。 这是我的MATLAB代码;
clc;clear all; close all; delete(instrfind);
%% SERIAL PORT COMMUNICATION (RS-232 Interface)
%% Parameters
s = serial('COM4'); % Modify COM4 according to your COM port
set(s,'BaudRate',115200); %DO NOT MODIFY (FIXED BAUDRATE)
set(s,'InputBufferSize',2^10); % 1024 byte (you don't have to modify)
set(s,'OutputBufferSize',2^10); % 1024 byte (you don't have to modify)
get(s) % Properties of your serial port
%index parameters
k=1;
nofElem=21; %Number of receive samples (21 for test samples) MODIFY FOR DATA samples
decdata=zeros(1,nofElem); % integer data
%% Read From Serial Port
fopen(s);
while (1)
decdata(k) = fread(s,1);
k=k+1;
if k == nofElem
break; % break the inf loop
end
end
fclose(s);
plot(decdata)
答案 0 :(得分:3)
尝试将代码更改为以下内容。 我刚刚添加了一个检查,看看是否有从串口读取的东西,并查看为什么没有读取任何内容的原因。
fopen(s);
while (k<nofElem)
tic
aux=fread(s,1);
t=toc;
if(isempty(aux))
if t>=get(s,'Timeout')
error('Timeout. Waited for too long')
else
error('Nothing read from serial port and I dont know why');
end
end
decdata(k) = aux;
k=k+1;
end