Keil中串口打印函数重定义snprintf()

#ifndef _DEBUG_H__
#define _DEBUG_H__

#include "stdio.h"
#include "usart.h"

//#define DEBUG_PRINTF   //如果不需要串口打印信息,将此宏打开

//  debug(...)中的'...'内容用__VA_ARGS__或##__VA_ARGS__替代 ,__VA_ARGS__和##__VA_ARGS__两种写法都可以
// C语言中以 ; 作为语句的结束,不以行为单位结束,当一行的内容太长不方便卸载一行时可使用反斜 杠"\"作为继续符
//  注意宏定义的中的语句,换行要加反斜杠'\'继续符,表示上一语句紧挨着下条语句,
//  且反斜杠后面不要有任何注释语句甚至空格,反斜杠前可以有空格
// 注意每条语句后面都添加反斜杠'\',否则会报错
#ifndef DEBUG_PRINTF
#define debug(...) {\
                int len = 0;\
                len = snprintf(debug_buf, 128, ##__VA_ARGS__);   \
                if(len > 0)\
                {\
                    HAL_UART_Transmit_DMA(&huart2, (uint8_t *)debug_buf, len);\
                }\
            }  //到此不用加反斜杠
#else
#define debug(...) {\
                snprintf(debug_buf, 128, __VA_ARGS__);\
            }
#endif
            
#endif  //_DEBUG_H__

 

posted @ 2020-08-22 13:21  MyBooks  阅读(871)  评论(0编辑  收藏  举报