MATLAB Answers

0

Serial timeout but good data received

Alan ALLART 님이 질문을 제출함. 8 Oct 2019
Hi,
I am currently reading from an STM32 through UART using Matlab.
With the following program, I have been able to read single byte data (uint8, int8, char), even if several bytes come right after each others.
The problem comes when I try with multibytes datatypes (float32, uint32, ...). The console print me the received data, which is the one sent by the STM32, but the soft freezes and after the 10seconds default value timeout, I get this warning:
Warning: The specified amount of data was not returned within the Timeout period. 'serial' unable to read all requested data. For more information on possible reasons, see Serial Read Warnings.
The fact that I receive exactly the sent numbers (and that there is the good amount of bits sent when I check with an oscilloscope) tends to tell me that the problem is not from my soft in the STM32, but in the matlab interpretation, which seems to wait for something else.
Thank you all in advance for your ideas
clear;
clc;
% ------------------------ TWEAKING PARAMETERS -------------------------- %
port = 'COM4'; %Serial port
% seriallist : This command list all the COM port available
baudrate = 115200; % Frequency
dataType = 'uint32'; %Data to be received and transmitted
readDelay = 0.001; %The time between 2 buffer emptying in seconds
maxDataStored = 1000; %The number of data to be stored in final file
serialInBufferSize = 1024; %The input buffer size
%Make sure that sending frequency < serialInBufferSize / readDelay
storeCSV = 0; %Enable storage in CSV file
% ----------------------------------------------------------------------- %
totalData = 0;
maxDataReached = 0;
timeStamps(maxDataStored) = datetime;
timeElapsed = zeros(1, maxDataStored);
receivedData = zeros(1, maxDataStored, dataType);
%Creates main control panel to end serial streaming
controlPanel = figure('WindowStyle', 'docked', ...
'MenuBar', 'none');
stop = uicontrol('Style', 'PushButton', ...
'String', 'End communication', ...
'Position', [0, 0, 200, 40], ...
'Callback', 'delete(gcbf)');
drawnow;
% Initializes serial port
disp(['Initialising serial port ' port]);
s = instrfind('port',port);
if not(isempty(s)) % Destroy existing serial port
disp(['Killing existant communication on ' port]);
fclose(s);
delete(s);
clear s
end
s = serial(port,'BaudRate',baudrate, ...
'InputBufferSize', serialInBufferSize, ...
'Parity', 'even');
fopen(s);
startTime = datetime('now');
disp(['Port ' port ' initialised successfully at ' num2str(baudrate) ...
' bauds']);
disp('Beginning of communication');
disp(' ');
% Main script to store data
while (ishandle(stop) && maxDataReached == 0)
%The < 4 condition was an unsuccessfull test, it used to be == 0
while (s.BytesAvailable < 4 && ishandle(stop))
pause (readDelay);
end
if(ishandle(stop))
in = fread(s, s.BytesAvailable, dataType);
for i = 1 : length(in)
if (totalData + 1 > maxDataStored)
maxDataReached = 1;
disp(' ');
disp('Maximum amount of received data reached');
break
end
dt = seconds((datetime('now') - startTime));
%Storage
timeStamps(totalData + 1) = datetime('now');
timeElapsed(totalData + 1) = dt;
receivedData(totalData + 1) = in(i);
%Console printing
disp([datestr(now,'HH:MM:SS.FFF'), ' || ', ...
num2str(dt, '%.3f'), ...
's since startup || received : ', ...
num2str(in(i))]);
totalData = totalData + 1;
end
pause(0.01);
end
end
% Closes serial port
disp(' ');
disp(['Ending communication on port ' port ' ...']);
fclose(s);
delete(s);
clear s
disp('Communication ended properly (I hope so...)');
%Script termination
close(gcf);
disp(' ');
disp('Script ended');

  댓글 수: 0

로그인 to comment.

태그

답변 수: 0


Translated by