Reputation: 1132
I want to write a quick printf()
kinda function for an embedded board that I am working on where the output terminal is a serial port. I tried something like this:
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;
}
But the output that I got when I called it as follows:
printfDebugSerial("Hello = %u", 1234);
output:
Hello = 536929228
then called as:
printfDebugSerial("Hello = %f", 934.3245);
output:
Hello = 0.000000
Any help, what is wrong here?
Upvotes: 0
Views: 1041
Reputation: 1797
You should use vsprintf instead of sprintf if you are forwarding va_list
's:
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;
}
Upvotes: 6