How we will create a UDP packet in matlab. Does matlab have built in function or not. Kindly explain with details.
MATLAB: How we will create a UDP packet in Matlab
Instrument Control ToolboxNetworkpacketudp
Related Solutions
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 ObjectudpB = udp(ipA,portA,'LocalPort',portB);%%Connect to UDP Objectfopen(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:
The 256 value that that is observed here is actually the number of values read from the UDP object, not the number of bytes read. Since the bytes being read were interpreted as 'int16' values, each value being read consisted of 2 bytes (16 bits), so we are actually reading 2*256 = 512 bytes from the UDP object. Note that this can also be applied to the UDP sender: we are writing 1024 values each represented by 2-bytes, so our write operation is actually writing 2048 bytes.
The reason we are reading only 512 bytes instead of the expected 1024-byte-long datagram we expected is because the default OutputDatagramPacketSize is 512-bytes, so the UDP sender is only sending 512-byte datagrams. If we change this property:
>> udps.OutputDatagramPacketSize = 1024;
We will be able to read the expected 1024-byte packets.
For more information on how read and write operations work in the UDP interface, refer to the following documentation:
Best Answer