rt-thread+SAL+W5500 Ethernet Socket communication

rt-thread+SAL+W5500 Ethernet Socket communication
ORIGINAL POST
By None
components
Hardware Components
W5500
X 1
stm32f103
X 1
details

76f43d3d68cf3269daa702d180bf9486.png

Get firstrt-thread-master
enterstm32f103-fire-arbitraryThe bsp directory:

pkgs --upgradeFirst update the software source:

Select Wiznet in the online package:

Save and exit,pkgs --updateDownload package:

menuconfigReconfigure and enable SAL:

Comparing the bsp schematics, we know that INT on the development board is connected to pin 93, RST is connected to pin 132, and spi2 is used.


Modify hardware configuration:

To enable spi2:

There will be a warning when saving, just ignore it:

scons --target=mdk5Generate keil project, download and run:

DHCP timeout, because the PC is directly connected, there is no way to automatically assign IP.
Turn off DHCP and set static IP:


Regenerate the code and run it. Initialization successful:

Computer IP setting:

Computer pings:

The development board can ping the computer:

has data package:

PasteDemo in the rt-thread documentTo the project:

#include <stdio.h>
#include <string.h>

#include <rtthread.h>
#include <sys/socket.h> 
#include <netdb.h>

/* RT-Thread official website, support TLS function */
#define SAL_TLS_HOST    "www.rt-thread.org"
#define SAL_TLS_PORT    443
#define SAL_TLS_BUFSZ   1024

static const char *send_data = "GET /download/rt-thread.txt HTTP/1.1rn"
    "Host: www.rt-thread.orgrn"
    "User-Agent: rtthread/4.0.1 rttrnrn";

void sal_tls_test(void)
{
    int ret, i;
    char *recv_data;
    struct hostent *host;
    int sock = -1, bytes_received;
    struct sockaddr_in server_addr;

    /* Obtain the host address through the function entry parameter url (if it is a domain name, it will do domain name resolution) */
    host = gethostbyname(SAL_TLS_HOST);

    recv_data = rt_calloc(1, SAL_TLS_BUFSZ);
    if (recv_data == RT_NULL)
    {
        rt_kprintf("No memoryn");
        return;
    }

    /* Create a socket, the type is SOCKET_STREAM, TCP protocol, TLS type */
    if ((sock = socket(AF_INET, SOCK_STREAM, PROTOCOL_TLS)) < 0)
    {
        rt_kprintf("Socket errorn");
        goto __exit;
    }

    /* Initialize the pre-connected server address */
    server_addr.sin_family = AF_INET;
    server_addr.sin_port = htons(SAL_TLS_PORT);
    server_addr.sin_addr = *((struct in_addr *)host->h_addr);
    rt_memset(&(server_addr.sin_zero), 0, sizeof(server_addr.sin_zero));

    if (connect(sock, (struct sockaddr *)&server_addr, sizeof(struct sockaddr)) < 0)
    {
        rt_kprintf("Connect fail!n");
        goto __exit;
    }

    /* Send data to socket connection */
    ret = send(sock, send_data, strlen(send_data), 0);
    if (ret <= 0)
    {
        rt_kprintf("send error,close the socket.n");
        goto __exit;
    }

    /* Receive and print the response data, use encrypted data transmission */
    bytes_received = recv(sock, recv_data, SAL_TLS_BUFSZ  - 1, 0);
    if (bytes_received <= 0)
    {
        rt_kprintf("received error,close the socket.n");
        goto __exit;
    }

    rt_kprintf("recv data:n");
    for (i = 0; i < bytes_received; i++)
    {
        rt_kprintf("%c", recv_data[i]);
    }

__exit:
    if (recv_data)
        rt_free(recv_data);

    if (sock >= 0)
        closesocket(sock);
}

#ifdef FINSH_USING_MSH
#include <finsh.h>
MSH_CMD_EXPORT(sal_tls_test, SAL TLS function test);
#endif /* FINSH_USING_MSH */

Direct compilation will make an error, put the parameters herePROTOCOL_TLSJust modify it to 0. (The blogger found that this parameter was not used after debugging)

followAn online saying

With the address type and data transmission method, is it not enough to decide which protocol to use? Why is the third parameter needed?
As you might think, in general, you can create a socket with two parameters af and type, and the operating system will automatically deduce the protocol type, unless you encounter such a situation: There are two different protocols that support the same address type and data transmission type. If we do not specify which protocol to use, the operating system cannot automatically deduct it.
This tutorial uses IPv4 addresses, and the value of parameter af is PF_INET. If you use SOCK_STREAM to transmit data, the only protocol that meets these two conditions is TCP, so you can call the socket() function like this:
int tcp_socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); //IPPROTO_TCP means TCP protocol
This type of socket is called a TCP socket.
If you use the SOCK_DGRAM transmission method, the only protocol that meets these two conditions is UDP, so you can call the socket() function like this:
int udp_socket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); //IPPROTO_UDP means UDP protocol
This type of socket is called a UDP socket.
In the above two cases, only one protocol meets the conditions. You can set the value of protocol to 0, and the system will automatically deduce which protocol should be used, as follows:
int tcp_socket = socket(AF_INET, SOCK_STREAM, 0); //Create TCP socket
int udp_socket = socket(AF_INET, SOCK_DGRAM, 0); //Create UDP socket
This simplified notation is often used in subsequent tutorials.

The latter two parameters are jointly limited as TCP or UDP transmission, so just write 0 directly.
Modify the IP and PORT of HOST:

Recompile, download and run.
The host computer turns on Server monitoring:

MSH calls the test program:

The network debugging assistant received a message:

The network debugging assistant returns a message:

