How do I send data between two computers using the UDP function from the Instrument Control Toolbox?
87 次查看(过去 30 天)
显示 更早的评论
MathWorks Support Team
2012-9-10
编辑: MathWorks Support Team
2023-5-22
I am using the Instrument Control Toolbox, and I would like to communicate between two computers using the User Datagram Protocol (UDP).
采纳的回答
MathWorks Support Team
2023-5-22
编辑:MathWorks Support Team
2023-5-22
By creating and customizing a UDP object on each computer, you can configure each object to communicate to the other through data reading and writing commands. Keep in mind that UDP protocol does not guarantee transmission or order of the packets.
The following example shows how to communicate between two computers, referred to as 'machineA' and 'machineB' using the UDP function of the Instrument Control Toolbox. In this example, the IP addresses of machineA and machineB are 144.212.206.23 and 144.212.206.178, respectively. The ports used by these computers are 9090 and 9091, respectively.
Step 1. Configure Machine A
On the first machine, issue the following commands at the MATLAB command prompt:
%% Define computer-specific variables
ipA = '144.212.206.23'; portA = 9090; % Modify these values to be those of your first computer.
ipB = '144.212.206.178'; portB = 9091; % Modify these values to be those of your second computer.
%% Create UDP Object
udpA = udp(ipB,portB,'LocalPort',portA);
%% Connect to UDP Object
fopen(udpA)
Step 2. Configure Machine B
On the second machine, issue the following commands at the MATLAB command prompt:
%% Define computer-specific variables
% Modify these values to be those of your first computer:
ipA = '144.212.206.23'; portA = 9090;
% Modify these values to be those of your second computer:
ipB = '144.212.206.178'; portB = 9091;
%% Create UDP Object
udpB = udp(ipA,portA,'LocalPort',portB);
%% Connect to UDP Object
fopen(udpB)
Both computers are now configured for communicating with one another via UDP. We will continue this example, however, to show how a simple communication may progress.
Step 3. Send messages from Machine A to Machine B.
On the first machine, issue the following commands at the MATLAB command prompt:
fprintf(udpA,'This is test message number one.')
fprintf(udpA,'This is test message number two.')
fprintf(udpA,'doremifasolatido')
Step 4. Retrieve messages on Machine B from Machine A.
On the second machine, issue the following commands at the MATLAB command prompt:
fscanf(udpB)
Note that only the first message is retrieved:
ans =
This is test message number one.
Now, retrieve the second message by issuing the same command at the MATLAB command prompt:
fscanf(udpB)
Here again, only the second message is retrieved:
ans =
This is test message number two.
Now, let us retrieve the third message -- only this time, we will retrieve it two characters at a time. To do this, we will need to set the 'DatagramTerminateMode' property to 'off'.
On the second machine, issue the following commands at the MATLAB command prompt:
set(udpB,'DatagramTerminateMode','off')
for notes=1:8, fscanf(udpB,'%c',2), end
Your output should look like the following:
ans =
do
ans =
re
ans =
mi
ans =
fa
ans =
so
ans =
la
ans =
ti
ans =
do
Step 5. Clean Up Machine A
On the first machine, issue the following commands at the MATLAB command prompt:
%% Clean Up Machine A
fclose(udpA)
delete(udpA)
clear ipA portA ipB portB udpA
Step 6. Clean Up Machine B
On the second machine, issue the following commands at the MATLAB command prompt:
%% Clean Up Machine B
fclose(udpB)
delete(udpB)
clear ipA portA ipB portB udpB notes
For more information, see the "Controlling Instruments Using TCP/IP and UDP" section of the Instrument Control Toolbox documentation at:
1 个评论
Walter Roberson
2016-10-21
Nabeel Afridi, you can fwrite() the array in binary, or you can fprintf() the array. The code above shows a string being sent, and strings are arrays (of characters.) If the array is not fixed size you might be wanting to send the size information just before you send the array.
更多回答(1 个)
Vinod
2014-4-14
编辑:MathWorks Support Team
2023-4-27
The UDP protocol does not guarantee transmission or order of the packets.
For most applications, you probably want to use TCPIP, like this:
1 个评论
Walter Roberson
2014-4-14
Real-time communications require UDP rather than TCP, as TCP has no upper bound on packet delivery time and does not allow later packets to be delivered.
TCP is also not usable for broadcasting of any kind.
Effectively all other IP protocols other than TCP are unidirectional, so there are a lot of different applications where in-order guaranteed delivery is not suitable.
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!