Exchange Ethernet Data by Using UDP Blocks
The Simulink® Coder™ Support Package for NXP™ FRDM-K64F Board provides UDP Send and UDP Receive blocks so that you can exchange User Datagram Protocol (UDP) data between your development computer and the target hardware.
This example shows how to use UDP blocks to exchange data between your development computer and the target hardware via Ethernet communication.
The UDP target model that runs on the NXP FRDM-K64F board receives UDP data from your development computer. The model then sends the data back to your development computer. A companion UDP host model running on your development computer receives the data sent by the NXP FRDM-K64F board.
Configure Network Settings for NXP FRDM-K64F Ethernet Resource
Configure the target hardware for Ethernet communication with your development computer.
To configure the network settings for the NXP FRDM-K64F Ethernet resource:
Connect the NXP FRDM-K64F board to your computer with a micro USB cable.
In the Simulink Editor, select Simulation > Model Configuration Parameters.
In the Configuration Parameters dialog box, click Hardware Implementation.
Set the Hardware board parameter to
NXP FRDM-K64F
.Under Target Hardware Resources, select
Ethernet
.Specify the MAC (Media Access Control) address, the physical network address for the board in MAC address.
When you specify the Ethernet MAC address, follow these guidelines:
The MAC address must be in the six octet format. For example, DE:AD:BE:EF:FE:ED.
If the board is connected to a router, the MAC address must not be in the filtered list for that router.
The NXP FRDM-K64F board, by default, gets its Internet Protocol (IP) address through Dynamic Host Configuration Protocol (DHCP). This workflow is compatible when the development computer and the board are connected to a router.
Make sure that the network to which your board connects supports DHCP for IP address assignment.
Alternatively, you can specify a static IP address to the board by using the Board IP address parameter. If you connect the board to your development computer with the Ethernet cable, assign the board with a static IP address.
When you specify the IP address, follow these guidelines:
The IP address must be in the four octet format. For example, 192.168.0.20.
When connected to a development computer, the IP address of the board and the IP address of the Ethernet interface on the development computer must be in the same subnet. Only then can they communicate with each other.
However, make sure that the IP addresses are different.
When connected to a router, the IP address must be in the valid range of the supported IP addresses and must not be in the filtered list for that router.
For more information on the configuration parameters, see Model Configuration Parameters for NXP FRDM-K64F Board.
Configure UDP Host and Target Model
Configure the UDP host and the target model for your development computer to exchange UDP data with the target model.
UDP Host Model
The UDP host model is configured to send an array of 10 bytes of UDP data to the target hardware. The model is also configured to receive data sent by the hardware.
The UDP Send and Receive blocks used in the model are in the Instrument Control Toolbox™ library.
To configure the UDP host model that runs on your development computer:
Create the UDP host model.
To open the Block Parameters dialog box, double-click the UDP blocks.
Set the block parameter values to the values listed in this table.
Block Parameter Name Value UDP Send block Remote address IP address of the target hardware.
In this example, the IP address is
192.168.1.102
.Port 34567
UDP Receive block Remote address IP address of the target hardware.
In this example, the IP address is
192.168.1.102
.Local port 51001
The IP address of the target hardware in the Remote address parameter of UDP blocks and the board address that you specify in Simulation > Model Configuration Parameters > Hardware Implementation > Ethernet > Board IP address must be same.
For more information on how to specify the board IP address, see Configure Network Settings for NXP FRDM-K64F Ethernet Resource.
To save and close the Block Parameter dialog box, click OK.
UDP Target Model
The UDP target model is configured to receive an array of 10 bytes of UDP data and send the data back to your development computer.
The UDP Receive and UDP Send used in the model are in the Simulink Coder Support Package for NXP FRDM-K64F Board library.
To configure the UDP target model that runs on the target hardware:
Create the UDP target model.
To open the Block Parameters dialog box, double-click the UDP blocks.
Set the block parameter values to the values listed in the table.
Block Parameter Name Value UDP Receive block Local IP Port 34567
Data type uint8
Data size (N) 10
UDP Send block Remote IP address IP address of the Ethernet interface on the development computer.
In this example, the IP address is
192.168.1.103
.Remote IP Port 51001
Local IP Port 51000
For more information on the UDP block parameters, see UDP Receive and UDP Send.
To save and close the Block Parameter dialog box, click OK.
Run UDP Host and Target Model
To run the UDP host model and the UDP target model:
Configure the network properties of the model as described in Configure Network Settings for NXP FRDM-K64F Ethernet Resource.
Configure the UDP host model as described in UDP Host Model.
For the development computer to send data to the target hardware, the remote port number of the host UDP Send block and the local port number of the target UDP Receive block must be same. In this case, the Port of the host UDP Send block and the Local IP Port of the target UDP Receive block is
34567
.Configure the UDP target model as described in UDP Target Model.
For the target hardware to send data to your development computer, the remote port number of the target UDP Send block and the local port number of the host UDP Receive block must be same. In this case, the Remote IP Port of the target UDP Send block and the Local IP Port of the host UDP Receive block is
51001
.In the target model, click the Build Model button on the toolbar or press Ctrl+B.
As the target model is deployed on the target hardware, the hardware starts receiving data on port
34567
and it starts sending data to port51001
of your development computer.In the host model, set the Simulation mode on the toolbar to
Normal
.On the toolbar, click the Run button.
As the model simulates, your development computer sends an array of 10 bytes of data to port
34567
of the target hardware. The hardware receives the data and sends it back to port51001
of your development computer. Observe the result on the Display block.
As the host model runs on the development computer in the normal mode, it does not consider the Sample time parameter value specified in the Block Parameters dialog box. The model runs as fast as it can, given the presence of operating system processes.
The real-time execution speed of the model can potentially be slower than the simulation speed of the model in normal mode. Due to the difference in the sample time, the model might not run properly in real time.
See Also
UDP Receive | UDP Send | Model Configuration Parameters for NXP FRDM-K64F Board