X-Shell received the message:

socket communication is complete!

76f43d3d68cf3269daa702d180bf9486.png

Get firstrt-thread-master
enterstm32f103-fire-arbitraryThe bsp directory:

pkgs --upgradeFirst update the software source:

Select Wiznet in the online package:

Save and exit,pkgs --updateDownload package:

menuconfigReconfigure and enable SAL:

Comparing the bsp schematics, we know that INT on the development board is connected to pin 93, RST is connected to pin 132, and spi2 is used.


Modify hardware configuration:

To enable spi2:

There will be a warning when saving, just ignore it:

scons --target=mdk5Generate keil project, download and run:

DHCP timeout, because the PC is directly connected, there is no way to automatically assign IP.
Turn off DHCP and set static IP:


Regenerate the code and run it. Initialization successful:

Computer IP setting:

Computer pings:

The development board can ping the computer:

has data package:

PasteDemo in the rt-thread documentTo the project:

#include <stdio.h>
#include <string.h>

#include <rtthread.h>
#include <sys/socket.h> 
#include <netdb.h>

/* RT-Thread official website, support TLS function */
#define SAL_TLS_HOST    "www.rt-thread.org"
#define SAL_TLS_PORT    443
#define SAL_TLS_BUFSZ   1024

static const char *send_data = "GET /download/rt-thread.txt HTTP/1.1rn"
    "Host: www.rt-thread.orgrn"
    "User-Agent: rtthread/4.0.1 rttrnrn";

void sal_tls_test(void)
{
    int ret, i;
    char *recv_data;
    struct hostent *host;
    int sock = -1, bytes_received;
    struct sockaddr_in server_addr;

    /* Obtain the host address through the function entry parameter url (if it is a domain name, it will do domain name resolution) */
    host = gethostbyname(SAL_TLS_HOST);

    recv_data = rt_calloc(1, SAL_TLS_BUFSZ);
    if (recv_data == RT_NULL)
    {
        rt_kprintf("No memoryn");
        return;
    }

    /* Create a socket, the type is SOCKET_STREAM, TCP protocol, TLS type */
    if ((sock = socket(AF_INET, SOCK_STREAM, PROTOCOL_TLS)) < 0)
    {
        rt_kprintf("Socket errorn");
        goto __exit;
    }

    /* Initialize the pre-connected server address */
    server_addr.sin_family = AF_INET;
    server_addr.sin_port = htons(SAL_TLS_PORT);
    server_addr.sin_addr = *((struct in_addr *)host->h_addr);
    rt_memset(&(server_addr.sin_zero), 0, sizeof(server_addr.sin_zero));

    if (connect(sock, (struct sockaddr *)&server_addr, sizeof(struct sockaddr)) < 0)
    {
        rt_kprintf("Connect fail!n");
        goto __exit;
    }

    /* Send data to socket connection */
    ret = send(sock, send_data, strlen(send_data), 0);
    if (ret <= 0)
    {
        rt_kprintf("send error,close the socket.n");
        goto __exit;
    }

    /* Receive and print the response data, use encrypted data transmission */
    bytes_received = recv(sock, recv_data, SAL_TLS_BUFSZ  - 1, 0);
    if (bytes_received <= 0)
    {
        rt_kprintf("received error,close the socket.n");
        goto __exit;
    }

    rt_kprintf("recv data:n");
    for (i = 0; i < bytes_received; i++)
    {
        rt_kprintf("%c", recv_data[i]);
    }

__exit:
    if (recv_data)
        rt_free(recv_data);

    if (sock >= 0)
        closesocket(sock);
}

#ifdef FINSH_USING_MSH
#include <finsh.h>
MSH_CMD_EXPORT(sal_tls_test, SAL TLS function test);
#endif /* FINSH_USING_MSH */

Direct compilation will make an error, put the parameters herePROTOCOL_TLSJust modify it to 0. (The blogger found that this parameter was not used after debugging)

followAn online saying

With the address type and data transmission method, is it not enough to decide which protocol to use? Why is the third parameter needed?
As you might think, in general, you can create a socket with two parameters af and type, and the operating system will automatically deduce the protocol type, unless you encounter such a situation: There are two different protocols that support the same address type and data transmission type. If we do not specify which protocol to use, the operating system cannot automatically deduct it.
This tutorial uses IPv4 addresses, and the value of parameter af is PF_INET. If you use SOCK_STREAM to transmit data, the only protocol that meets these two conditions is TCP, so you can call the socket() function like this:
int tcp_socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); //IPPROTO_TCP means TCP protocol
This type of socket is called a TCP socket.
If you use the SOCK_DGRAM transmission method, the only protocol that meets these two conditions is UDP, so you can call the socket() function like this:
int udp_socket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); //IPPROTO_UDP means UDP protocol
This type of socket is called a UDP socket.
In the above two cases, only one protocol meets the conditions. You can set the value of protocol to 0, and the system will automatically deduce which protocol should be used, as follows:
int tcp_socket = socket(AF_INET, SOCK_STREAM, 0); //Create TCP socket
int udp_socket = socket(AF_INET, SOCK_DGRAM, 0); //Create UDP socket
This simplified notation is often used in subsequent tutorials.

The latter two parameters are jointly limited as TCP or UDP transmission, so just write 0 directly.
Modify the IP and PORT of HOST:

Recompile, download and run.
The host computer turns on Server monitoring:

MSH calls the test program:

The network debugging assistant received a message:

The network debugging assistant returns a message:

X-Shell received the message:

socket communication is complete!

COMMENTS

Please Login to comment
  Subscribe  
Notify of
POSTED BY
Reusable S/W