title: HAL-Printf 自定义多个串口打印
tags:
- STM32
- Hal
- Cubemax
内容
主旨在于多串口使用printf 重定义
myprintf.c
#include "myprintf.h"
#include "stm32f1xx_hal.h"
#include <stdarg.h> // va_list va_start va_end
#include <stdio.h> // sprintf
#include <string.h> // strlen
extern UART_HandleTypeDef huart1;
void UART1_Printf(const char* fmt, ...) {
char buff[64];
va_list args;
va_start(args, fmt);
vsnprintf(buff, sizeof(buff), fmt, args);
HAL_UART_Transmit(&huart1, (uint8_t*)buff, strlen(buff), HAL_MAX_DELAY);
va_end(args);
}
myprintf.h
#ifndef __MYPRINTF_H_
#define __MYPRINTF_H_
#ifdef __cplusplus
extern "C" {
#endif
void UART1_Printf(const char* fmt, ...);
#ifdef __cplusplus
}
#endif
#endif