IAR等一些软件printf 和scanf函数对应的是虚拟终端。想通过自己的串口打印就需要自己写输入、输出函数。
前提:对应串口已经完成初始化,包好对应的读写函数。
#include "my_print.h"
#include <stdio.h>
#include "stdarg.h"
#include "usruart.h"
#include "uart.h"
#include "utypes.h"
void my_print(const char *ptr,...)
{
va_list ap;
char string[256]; //可根据fifo大小进行设置
va_start(ap,ptr);
vsnprintf(string,256,ptr,ap);
UartWrite(&g_UsrUartPL[0], string, strlen(string));
va_end(ap);
}
//本函数只针对做菜单时的选项读取,没进行多输入输入测试
int my_scanf(const char *fmt,...)
{
int i = 0;
va_list args;
char RxBuff[256] = {0};
while(1)
{
//从串口接收字符
UartRead(&g_UsrUartPL[0], RxBuff, 1); //读取一个字节数据
// 接收到的数据发送到上位机
if((RxBuff[i] == 0x0d) || (RxBuff[i] == 0x0a)) //判断是否回车或者换行
{
break; //是则退出
}
if(RxBuff[i] != '\0') //判断是否为空
{
i++;
}
}
va_start(args,fmt);
i = vsscanf(RxBuff,fmt,args);
va_end(args);
return i;
}