HAPSRG / Mbed OS UDP_Client_0003

Dependencies:   UsaClient

main.cpp

Committer:
cocorlow
Date:
2021-11-06
Revision:
2:1b4bba79cd29
Parent:
1:c050a4297deb

File content as of revision 2:1b4bba79cd29:

#include "mbed.h"
#include "EthernetInterface.h"
#include "UsaClient.hpp"

EthernetInterface net;
DigitalIn button(PC_13);

using namespace std::chrono;

int main()
{
    printf("system start.\r\n");
    SocketAddress sockAddr;
    if (net.connect() != 0)
    {
        printf("a");
        return -1;
    }
    net.get_ip_address(&sockAddr);
    printf("IP: %s\r\n", sockAddr.get_ip_address() ? sockAddr.get_ip_address() : "No IP");
    
    
    UsaClient udp(&net, "192.168.10.109", 10000); // server IP address, Port number
    uint8_t send[200];
    uint8_t recv[200];
    
    int wN = 100;
    int rN = 100;
    
    Timer t;
    
    while (1)
    {
        if (button)
        {
            printf("\r\n##########################\r\n");
            printf("write start\r\n");
            printf("wN = %d\r\n", wN);
            t.reset();
            t.start();
            for (int i = 0; i < wN; i++)
            {   
                send[0] = i;
                udp.Write(0, send, 100);
            }
            t.stop();
            printf("write: %llu us\r\n", duration_cast<microseconds>(t.elapsed_time()).count());
            
            ThisThread::sleep_for(chrono::milliseconds(1000));
            
            printf("read start\r\n");
            printf("r = %d\r\n", wN);
            t.reset();
            t.start();
            for (int i = 0; i < rN; i++)
            {
                udp.Read(0, recv, 100);
            }
            t.stop();
            printf("%d\r\n", recv[0]);
            printf("read : %llu us\r\n", duration_cast<microseconds>(t.elapsed_time()).count());
            ThisThread::sleep_for(chrono::milliseconds(1000));
        }
    }
}