How to improve UDP performance when using udp() function from Instrument Control Toolbox?
11 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
MathWorks Support Team
am 22 Mär. 2018
Bearbeitet: MathWorks Support Team
am 3 Sep. 2025 um 15:39
I am using the fread() function to continuously receive datagrams, and the fprintf() function to write the data to a text file when a datagram is received:
UDP = udp(ip, port);UDP.InputBufferSize = 1024;
UDP.DatagramTerminateMode = 'ON';fopen(UDP);while 1
ADCdata = fread(UDP, 1, 'uint16');
fprintf(ADCdataFile, '%d\n', ADCdata);end
We would like the data rate to be as high as possible for our application. Right now, when transmitting at a rate of 4Mbps or so, the data is read in and saved with no problem. However, at 15–20Mbps, MATLAB is not able to keep up with the data, and much of the data is lost (i.e. not read/saved).
Is there a data rate limitation to the UDP fread() functionality that is known?
Is there a way to increase the rate at which UDP data is read in and saved to a file? Perhaps the above code is not optimal, or there may be a better function than fprintf()?
Akzeptierte Antwort
MathWorks Support Team
am 3 Sep. 2025 um 0:00
Bearbeitet: MathWorks Support Team
am 3 Sep. 2025 um 15:39
Regarding the usage of the 'udp' interface, the reading speed is slower than the data transmission, most likely because the writing to file operation is happening for every byte. Please consider reading and processing the data in chunks to speed up the process.
Please refer to a modified example below that might help with your program by using a callback function when a diagram packet is received, which should help speed things up:
% Define the IP address and port for the UDP connection
ip = 'your_ip_address'; % Replace with the actual IP address
port = your_port_number; % Replace with the actual port number
% Create a UDP object
UDP = udp(ip, port);
% Configure UDP object properties
UDP.InputBufferSize = 1024;
UDP.DatagramTerminateMode = 'on';
UDP.DatagramReceivedFcn = @callbackFcn;
% Open the UDP object to begin communication
fopen(UDP);
% Define the callback function
function callbackFcn(src, ~)
% Read the received datagram
data = fread(src, src.InputDatagramPacketSize, 'uint16');
% Process the data as needed
% For example, display the data
fprintf('Received data: %s\n', num2str(data'));
% Additional processing can be added here
end
% It's good practice to clean up by closing and deleting the UDP object when done
% fclose(UDP);
% delete(UDP);
% clear UDP;
Please note that it is not recommended to use UDP when transmitting heavy payloads due to the size limitation of UDP itself. In general, since UDP is established on top of IP (Internet Protocol), the theoretical maximum packet it can handle is 65,535 bytes, including necessary headers. Packets larger than this are broken down by the protocol into separate packets, and UDP does not guarantee the ordering and verification of delivery in this scenario.
However, it is not recommended to send a payload over 512 bytes in a single UDP packet, because smaller packets would most likely cause fewer issues in transmission, as UDP is unreliable.
In MATLAB, according to this documentation page, the maximum packet size for reading is 8192 bytes. You may write any data size to the output buffer, but the data will be sent in packets of at most 4096 bytes.
Additional note: Starting from R2020b, the usage of udp() is no longer recommended. Use udpport() instead. To access the relevant documentation, see: https://www.mathworks.com/help/instrument/udpport.html
0 Kommentare
Weitere Antworten (0)
Siehe auch
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!