MATLAB: How to send data between two computers using the UDP function from the Instrument Control Toolbox

instrInstrument Control Toolboxiplinkpcstalktcptcp/iptcpip

I am using the Instrument Control Toolbox, and I would like to communicate between two computers using the User Datagram Protocol (UDP).

Best Answer

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.
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: