Reputation: 35
I am trying to send binary data samples from basys 2, to matlab with COM4 cable via RS232 but MATLAB gives me "Improper assignment with rectangular empty matrix." error. Here is my MATLAB CODE;
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)
Upvotes: 0
Views: 334
Reputation: 35525
Try changing your code to the following. I just added a check to see if there is something read from the serial port, and to see the cause of why nothing has been read.
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
Upvotes: 3