TCP Connection Fread data Size Warning Error

8 views (last 30 days)
I have a TCP server in my Matlab code, my client sends some data (max 4 bytes) on some triggers from another application to Matlab. I am receiving those numbers but I am also receiving this warning message many times:
Warning: The specified amount of data was not returned within the Timeout period. 'tcpip' unable to read all requested data. For more information on possible reasons, see TCPIP Read Warnings.
clc;
t = tcpip('0.0.0.0', 55000,'InputBufferSize', 1024,'NetworkRole','Server', 'TimeOut', 0.5);
fopen(t);
while(1)
data = fread(t, 4, 'char');
dataChar = char(data);
dataDouble = str2double(dataChar);
if (~isnan(dataDouble))
if (dataDouble == 0)
fclose(t); % closing the tcp connecting
error('0 value is received!') % to jump out of Matlab run mode
else
disp("Last Receieved double was: " + dataDouble)
end
end
end
I also tried get(obj1,'BytesAvailable'), but it gives me error. Does anyone know how to bypass this warning?

Accepted Answer

Mohsenne Chaverdie
Mohsenne Chaverdie on 18 Nov 2018

More Answers (1)

Walter Roberson
Walter Roberson on 4 Nov 2018
You did not configure for termination mode 'datagram' so when you fread 4 then tcpip is going to read as many packets as it needs in order to get the 4 bytes. It is encountering a timeout in doing so, which is generating the warning.
If you use fread with a size you should be sure that the other end is going to send that size. If the sender is sending variable size then the easiest way to handle it is to have the sender emit a termination character, configure the tcpip connection for matching character, and use fgetl()
It is possible to configure for termination mode datagram and send a variable length packet with no terminator, but for that it is recommended to configure bytes available fcn callback and fread(obj1, obj1.BytesAvailable)

Products


Release

R2018b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!