From 07aaa98dc45652f243950a580c7b60e9b6b68b07 Mon Sep 17 00:00:00 2001 From: murmur Date: Sun, 15 Dec 2024 16:35:35 +0800 Subject: [PATCH] =?UTF-8?q?=E9=87=8D=E5=AE=9A=E5=90=91printf=E5=88=B0udp?= =?UTF-8?q?=E8=BE=93=E5=87=BA=EF=BC=8C=E7=AB=AF=E5=8F=A38888?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Core/Src/http_server.c | 1 + Core/Src/main.c | 1 + Core/Src/w5500_init.c | 53 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 55 insertions(+) diff --git a/Core/Src/http_server.c b/Core/Src/http_server.c index edfff04..96f8106 100644 --- a/Core/Src/http_server.c +++ b/Core/Src/http_server.c @@ -251,6 +251,7 @@ static void handle_status_request(uint8_t sn) // 发送完成后主动关闭连接 disconnect(sn); + printf("send status success!\r\n"); } void http_server_task(void) diff --git a/Core/Src/main.c b/Core/Src/main.c index 35393ea..53aa946 100644 --- a/Core/Src/main.c +++ b/Core/Src/main.c @@ -110,6 +110,7 @@ int main(void) // 执行一次HTTP客户端测试 // http_test_request(); HAL_GPIO_WritePin(LED_GPIO_Port, LED_Pin, GPIO_PIN_RESET); + printf("Init success!\r\n"); /* USER CODE END 2 */ /* Infinite loop */ diff --git a/Core/Src/w5500_init.c b/Core/Src/w5500_init.c index 7376f97..cc0bd3d 100644 --- a/Core/Src/w5500_init.c +++ b/Core/Src/w5500_init.c @@ -1,4 +1,6 @@ #include "w5500_init.h" +#include +#include "socket.h" // W5500 Network Configuration wiz_NetInfo gWIZNETINFO = { @@ -37,6 +39,52 @@ static void W5500_WriteByte(uint8_t byte) { W5500_WriteBuff(&byte, 1); } +// UDP日志相关定义 +#define UDP_LOG_PORT 8888 // UDP日志端口 +#define UDP_LOG_SOCKET 7 // 使用Socket 7作为日志socket + +// 初始化UDP日志功能 +int8_t UDP_Log_Init(void); +// 发送UDP日志数据 +void UDP_Log_Send(const uint8_t* data, uint16_t len); + +static uint8_t udp_log_buffer[1024]; // UDP发送缓冲区 + +int8_t UDP_Log_Init(void) +{ + // 关闭可能已经打开的socket + close(UDP_LOG_SOCKET); + + // 以UDP模式打开socket + if(socket(UDP_LOG_SOCKET, Sn_MR_UDP, UDP_LOG_PORT, 0) != UDP_LOG_SOCKET) + { + return -1; + } + + return 0; +} + +void UDP_Log_Send(const uint8_t* data, uint16_t len) +{ + uint8_t broadcast_ip[] = {255, 255, 255, 255}; // 广播地址 + sendto(UDP_LOG_SOCKET, data, len, broadcast_ip, UDP_LOG_PORT); +} + +// printf重定向相关实现 +#ifdef __GNUC__ +int _write(int file, char *ptr, int len) +{ + UDP_Log_Send((uint8_t*)ptr, len); + return len; +} +#elif defined(__ICCARM__) +size_t __write(int handle, const unsigned char *buffer, size_t size) +{ + UDP_Log_Send(buffer, size); + return size; +} +#endif + int8_t W5500_Init(void) { // 1. 注册通信函数 @@ -70,5 +118,10 @@ int8_t W5500_Init(void) } } + // 初始化UDP日志功能 + if(UDP_Log_Init() < 0) { + return -1; + } + return 0; } \ No newline at end of file