作者:mR_woManh | 来源:互联网 | 2022-12-04 14:04
我想为printf()
我正在研究的输出终端是串口的嵌入式板写一个快速的功能.我试过这样的事情:
int32_t printfDebugSerial(const char *format, ...)
{
char tempBuff[256];
memset(tempBuff, 0, sizeof tempBuff);
va_list arg;
int32_t done;
va_start (arg, format);
dOne= (int32_t)sprintf(tempBuff,format, arg);
va_end (arg);
HAL_sendToSerial((uint8_t*)tempBuff, strlen(tempBuff)); // writes bytes to serial port
return done;
}
但是当我调用它时,我得到的输出如下:
printfDebugSerial("Hello = %u", 1234);
输出:
Hello = 536929228
然后称为:
printfDebugSerial("Hello = %f", 934.3245);
输出:
Hello = 0.000000
任何帮助,这里有什么问题?
1> likle..:
如果你要转发,你应该使用vsprintf而不是sprintf va_list
:
int32_t printfDebugSerial(const char *format, ...)
{
char tempBuff[256];
memset(tempBuff, 0, sizeof tempBuff);
va_list arg;
int32_t done;
va_start (arg, format);
dOne= (int32_t)vsprintf(tempBuff,format, arg);
va_end (arg);
HAL_sendToSerial((uint8_t*)tempBuff, strlen(tempBuff)); // writes bytes to serial port
return done;
}