2 years, 7 months ago.

UDP server no respons

Made the UDP server using the "UIPEthernet" library and enc28j60. The code is compiled, the server pings, but when I send a request from the Python client, the answer does not come. It seems that "parsePacket ()" does not work. What's my mistake?


#include "mbed.h"
#include "UIPEthernet.h" 
UIPEthernetClass Ethernet(D11, D12, D13, D10); //mosi, miso, sck, cs

int main(void) {
    EthernetUDP udp;
    const uint8_t MY_MAC[6] = { 1, 2, 3, 4,5, 6 };
    const uint16_t MY_PORT = 10001; 
    const IPAddress MY_IP(192, 168,1, 33); 

    Ethernet.begin(MY_MAC, MY_IP); int success = udp.begin(MY_PORT);

    while(1) {

        int size= udp.parsePacket();
        while ((size= udp.available())>0) {
            char* msg = (char*)malloc( size+1);
            int len = udp.read(msg, size+1); 
        int success;
        while (!success) {
            success = udp.beginPacket(udp.remoteIP(),udp.remotePort());
        char* acknowledge = "Data"; 
        success = udp.write((uint8_t*)acknowledge, strlen(acknowledge));
        success = udp.endPacket(); 

1 Answer

2 years, 7 months ago.

Hello Aleksey,

I ran the UDP server code below on a NUCLEO-F103RB + ENC28J60:

#include "mbed.h"
#include "UIPEthernet.h"
#include "UIPUdp.h"

Serial              pc(USBTX, USBRX);

UIPEthernetClass    UIPEthernet(D11, D12, D13, D10);    // mosi, miso, sck, cs

// MAC number must be unique within the connected network. Modify as appropriate.
const uint8_t       MY_MAC[6] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05 };
const uint16_t      MY_PORT = 7;
UIPUDP              udp;

 * @brief
 * @note
 * @param
 * @retval
int main(void) {

    // IP address must be unique and compatible with your network.
    const IPAddress MY_IP(192, 168, 1, 181);

    UIPEthernet.begin(MY_MAC, MY_IP);

    IPAddress   localIP = UIPEthernet.localIP();
    pc.printf("Local IP = ");
    for(uint8_t i = 0; i < 3; i++)
        pc.printf("%d.", localIP[i]);
    pc.printf("%d\r\n", localIP[3]);

    pc.printf("Initialization ");

    while(1) {
        int size = udp.parsePacket();
        if(size > 0) {
            do {
                char*   msg = (char*)malloc(size + 1);
                int     len = udp.read(msg, size + 1);
                msg[len] = 0;
            } while((size = udp.available()) > 0);

            //finish reading this packet:

            int success;
            do {
                //send new packet back to ip/port of client. This also
                //configures the current connection to ignore packets from
                //other clients!
                success = udp.beginPacket(udp.remoteIP(), udp.remotePort());
                    pc.printf("beginPacket: succeeded%\r\n");
                    pc.printf("beginPacket: failed%\r\n");

                //beginPacket fails if remote ethaddr is unknown. In this case an
                //arp-request is send out first and beginPacket succeeds as soon
                //the arp-response is received.
            } while(!success);

            char*   message = "Hello World from mbed";
            success = udp.write((uint8_t*)message, strlen(message));

                pc.printf("bytes written: %d\r\n", success);

            success = udp.endPacket();

                pc.printf("endPacket: succeeded%\r\n");
                pc.printf("endPacket: failed%\r\n");


            //restart with new connection to receive packets from other clients
                pc.printf("restart connection: succeeded%\r\n");
                pc.printf("restart connection: failed%\r\n");

Then I used the following UDP client, running on a LPC1768, to send it UDP messages:

/*  Ethernet UDP Client (to be used with Ethernet_UDP_server)                         */
/*  NXP         mareikeFSL  2015.12.23      rev 1.0     initial                       */
/*                                                                                    */
/*  This "Hello World" program is used in conjunction with the Ethernet_UDP_server    */
/*  program. It communicates between two FRDM-K64F boards via the Ethernet protocol.  */
/*  To use this program, you need to do the following:                                */
/*      - Connect an Ethernet cable between two FRDM-K64F boards (a crossover cable   */
/*        is not required).                                                           */
/*      - Flash one board with Ethernet_UDP_client and the other with                 */
/*        Ethernet_UDP_server                                                         */
/*      - [optional] If you would like to see the "Hello World" output on your        */
/*        monitor, install and open a terminal. Tera Term is used in the Wiki for     */
/*        this program.                                                               */
#include "mbed.h"
#include "EthernetInterface.h"
const int PORT = 7;
static const char* SERVER_IP = ""; //IP of server board
static const char* CLIENT_IP = ""; //IP of client board
static const char* MASK = "";      //mask
static const char* GATEWAY = "";     //gateway
Serial pc(USBTX, USBRX);        //create PC interface
EthernetInterface eth;          //create ethernet
UDPSocket sock;                 //creat Ethernet socket
Endpoint server;                //create endpoint
DigitalOut red(LED1);        //debug led
DigitalOut green(LED2);    //debug led
int n;                  //size of received message
char in_buffer[1];      //create receive buffer
char counter[1] = {0};  //sample send buffer
/*--FUNCTION DECLARATIONS---------------------------------------------------------------*/
void init_usb(void);    //initializes pc.printf if required
void init_eth(void);    //initializes Ethernet
void end_eth(void);     //closes Ethernet socket
int main(void);         //main
/*--FUNCTION DEFINITIONS----------------------------------------------------------------*/
void init_usb(void)
    pc.baud(9600);    //baud
}   //end init_usb()
void init_eth(void)
    eth.init(CLIENT_IP, MASK, GATEWAY);                                         //set up IP
    eth.connect();                                                              //connect ethernet
    pc.printf("\nCLIENT - Client IP Address is %s\r\n", eth.getIPAddress());    //get client IP address
    sock.init();                                                                //initialize socket
    server.set_address(SERVER_IP, PORT);                                        //set address of server
}   //end init_eth()
void end_eth(void)
    sock.close();       //close socket
    eth.disconnect();   //close Ethernet
}   //end end_eth()
int main(void)
    red = 0;                                                                                //client
    green = 1;
    init_usb();                                                                             //initialize the PC interface
    init_eth();                                                                             //initialize the Ethernet connection
    while(true)                                                                             //repeat forever
        pc.printf("\nCLIENT - Sending '%i' to server %s\r\n", counter[0], SERVER_IP);       //print message to send
        sock.sendTo(server, counter, sizeof(counter));                                      //send message
        pc.printf("CLIENT - Waiting for UDP packet...\r\n");                                //wait for message
        n = sock.receiveFrom(server, in_buffer, sizeof(in_buffer));                         //receive message from server
        in_buffer[n] = '\0';                                                                //add \0 to end of message
        pc.printf("CLIENT - Received '%i' from server %s\r\n", in_buffer[0], SERVER_IP);    //print message received
        counter[0] = (counter[0] + 1)%11;                                                   //only count up to 10, then reset to 0
        wait(1);                                                                            //wait 1 second
}   //end main()

and everything worked fine. So it seems that there is some issue with the Python application you used.

Accepted Answer

Thank you very much, Zoltan. I ran your code and the server was working.

posted by Aleksey Pashnin 27 Jun 2017