UDP communication fails in a strange behavior
8 次查看(过去 30 天)
显示 更早的评论
I am working with an Arduino ESP32 board which communicates with the Matlab app through the wireless UDP setup. The communication works fine as long as the receive is within 1 minute of the send/write command. But if the Arduino sends something after more than a minute of receiving the command, the Matlab side never picks it up. Here is the code excerpt-
write(app.pairedStimBox_udpObj, uint8(message), "uint8", app.connection.host, app.connection.espPort);
% Log the sent parameters
out = 'Stim parameters sent: '+ string(message);
activeLog(app, out);
drawnow;
% wait for receiving back the end of stimulation confirmation
while (~app.recvFlag)
if app.pairedStimBox_udpObj.NumBytesAvailable > 0
dataReceived = char(read(app.pairedStimBox_udpObj,app.pairedStimBox_udpObj.NumBytesAvailable, "uint8"));
values = str2num(dataReceived);
Once the writing-reading gap is more than a minute, the NumBytesAvailable does not pick up any data!
On the Arduino side, in Serial Monitor, I see the data is being sent, so I think that is not the issue. Can anyone help me understand this behavior and help me fix it? Appreciate your help.
0 个评论
回答(1 个)
Abhiram
2025-4-23
The timeout issue with UDP sockets can occur as UDP sockets do not maintain a persistent connection. If there is no data transfer for a while, the underlying OS or MATLAB's UDP object may close or pause the socket.
A workaround for this issue is to keep the UDP socket alive by sending a ‘keep-alive’ message from the Arduino board to MATLAB every 30 seconds. This keeps the socket active and prevents OS/MATLAB timeouts.
0 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!