1 month, 2 weeks ago.

Ethernet connection Ublox c027

Hi

I want to establish a n ethernet connection between my PC and my Ublox C027. I don't want to use the CELLULAR_NETWORK.

Could you please help me by providing any example code about this.

My code is

  1. include "mbed.h"
  2. include "rtos.h"
  3. include "EthernetInterface.h"
  4. include<string> #include "lwip-interface/EthernetInterface.h"

Serial pc(USBTX, USBRX); between the mbed and the PC.

Time protocol implementation : Address: time.nist.gov UDPPort: 37

typedef struct { uint32_t secs; Transmit Time-stamp seconds. }ntp_packet;

const int PORT = 4466;

static const char* mbedIP = "10.5.255.99"; IP static const char* mbedMask = "255.255.248.0"; Mask static const char* mbedGateway = "10.5.248.1"; Gateway

static const char* recvIP = "10.5.248.2";

Creating Ethernet object EthernetInterface eth;

void transmitter(){

Buffer for output messages const char out_buffer[] = "very important data";

address of destination ip address and port SocketAddress td_addr(recvIP, PORT); UDPSocket td_sock(&eth);

Loop to send data to Socket address "td_sock" while(true) { int ret = td_sock.sendto(td_addr, &out_buffer, sizeof(out_buffer));

Thread::wait(500); } }

int main() {

pc.baud(9600); Thread Transmitt;

Transmitt.start(transmitter);

eth.set_network(mbedIP, mbedMask, mbedGateway); my device address

printf("UDP Socket example\n"); eth.init(); DHCP if (ethernet.connect() != 0) { printf("UDP Error\n"); return -1; }

receive.start(receiver);

while(true) {

Thread::wait(10000);

} }

Am using a c# UDP server on my laptop.

Your help will really be appreciated

Thank you

Nada.

Hello Nada,

Try to click on the Edit button located in the top-right corner and then enclose your source code with <<code>> and <</code>> tags, each on separate line, like:

<<code>>
Your source
code.
<</code>>

After that, click on the Save Question button and it will be displayed as formatted code. Then it can be easily copy & pasted by others into the online compiler for testing.

posted by Zoltan Hudak 26 Oct 2019
Comment on this question

1 Answer

1 month, 1 week ago.

Hello Nada,

See below an example which could help:

#include "mbed.h"
#include "EthernetInterface.h"
#include <string>

Serial              pc(USBTX, USBRX);           //Serial connection with the PC.
static const char*  mbedIP = "10.5.248.99";     //Static IP address of this mbed board
static const char*  mbedMask = "255.255.248.0"; //Mask
static const char*  mbedGateway = "10.5.248.1"; //Gateway
static const char*  recvIP = "10.5.248.2";      //IP address of the C# UDP server running on the PC
const int           PORT = 4466;                //Port used for UDP communication
EthernetInterface   eth;                        //Ethernet interface
UDPSocket*          td_sock;                    //Pointer to mbed's UDP Socket
SocketAddress       td_addr(recvIP, PORT);      //Socket address of destination UDP server
Thread              transmit;                   //Thread for transmission
Thread              receive;                    //Thread for reception

void transmitter()
{
    //Buffer for outgoing messages
    const char  out_buffer[] = "very important data";

    //Loop to send data to Socket address "td_sock"
    while (true) {
        if (td_sock->sendto(td_addr, out_buffer, sizeof(out_buffer)) < 0) {
            printf("Error sending data.\r\n");
        }

        ThisThread::sleep_for(500);
    }
}

void receiver()
{
    //Buffer for incoming messages
    char    in_buffer[256];

    while (true) {
        if (int n = td_sock->recvfrom (&td_addr, in_buffer, sizeof (in_buffer)) > 0) {
            pc.printf("%d bytes received:\r\n", n);
            for (int i = 0; i < n; i++) {
                pc.printf("0x%.2X ", in_buffer[i]);
            }
            pc.printf("\r\n");
        }
    }
}

int main()
{
    pc.baud(9600);

    eth.set_network(mbedIP, mbedMask, mbedGateway);
    printf("UDP Socket example\n");
    if (eth.connect() != 0) {
        printf("Unable to connect to the Ethernet.\r\n");
        return -1;
    }

    // Show local address
    const char*     ip = eth.get_ip_address();
    printf("Local IP address: %s\n", ip ? ip : "No IP");

    td_sock = new UDPSocket;        //Create a UDP socket object
    if (td_sock->open(&eth) != 0) {
        printf("Cannot open the UDP socket.\r\n");
        return -1;
    }

    receive.start(receiver);        //Start receiver task in the receive thread
    transmit.start(transmitter);    //Start transmitter task in the transmit thread
    while (true) { }
}

For more info have a look at https://os.mbed.com/docs/mbed-os/v5.14/apis/udpsocket.html.

Thank you Zoltan.

I figure out what the issue. It's only adding a thread sleep to give time for establishing the connection.

Thank you for your help

posted by Nada Ali 27 Oct 2019

You need to log in to post a question