problem receiving UDP data
5 views (last 30 days)
Show older comments
I am trying to receive UDP data from an external device. Although it is present, I am unable to read it using MATLAB.
I am using a USB-to-Ethernet adapter to connect to the device. The device receives commands on port 50000 and responds on port 50001.
As shown in the Wireshark screenshot, MATLAB can send a command to the device using the 'udpport' and 'write' commands. However, MATLAB is unable to receive the data, even though it is present on the line.
I tried different delays between sending and releasing, as well as a different way of reading the data using 'dsp.UDPReceiver'.
MATLAB CODE:
coreSensIP = "192.168.81.2"; % IP
coreSensPortTx = 50000; % Command port (to module)
coreSensPortRx = 50001; % Response port (from module)
% Create UDP sockets
uTx = udpport("datagram", "IPV4"); % Sending socket
uRx = udpport("datagram", "IPV4", "LocalPort", coreSensPortRx ); % Receiving socket
% Define SCPI command
cmd = '*IDN?';
% Append <CR> (0x0D) as required
cmdBytes = uint8([cmd, char(13)]); % char(13) = 0x0D
% Construct packet fields
dataID = typecast(uint16(1000), 'uint8'); % 0x03E8
segmentID = typecast(uint16(257), 'uint8'); % 0x0101
dataLength = typecast(uint16(length(cmdBytes)), 'uint8');
packet = [dataID, segmentID, dataLength, cmdBytes];
% Send command to device
write(uTx, packet, "uint8", coreSensIP, coreSensPortTx);
disp("Command sent.");
% Wait and read response
disp(uRx);
pause(0.5)
DataAvalible = uRx.NumDatagramsAvailable
if uRx.NumDatagramsAvailable > 0
response = read(uRx, uRx.NumBytesAvailable, "uint8");
disp("Response received:");
disp(char(response));
else
disp("No data available.");
end
% Cleanup
clear uTx uRx;
MATLAB Response:
>> bspApplication
Command sent.
UDPPort with properties:
IPAddressVersion: "IPV4"
LocalHost: "0.0.0.0"
LocalPort: 50001
Tag: ""
NumDatagramsAvailable: 0
Show all properties, functions
DataAvalible =
0
No data available.

4 Comments
Swastik Sarkar
on 16 Jun 2025
It may be helpful to increase the OutputDatagramSize on the receiving UDP port to a higher value, such as 65500, and observe whether any data becomes available on the socket.
Additionally, please provide the size of the data received in the Python program. This will help verify whether the datagram length is within the range expected by the MATLAB UDP port.
Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!