7 years, 11 months ago.

Cannot send UDP string with wiznet 5500

I'm trying to send udp messages with nucleo 401RE and a wiznet WIZ550IO (uses the w5500). It seems that my code works fine, but there only sending 0 bytes. What is wrong?

#include "mbed.h"
#include "EthernetInterface.h"

Serial dbg(USBTX, USBRX);

// variables
uint8_t mac[6] = {0x00, 0x08, 0xDC, 0x1C, 0xAA, 0x06};
char IP_ADDRESS[] = "10.101.1.200";
char IP_ADDRESS_MASK[] = "255.255.0.0";
char IP_ADDRESS_GATEWAY[] = "10.101.1.1";
char IP_ADDRESS_OSC_TX[] = "10.101.1.100";
int OSC_UDP_PORT_TX = 54000;
char oscMessage[64];



// ethernet constructors
SPI spi(SPI_MOSI, SPI_MISO, SPI_SCK);
EthernetInterface eth(&spi, SPI_CS, PA_9); // mosi, miso, sclk, cs, reset
UDPSocket udp;
Endpoint consoleUDP;


int main() {
    spi.format(8,0); // 8bit, mode 0
    spi.frequency(7000000); // 7MHz
    wait(1); // 1 second for stable state
    // ethernet init
    eth.init(mac, IP_ADDRESS, IP_ADDRESS_MASK, IP_ADDRESS_GATEWAY);
    if(eth.connect() == -1) {
        printf("Ethernet init failed \n");
        }
    else {
        printf("Ethernet init ok \n");
        printf("IP Address is %s\n", eth.getIPAddress());
        printf("IP Subnet is %s\n", eth.getNetworkMask());
        printf("IP Gateway is %s\n", eth.getGateway());
        }
    // osc ethernet settings
    if(udp.init() == -1) {
        printf("UDP init failed \n");
        }
    else {
        printf("UDP init ok \n");
        }
    if(consoleUDP.set_address(IP_ADDRESS_OSC_TX, OSC_UDP_PORT_TX) == -1) {
        printf("Endpoint init failed \n");
        }
    else {
        printf("Endpoint init ok \n");
        printf("Endpoint IP Console %s\n", consoleUDP.get_address());
        printf("Endpoint Port %i\n", consoleUDP.get_port());
        }


    while(1) {
        strcpy(oscMessage, "/eos/go0");
        printf("OSC Message: %s \n", oscMessage);
        printf("OSC Message Length: %i \n", strlen(oscMessage));
        int sendBytes = udp.sendTo(consoleUDP ,oscMessage, strlen(oscMessage));
        if(sendBytes == -1) {
            printf("UDP sending failded \n");
            }
        else {
            printf("UDP sends %i bytes\n", sendBytes);
            }
        wait(1);
        }
    }

Found the error. You have to use udp.bind() instead udp.init(). That is wiznet specific, a socket must! bind before you can use it.

posted by Stefan Staub 02 Dec 2016
Be the first to answer this question.