STM32CubeIDE printf 多个串口
自定义USART_printf如下
#include <stdarg.h> #include <stdio.h> #include <string.h> void USART_printf (UART_HandleTypeDef *huart,char *fmt, ...){ static char buffer[256]; va_list args; va_start(args,fmt); vsprintf(buffer,fmt,args); va_end(args); HAL_UART_Transmit(huart,(uint8_t*)buffer,strlen(buffer),0xFFFFFFFFU); }
使用方法如下:
USART_printf(&huart1,"%s is %d years old\r\n","Li Ming",15);//在串口1中打印信息 USART_printf(&huart2,"%s is %d years old\r\n","Wang Fei",16);//在串口2中打印信息
不同的串口同时打印信息结果如下
第二种重定向printf
int __io_putchar(int ch) { HAL_UART_Transmit(&huart1, (uint8_t*)&ch, 1, 1000); return ch; }