problem receiving UDP data
이전 댓글 표시
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
Abhiram
2025년 6월 13일
Can you try the possible solutions listed in the given MATLAB Documentation page and see if that fixes the issue:
Christoph
2025년 6월 13일
Swastik Sarkar
2025년 6월 16일
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.
Christoph
2025년 6월 26일
답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 UDP Interface에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!