Akagami Shanks
Akagami Shanks

Reputation: 27

Sending Data over Ethernet UDP via lwIP Problem

I am trying to send messages using the lwIP library by UDP. I am using a TMS570LS3137 microprocessor. Since it is complicated to manage including's of lwIP I am using this example project by varying sys_main.c.

After I build the code below and debug, my while loop works but I don't see any UDP message when I try to see the output by WireShark.

What might be the problem here?

//version1
#include "lwip/udp.h"
#include <stdint.h>
#include <string.h> // For strlen
#include "lwip/init.h" // For lwip_init
#include "lwip/tcpip.h" // For sys_check_timeouts
#include "lwip/sys.h"
#include <stdio.h> // For printf

// Function to send a UDP message
void send_udp_message(struct udp_pcb *pcb) {
    struct pbuf *p;
    const char *message = "Hello, UDP!";

    // Create a pbuf to hold the message data
    p = pbuf_alloc(PBUF_TRANSPORT, strlen(message), PBUF_RAM);
    if (p != NULL) {
        // Copy the message data into the pbuf payload
        memcpy(p->payload, message, strlen(message));

        // Send the pbuf over the UDP connection
        udp_send(pcb, p);

        // Free the pbuf
        pbuf_free(p);
    }
}

void simple_delay(uint32_t milliseconds) {
    volatile uint32_t i, j;
    for (i = 0; i < milliseconds; i++) {
        for (j = 0; j < 4000; j++) {
            // Adjust the inner loop count based on your system's clock frequency
            __asm("nop");
        }
    }
}

int main() {
    struct udp_pcb *pcb;

    // Initialize lwIP
    lwip_init();

    // Create a new UDP pcb
    pcb = udp_new();

    int i = 0;

    if (pcb != NULL) {
        // Bind the UDP pcb to a local port
        udp_bind(pcb, IP_ADDR_ANY, 12345);

        // Set the remote IP address and port
        ip_addr_t remote_ip;
        IP4_ADDR(&remote_ip, 255, 255, 255, 255);
        udp_connect(pcb, &remote_ip, 54321);

        // Main loop
        while (1) {
            // Send the UDP message
            send_udp_message(pcb);

            // Main lwIP processing
            sys_check_timeouts();

            // Delay to control the rate of message sending
            simple_delay(1000);
            i++;
            printf("Cycle - %d\n", i);
        }
    }

    return 0;

}

I made some changes according to your response, but I still don't see any UDP message. Instead my microcontroller is sending this: "2 1.038562 fe80::1814:2da7:5472:f4e2 ff02::1:2 DHCPv6 148 Solicit XID: 0x62931f CID: 000100012d45818ac0b88323d67e "

//version2
#include "lwip/udp.h"
#include <stdint.h>
#include <string.h> // For strlen
#include "lwip/init.h" // For lwip_init
#include "lwip/tcpip.h" // For sys_check_timeouts
#include "lwip/sys.h"
#include "lwip/timers.h"
#include <stdio.h> // For printf

// Function to send a UDP message
void send_udp_message(struct udp_pcb *pcb) {
    struct pbuf *p;
    const char *message = "Hello, UDP!";

    // Create a pbuf to hold the message data
    p = pbuf_alloc(PBUF_TRANSPORT, strlen(message)+1, PBUF_RAM);
    if (p != NULL) {
        // Copy the message data into the pbuf payload
        memcpy(p->payload, message, strlen(message)+1);

        // Send the pbuf over the UDP connection
        udp_send(pcb, p);

        // Free the pbuf
        pbuf_free(p);
    }
}

void simple_delay(uint32_t milliseconds) {
    volatile uint32_t i, j;
    for (i = 0; i < milliseconds; i++) {
        for (j = 0; j < 4000; j++) {
            // Adjust the inner loop count based on your system's clock frequency
            __asm("");
        }
    }
}

int main() {
    struct udp_pcb *pcb;

    // Initialize lwIP
    lwip_init();

    // Create a new UDP pcb
    pcb = udp_new();

    int i = 0;

    if (pcb != NULL) {
        // Bind the UDP pcb to a local port
        udp_bind(pcb, IP_ADDR_ANY, 12345);

        // Set the remote IP address and port
        ip_addr_t remote_ip;
        IP4_ADDR(&remote_ip, 192, 168, 0, 87);
        udp_connect(pcb, &remote_ip, 54321);

        // Main loop
        while (1) {
            // Send the UDP message
            send_udp_message(pcb);

            // Main lwIP processing
            sys_check_timeouts();

            // Delay to control the rate of message sending
            simple_delay(1000);
            i++;
            printf("Cycle - %d\n", i);
        }
    }

    return 0;
}

Upvotes: 1

Views: 408

Answers (1)

Im Groot
Im Groot

Reputation: 645

There are a couple of thing that you should check here

  1. Is ping working fine?
  2. You have set remote ip address to 255.255.255.255, which is the broadcast address. You are connected to a specific device and for that you should send the message to specific ip address instead.
  3. Instead of using strlen(message) as length, use strlen(message) + 1 to include the null terminator.
  4. Check the firewall, if there is some blockage due to that.

Upvotes: 1

Related Questions