UDP Client Socket Tutorial

From openPicus Wiki
Jump to: navigation, search

Contents

What you are going to learn

This tutorial will show you how to achieve a consistent UDP data exchange using UDP Client Socket with Flyport Wi-Fi and Ethernet Modules.

UDP Protocol

To have a short intro on UDP Protocol and how it works you can refer to this tutorial.

UDP Client Socket usage example: use Flyport as UDP Client and a PC as Server

Initial setup

To create a Simple UDP Client example you can start creating a new project for your Flyport Wi-fi or Ethernet. If needed, you can add more space to each UDP socket Buffer using the IDE Wizard. NOTE that at least one UDP Socket should be enabled to let this example code works properly!
Install the useful "TCP/IP Builder" software on your pc and open it! Fill params with your PC local network IP Address, and use port 5010 as port, choose UDP Protocol.
In this way you are creating a UDP socket that will be our UDP Server. Since we don't know UDP Port number that Flyport will use, this info will be printed on IDE Serial Monitor by firmware. You have to insert Flyport IP address and UDP Port to send back data using TCP/IP Builder!

Code example

In the following code example you can see user have to set Server IP and Port, since Flyport needs to know those info when opens a UDP Client connection.
Once Client is opened, int udpClientPort = UDPLocalPort(UdpSocket); reads socket number. Use this info to setup TCP/IP Builder. After PC Software is ready you can send data and see Flyport will read it and send it back!


#include "taskFlyport.h"
 
int UdpSocket = INVALID_SOCKET;
int UdpRxLength = 0;
char UdpString [512];
char servIp[] = "192.168.0.101";
char servPort[] = "41234";
 
void FlyportTask()
{
#ifdef FLYPORT_ETH
    while(!MACLinked);
#else
    // Flyport connects to default network
    WFConnect(WF_DEFAULT);
    while(WFGetStat() != CONNECTED);
#endif	
    vTaskDelay(25);
 
    _dbgline("Flyport connected... hello world!");
 
 
    // Open a UDP Client socket (need server IP Address and PORT)
    UdpSocket = UDPClientOpen(servIp, servPort);
 
    // Wait for DNS and ARP resolution:
    _dbgline("Waiting for DNS and ARP operations...");
    while(UDPisOpened(UdpSocket) == FALSE);
    _dbgline("Udp opened correctly");
 
    int udpClientPort = UDPLocalPort(UdpSocket);
    sprintf(UdpString, "Client port: %d", udpClientPort);
    _dbgline(UdpString);
 
    while(1)
    {
        // wait 0.5 sec
        vTaskDelay(50);
 
        if(UdpSocket == INVALID_SOCKET)
        {
            _dbgline("Socket not opened...");
        }
        else
        {
            // Check UDP Socket RX length
            UdpRxLength = UDPRxLen(UdpSocket);
 
            // Read data received
            if(UdpRxLength > 0)
            {
                sprintf(UdpString, "UDP Received: %d BYTES", UdpRxLength);
                _dbgline(UdpString);
                vTaskDelay(20);
                UDPRead(UdpSocket, UdpString, UdpRxLength);
                UdpString[UdpRxLength] = '\0';
                _dbgline(UdpString);
 
                // Send data back!
                // write a string via UDP!
                UDPWrite(UdpSocket, (BYTE*)UdpString, UdpRxLength);
            }
        }
    }
}
Personal tools
Namespaces

Variants
Actions
START HERE
DEVELOPMENT
HARDWARE INFO
RESOURCES
PHASED OUT
Toolbox