7 years, 8 months ago.

Udp Receive with nucleo_f767zi

I am using the new Network Stack with Mbed OS 5.1. I have changed the target.json file and the STF7 Hal file for the f767zi to allow ipv4.

It looks like broadcast is not yet supported because when I change the socket options I get a NSAPI_ERROR_UNSUPPORTED error.

So I tried to set up a p2p communication, I can send a string to a python program. When I try to send it back to the f767zi, it just block indefinitely. I have wiresharked the communication and I can see the data come into the PC and go back out so I am unsure what is going wrong.

I will test with TCP just to see if the behavior is the same.

Regards, Seth

PS when will broadcast be available? 5.2

TCP send and receive both work.

posted by Seth King 12 Aug 2016

I got it working. The port and the ip address in the Socket Address did not match where the data was coming from so I left it blank and set the bind to the port and that worked.

posted by Seth King 13 Aug 2016

1 Answer

Seth King
poster
7 years, 8 months ago.

Transmit/Receive with UDP and RTOS

#include "mbed.h"
#include "lwip-interface/EthernetInterface.h"

#include <string>
DigitalOut led1(LED1);


using std::string;
const int BROADCAST_PORT_T = 58080;
const int BROADCAST_PORT_R = 58081;
EthernetInterface eth;

void transmit()
{
    UDPSocket socket(&eth);
    string out_buffer = "very important data";
    SocketAddress transmit("192.168.1.159", BROADCAST_PORT_T);

    while (true)
    {
        int ret = socket.sendto(transmit, out_buffer.c_str(), out_buffer.size());
        printf("sendto return: %d\n", ret);

        Thread::wait(5000);
    }
}

void receive()
{
    SocketAddress receive;
    UDPSocket socket(&eth);
    int bind = socket.bind(BROADCAST_PORT_R);
    printf("bind return: %d\n", bind);

    char buffer[256];
    while (true)
    {
        printf("\nWait for packet...\n");
        int n = socket.recvfrom(&receive, buffer, sizeof(buffer));
        buffer[n] = '\0';
        printf("Packet from \"%s\": %s\n", receive.get_ip_address(), buffer);

        Thread::wait(500);
    }
}

int main()
{
    Thread transmitter;
    Thread receiver;
    eth.connect();

    printf("Controller IP Address is %s\r\n", eth.get_ip_address());
    Thread::wait(5000);

    transmitter.start(transmit);
    receiver.start(receive);

    while (true)
    {
        led1 = !led1;
        Thread::wait(500);
    }
}


Assigned to Seth King 7 years, 8 months ago.

This means that the question has been accepted and is being worked on.