UDP Send/Receive Example
A NetBurner embedded application that demonstrates bidirectional UDP communication between a NetBurner device and a host computer or another networked device.
Overview
This application creates a UDP server that can simultaneously send and receive UDP packets on the same port. It features a multi-threaded design with one thread dedicated to receiving packets and displaying them via the debug serial port, while the main thread handles user input and sends UDP packets to a specified destination.
Features
- Bidirectional UDP Communication: Send and receive UDP packets on the same port
- Multi-threaded Design: Separate receive thread for non-blocking packet handling
- Interactive Configuration: Runtime configuration of port number and destination IP
- Debug Output: Received packets displayed with hex dump formatting
- DHCP Support: Automatic network configuration with DHCP
Hardware Requirements
- NetBurner embedded device with network capability
- Serial connection to PC for debugging and user interaction
- Network connection (Ethernet)
Software Requirements
- NetBurner NNDK (NetBurner Network Development Kit)
- Terminal program (such as MTTTY) for serial communication
- UDP client application on PC (such as NetBurner UDP Terminal)
How It Works
Application Flow
- Initialization: Network stack initializes and waits for DHCP address assignment
- Configuration: User prompted to enter:
- UDP port number (used for both sending and receiving)
- Destination IP address for outgoing packets
- Task Creation: Separate receive task created with higher priority than main task
- Operation:
- Main thread waits for user input and sends UDP packets
- Receive thread continuously monitors for incoming packets
Threading Architecture
- Main Thread: Handles user input from serial terminal and sends UDP packets
- Receive Thread: Listens for incoming UDP packets using OS_FIFO mechanism
- Priority: Receive thread runs at higher priority than main thread
Usage
Setup
- Connect NetBurner device to network
- Connect serial cable between PC and NetBurner debug port
- Open terminal program (MTTTY) and connect to serial port
- Compile and flash the application to the NetBurner device
Operation
- Start Application: Reset or power on the NetBurner device
- Wait for Network: Application waits up to 5 seconds for DHCP address
- Configure Port: Enter the UDP port number when prompted
- Configure Destination: Enter the destination IP address
- Send Data: Type messages and press Enter to send UDP packets
- Receive Data: Incoming packets automatically displayed with source IP and hex dump
Example Session
Application: UDP Send/Receive Example
NNDK Revision: [Version]
Enter the UDP port number (will be used for send & receive): 1234
Enter the destination IP Address: 192.168.1.100
Listening/Sending on UDP Port 1234, Sending to IP address: 192.168.1.100
Enter data and hit return to send.
Hello World
Sent "Hello World" to 192.168.1.100:1234
Received UDP packet with 12 bytes from: 192.168.1.100
48 65 6C 6C 6F 20 57 6F 72 6C 64 00 Hello World.
Key Components
UDP Packet Handling
- Uses NetBurner UDP C++ class for packet management
- OS_FIFO mechanism for packet queuing
- Automatic packet validation and parsing
Network Functions
RegisterUDPFifo(): Associates FIFO with UDP port for packet reception
UDPPacket: Class for constructing and sending UDP packets
AsciiToIp(): Converts string IP address to IPADDR format
System Functions
OSTaskCreatewName(): Creates named threads with specified stack and priority
WaitForActiveNetwork(): Blocks until network interface is ready
ShowData(): Utility function for hex dump display
Configuration Options
- Port Number: Any valid UDP port (1-65535)
- Destination IP: Any reachable IP address on the network
- Timeout: Receive timeout (currently set to wait forever)
- Stack Size: Configurable via USER_TASK_STK_SIZE
Testing
Use the NetBurner UDP Terminal application on your PC:
- Set the IP address to match your NetBurner device
- Set the port number to match your configuration
- Send messages from both PC and NetBurner device
- Observe bidirectional communication
Notes
- Application uses the same port for both sending and receiving
- Received packets include source IP address identification
- System diagnostics are enabled (consider removing for production)
- DHCP timeout is set to 5 seconds
- Input buffer limited to 80 characters