【得捷电子Follow me第4期】基础任务二-TCP服务
[复制链接]
本帖最后由 qinyunti 于 2024-2-17 00:14 编辑
ff825077221a0da0d143752c69c6469e
- 基础任务二:主控板建立TCPIP或UDP服务器,局域网PC使用TCPIP或UDP客户端进行连接并发送数据,主控板接收到数据后,送液晶屏显示(没有则通过串口打印显示);通过抓包软件抓取交互报文,展示并分析。(TCP和UDP二选一,或者全都操作)
5.1 准备
结合前面的入门任务和基础任务一。
添加代码
修改CMakeLists.txt为如下
set(TARGET_NAME w5x00_loopback)
aux_source_directory(ZLG_GUI ZLG_GUI_SOURCES)
include_directories(../../libraries/ioLibrary_Driver/Internet/TFTP ZLG_GUI/include .)
add_executable(${TARGET_NAME}
${TARGET_NAME}.c
ls013.c
ls013_lcd.c
${ZLG_GUI_SOURCES}
)
target_link_libraries(${TARGET_NAME} PRIVATE
pico_stdlib
hardware_spi
hardware_dma
ETHERNET_FILES
IOLIBRARY_FILES
LOOPBACK_FILES
)
pico_enable_stdio_usb(${TARGET_NAME} 1)
pico_enable_stdio_uart(${TARGET_NAME} 0)
pico_add_extra_outputs(${TARGET_NAME})
5.2 代码
w5x00_loopback.c
/**
* Copyright (c) 2021 WIZnet Co.,Ltd
*
* SPDX-License-Identifier: BSD-3-Clause
*/
/**
* ----------------------------------------------------------------------------------------------------
* Includes
* ----------------------------------------------------------------------------------------------------
*/
#include <stdio.h>
#include "port_common.h"
#include "wizchip_conf.h"
#include "w5x00_spi.h"
#include "loopback.h"
#include "ping.h"
#include "ls013_lcd.h"
#include "config.h"
/**
* ----------------------------------------------------------------------------------------------------
* Macros
* ----------------------------------------------------------------------------------------------------
*/
/* Clock */
#define PLL_SYS_KHZ (133 * 1000)
/* Buffer */
#define ETHERNET_BUF_MAX_SIZE (1024 * 2)
/* Socket */
#define SOCKET_LOOPBACK 0
/* Port */
#define PORT_LOOPBACK 5000
/**
* ----------------------------------------------------------------------------------------------------
* Variables
* ----------------------------------------------------------------------------------------------------
*/
/* Network */
static wiz_NetInfo g_net_info =
{
.mac = {0x00, 0x08, 0xDC, 0x12, 0x34, 0x56}, // MAC address
.ip = {192, 168, 31, 111}, // IP address
.sn = {255, 255, 255, 0}, // Subnet Mask
.gw = {192, 168, 31, 1}, // Gateway
.dns = {8, 8, 8, 8}, // DNS server
.dhcp = NETINFO_STATIC // DHCP enable/disable
};
/* Loopback */
static uint8_t g_loopback_buf[ETHERNET_BUF_MAX_SIZE] = {
0,
};
/**
* ----------------------------------------------------------------------------------------------------
* Functions
* ----------------------------------------------------------------------------------------------------
*/
/* Clock */
static void set_clock_khz(void);
/**
* ----------------------------------------------------------------------------------------------------
* Main
* ----------------------------------------------------------------------------------------------------
*/
uint8_t pDestaddr[4] = {120,232,145,144};
int main()
{
uint8_t tmp=0;
/* Initialize */
int retval = 0;
set_clock_khz();
stdio_init_all();
wizchip_spi_initialize();
wizchip_cris_initialize();
wizchip_reset();
wizchip_initialize();
wizchip_check();
network_initialize(g_net_info);
/* Get network information */
print_network_information(g_net_info);
GUI_Initialize();
GUI_SetColor(1,0);
GUI_PutString8_8(10,10,"tcp echo test");
/* Infinite loop */
while (1)
{
#if 0
tmp = ping_auto(0,pDestaddr);
if(tmp == SUCCESS)
printf("-----------PING TEST OK----------\r\n");
else
printf("----------ERROR = %d----------\r\n",tmp);
sleep_ms(50);
#endif
#if 1
/* TCP server loopback test */
if ((retval = loopback_tcps(SOCKET_LOOPBACK, g_loopback_buf, PORT_LOOPBACK)) < 0)
{
printf(" Loopback error : %d\n", retval);
GUI_PutString8_8(10,20,"Loopback error");
}
else
{
GUI_PutString8_8(10,20,"Loopback ok");
GUI_PutString8_8(10,30,g_loopback_buf);
}
#endif
GUI_Exec();
}
}
/**
* ----------------------------------------------------------------------------------------------------
* Functions
* ----------------------------------------------------------------------------------------------------
*/
/* Clock */
static void set_clock_khz(void)
{
// set a system clock frequency in khz
set_sys_clock_khz(PLL_SYS_KHZ, true);
// configure the specified clock
clock_configure(
clk_peri,
0, // No glitchless mux
CLOCKS_CLK_PERI_CTRL_AUXSRC_VALUE_CLKSRC_PLL_SYS, // System PLL on AUX mux
PLL_SYS_KHZ * 1000, // Input frequency
PLL_SYS_KHZ * 1000 // Output (must be same as no divider)
);
}
5.3 测试
试用网络调试助手,连接开发板TCP端口5000,发送数据开发板原样返回,屏幕显示结果和收到的数据。
5.4 抓包分析
抓包TCP交互过程和内容如下
|