跳转到内容
新建笔记

cubemx的makefile方式的printf重定向到串口

utils.h
/***
* @Date: 2024-08-25 15:32:23
* @LastEditors: Please set LastEditors
* @LastEditTime: 2024-08-25 16:23:59
* @FilePath: \cameraLcd\Core\Inc\utils.h
*/
#ifndef __UTILS_H
#define __UTILS_H
int _write(int fd, char *ch, int len);
#endif /* __UTILS_H */
utils.c
#include "utils.h"
#include <stdio.h>
#include "usart.h"
int _write(int fd, char *ch, int len)
{
HAL_UART_Transmit(&huart1, (uint8_t*)ch, len, 0xFFFF);
return len;
}
main.c
#include "main.h"
...
#include "utils.h"
#include <stdio.h>
...
int main()
{
...
while(1)
{
printf("hello world\r\n");
}
...
}

VS Code 串口监视器收到 hello world 的历史运行截图


来源:CSDN 原文,首次发布于 2024-08-25。