Cannot send UDP message with mbed


#1

Hi,
I’m using a stm32 nucleo 401RE board with a wiznet WIZ550IO (ioShield-A, with some wires mods for SPI) on the mbed platform with the WIZnetInterface library.
My code seems ok (no errors on init), but the the app sends always 0 Bytes (in this example it should send 8 bytes). What is the problem? Here the code:

#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);
        }
    }


#2

I found the error. You should use udp.bind() instead of udp.init(). That is wiznet specific and differs from the
using of the mbed ethernet library, where an udp port doesn’t need to bind.