[Solved] Send/receive frame (octet) using MPLAB harmony

[Solved] Send/receive frame (octet) using MPLAB harmony

Postby Elinux » Fri Sep 28, 2018 1:23 pm

Hello,

I have a card PIC32 Ethernet Starter Kit II (PIC32MX795F512L) I use and MPLAB X IDE v4.20 with MPLAB Harmony v2_06.
I would like to create a client/server code to send/receive frames (bytes) between a Raspberry PI and my ESK II card. the Raspberry PI, the ESK II and my computer are connected on the same network with RJ45 cables using a switch.

To do this, I was advised to use MPLAB Harmony demo berkeley_tcp_server .

I created a client program with python on the RPI and I executed the berkeley_tcp_server project for ESK II, my python code allows to send 'Hello' to the same port as the ESK II, the berkeley_tcp_server program allows it to take what is received in the buffer and returned to the port .... so on the shell of python I get 'Hello'
 
I would like to send a data frame (bytes) for example ([0x7E, 0x18, 0x01, ...]) from the ESK II to the RPI.
 
:?: I tried to modify the code but I still receive errors, someone would have indications (codes, links, documentation ...)?
 
Here is the server code of the demo:

Code: Select all
  // Create a socket for this server to listen and accept connections on
            SOCKET tcpSkt = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
            if (tcpSkt == INVALID_SOCKET)
                return;
            appData.bsdServerSocket = (SOCKET) tcpSkt;

            appData.state = APP_BSD_BIND;
        }
            break;

        case APP_BSD_BIND:
        {
            // Bind socket to a local port
            struct sockaddr_in addr;
            int addrlen = sizeof (struct sockaddr_in);
            addr.sin_port = SERVER_PORT;
            addr.sin_addr.S_un.S_addr = IP_ADDR_ANY;
            if (bind(appData.bsdServerSocket, (struct sockaddr*) &addr, addrlen) == SOCKET_ERROR)
                return;

            appData.state = APP_BSD_LISTEN;
            // No break needed
        }
            break;

        case APP_BSD_LISTEN:
        {
            if (listen(appData.bsdServerSocket, MAX_CLIENT) == 0) {
                appData.state = APP_BSD_OPERATION;
                SYS_CONSOLE_PRINT("Waiting for Client Connection on port: %d\r\n", SERVER_PORT);
            }
        }
            break;
        case APP_BSD_OPERATION:
        {
            int length;
            struct sockaddr_in addRemote;
            int addrlen = sizeof (struct sockaddr_in);
            char buffer[15];
           

            for (i = 0; i < MAX_CLIENT; i++) {
                // Accept any pending connection requests, assuming we have a place to store the socket descriptor
                if (appData.ClientSock[i] == INVALID_SOCKET)
                    appData.ClientSock[i] = accept(appData.bsdServerSocket, (struct sockaddr*) &addRemote, &addrlen);

                // If this socket is not connected then no need to process anything
                if (appData.ClientSock[i] == INVALID_SOCKET)
                    continue;

                // For all connected sockets, receive and send back the data
                length = recv(appData.ClientSock[i], buffer, sizeof (buffer), 0);

                if (length > 0) {
                    buffer[length] = '\0';
                    send(appData.ClientSock[i], buffer, strlen(buffer), 0);
                }
                // else just wait for some more data
            }
        }
            break;
        default:
            break;
    }
}


Thank you all
Last edited by Elinux on Wed Oct 03, 2018 9:09 am, edited 1 time in total.
Elinux
 
Posts: 15
Joined: Mon Sep 03, 2018 9:39 am
Location: France

Re: Send/receive frame (octet) using MPLAB harmony

Postby Elinux » Wed Oct 03, 2018 9:07 am

Hello everyone,

The answers to my questions are on the same topic as this one I posted on the microchip forum, here is the link (https://www.microchip.com/forums/m1069568.aspx).
Elinux
 
Posts: 15
Joined: Mon Sep 03, 2018 9:39 am
Location: France


Return to PIC32 topics

Who is online

Users browsing this forum: No registered users and 14 guests