problem receiving UDP data

5 次查看(过去 30 天)
Christoph
Christoph 2025-6-11
评论: Christoph 2025-6-26
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 个评论
Swastik Sarkar
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
Christoph 2025-6-26
I increased the OutputDatagramSize by
disp(uRx);
pause(0.5)
response = read(uRx, 65500, "uint8");
disp("Response received:");
disp(char(response));
,but still I got no response:
Command sent.
UDPPort with properties:
IPAddressVersion: "IPV4"
LocalHost: "0.0.0.0"
LocalPort: 50001
Tag: ""
NumDatagramsAvailable: 0
Show all properties, functions
Warning: Specified amount of data was not returned within the Timeout period for "read".
'udpport' unable to read any data. For more information on possible reasons, see udpport Read
Warnings.
Response received:
My Python script recived the folowing Data (blacked out with X):
Listening on 192.168.1.134:50001
Received from ('192.168.81.2', 50001):
]*IDN?#1Opsens Sol., GSX-G2-N-100LXX-XX-X, SN:92AAXXXX-XX, HRN:4.X.X-R, FRN:5.2X.0-XXFeb1X, FLN

请先登录,再进行评论。

回答(0 个)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by