Featured image of post HAL_Printf 自定义多个串口打印

HAL_Printf 自定义多个串口打印

HAL_Printf 自定义多个串口打印

旨在于多串口使用printf 重定义

myprintf.c

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
#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

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
#ifndef __MYPRINTF_H_
#define __MYPRINTF_H_
#ifdef __cplusplus
extern "C" {
#endif

void UART1_Printf(const char* fmt, ...);

#ifdef __cplusplus
}
#endif
#endif
最后更新于 Mar 21, 2025 09:33 +0800
世界是你们
使用 Hugo 构建
主题 StackJimmy 设计