# [Mil Ruixinwei RK3568 Industrial Development Board] Client TCP connection based on libuv Today, I will share with you the Ethernet-related evaluation of the industrial development board of Rockchip RK3568 exported by Mir: TCP client implementation based on the open source library libuv. Ethernet, in today's society, no one should say that I have never been online. This is true for people and machines. In industrial equipment communication, compared with RS485, CAN, or WiFi, Bluetooth, zigbee and other communication methods, Ethernet is still a communication method with large bandwidth, high speed and low latency. These characteristics have also become an important bargaining chip for designing applications using Ethernet communication in the field of industrial control. For servo motor drives with extremely high real-time requirements, the EtherCAT protocol is also deployed on the basis of Ethernet. According to relevant reports from technology media, some leading new energy vehicle companies have also begun to deploy in-vehicle Ethernet and update Ethernet protocols that adapt to automotive safety. It can be seen that Ethernet function is a basic knowledge and skill that our embedded engineers must have. ## Mir RK3568 Development Board Since Ethernet is so important, how is the design of our Mir Rockchip RK3568 in this regard? As a domestic high-performance quad-core chip, RK3568 has a wealth of high-speed interfaces, such as the Ethernet interface we are going to evaluate this time. Mir has prepared two interfaces for us, and both Ethernet interfaces support gigabit speeds. That's right! It's a dual gigabit interface. In this way, we can use these two interfaces to join two different LANs, or use one interface to connect to the intranet and the other to the extranet. This can also isolate data and communication, and enhance data security. In short, dual gigabit network ports are sufficient for most applications in our industrial control field. ## libuv implements the client With the powerful hardware support of Mir RK3568, excellent, stable and reliable software drivers must also keep up. Considering the level of its own software implementation, I used the open source libuv to implement the TCP client and send and receive data this time. libuv is an open source cross-platform asynchronous IO library. It is also the underlying library implementation of the famous Node.js. The feature of the libuv open source library is asynchronous IO mode, but we use its client mode this time and borrow the implementation method of its callback function. Of course, for me personally, programming under an existing framework can ensure the robustness of the program. ## Deploy libuv client We need to download the source code of libuv from github.com. I suggest that you download the zip package of libuv directly. If you use git clone, it will fail due to well-known reasons. After downloading, unzip the zip package to a suitable folder. Since libuv is written in pure C and does not rely on other link libraries, we can directly run CMake to generate the dynamic link library .so file. Of course, if it is your own program, I still strongly recommend using a static link library, mainly for the sake of easy release and maintenance. There is another reason why I use the .so dynamic link library here. I also want to run the TCP server in the environment of Mier RK3568. In this way, we can share the same set of libuv libraries, which fully reflects the advantages of dynamic link libraries. ## Write our own TCP client With the libuv dynamic link library, we can write our program code based on the libuv framework. We let the app connect to the server with port number 9012 and send the string "Hello EEWORLD! Hello MYiR RK3568!". My reference source code is as follows: ```c /** ************************************************************************** *
@File main.c *
@author jobs *
@version v0.00 *
@date 2024-09-11 *
@brief * @note * ************************************************************************** */ #include
#include
#include
#include
#include “uv.h” #define DEFAULT_PORT (9012) uv_buf_t iov; char buffer[128]; const char hello_str[] = "Hello EEWORLD! Hello MYiR RK3568!"; uv_loop_t *loop; uv_tcp_t client_tcp_socket; uv_connect_t connect; void send_done (uv_write_t *req, int status) { if (status < 0) { fprintf(stderr, "Connection error %s\n", uv_strerror(status)); return; } uv_close((uv_handle_t *)&client_tcp_socket, NULL); } void on_connect(uv_connect_t *req, int status) { if (status < 0) { fprintf(stderr, "Connection error %s\n", uv_strerror(status)); return; } fprintf(stdout, "Connect ok\n"); stpcpy(buffer, hello_str); iov = uv_buf_init(buffer, sizeof(buffer)); iov.len = strlen(hello_str); uv_write ((uv_write_t *)req, (uv_stream_t *)&client_tcp_socket, &iov, 1, send_done); } int main() { int ret = 0; struct sockaddr_in dest; *loop = uv_default_loop(); uv_tcp_init(loop, &client_tcp_socket); uv_ip4_addr ("192.168.8.18", DEFAULT_PORT, &dest); ret = uv_tcp_connect(&connect, &client_tcp_socket, (const struct sockaddr *)&dest, on_connect); if (ret) { fprintf(stderr, "connect error %s\n", uv_strerror(ret)); return 1; } uv_run(loop, UV_RUN_DEFAULT); return 0; } /*** ***************************** END OF FILE ****************** ***************/ ```
We can see that our simulated TCP server successfully received the connection of Mier RK3568 and received the sent ## Summary Through this trial of the Ethernet interface of the TCP client, we can see that MiRK3568 can easily achieve TCP high-speed and large-bandwidth data communication in the fields of industrial control, automation, and robotics. It also plays a very positive and effective role. I believe that after this round of trials, Mir RK3568 will be given priority in the selection of actual projects, and it will definitely reduce the development cycle of my actual projects.