Matlab socket async mode is very slow (callback is called every 1~2 seconds)
9 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Hi I am developing a Matlab/Android server/client program, which needs Matlab to have a TCP connection to a Android device. Specifically, Matlab will get some sensor data from Android (streamed via socket) and provide some processed result back to Android.
I used to just let Matlab busy-wait for the socket fread (blocking call) and it behave ok. (1~5 ms delay for Android to send the sensor data and get response and Matlab can update a figure of sensor data in about 10fps.)
However, currently I need to a more sophisticated UI control on Matlab, so I need the Matlab socket to be async (e.g., I will write a script to open several sockets to talk to several Android devices simultaneously).
I found there is an async mode for the Matlab socket as shown in the following:
socket=tcpip('0.0.0.0', port, 'NetworkRole', 'server', 'Timeout', SOCKET_TIME_OUT);
socket.ReadAsyncMode='continuous';
socket.BytesAvailableFcnMode='byte';
socket.BytesAvailableFcnCount=1; % trigger the event every byte available
socket.BytesAvailableFcn=@(~,~)socketReadCallback;
socket.InputBufferSize = 4800;
function socketReadCallback()
% do some processing
dataSize=fread(socket, 1, 'int8');
data=fread(socket, dataSize, 'int8');
result=process(data); % do some processing for my application
fwrite(socket, result);
end
It works as expected. The socket will not hang the Matlab console so I can create other sockets for other devices and update some UI elements. Whenever there is available data in the socket, the callback will be called.
However, it is extremely SLOW, the callback is called every 1~2 seconds while the data is sent every 20ms. I read some posts online about the same issue of async read/write in USB port and the solution is to set a large "socket.BytesAvailableFcnCount" (so the fread/fwrite can hanlde a bulk data each time). But it does not apply to my scenario because I want to my system to be "responsive" with low delay. Specifically, I want the callback to be immediately called whenever there is byte available.
Any suggestion will be appreciated!
0 commentaires
Réponses (0)
Voir également
Catégories
En savoir plus sur MATLAB Mobile dans Help Center et File Exchange
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!