(转载)关于UART中用Printf()输出float型数据

在coocox上看到有人问关于Printf打印浮点型数据会输出0或者其他的问题
The code:
printf("float: %f \n\r", 10.02);
Produce that result:
float: H
帖子的楼猪问必须使用printf下怎么输出浮点型数据呢?
斑猪做了回答,代码如下:
The retarget printf don't support print float value now.
目前不支持float型......
然后可以做一些修改使其支持,如下
To printf float argument through UART, Semihosting or other communication method, you cannot use the Retarget Printf component in CoIDE. That component is a simple example for print int, char value.

You can do the followings:

    []Check C Library Component[/]

    []Open syscall/syscall.c, then modify the[/]


int _read(int file, char ptr, int len)
{
return 0;
}
int _write(int file, char ptr, int len)
{
return len;
}

to:
int _read(int file, char ptr, int len) {
int c, rxCount = 0;

(void) file;

while (len--) {
if ((c = GetChar()) != -1) { // Getchar Function need to be replaced by the function you used to get values.
ptr++ = c;
rxCount++;
} else {
break;
}
}

if (rxCount <= 0) {
return -1; / Error exit /
}

return rxCount;
}

int _write(int file, char ptr, int len) {
int txCount;

(void) file;

for (txCount = 0; txCount < len; txCount++) {
SendChar(ptr++); // SendChar Function need to be replaced by the function you used to print values.

}

return len;
}

    []Then you can use printf, scanf or other c library function to print and get value in your project.[/]
然后斑猪又提到一点:
The old version often hung up on Win7.

The new version support SWO Trace, and quicker, more stable than the old version.

Different version of firmware need different version of driver.

The virture COM is for SWO trace for M3 chips only (which have ITM function). They cannot be use as UART to USB function.
貌似意思是用usb虚拟串口的话,这样的printf是不能这样用的...................
本文转自coocox论坛,如有冒犯请指出,谢谢

2 个评论

以上,没测试过....不知道keil中是不是这样....也许keil中支持printf输出浮点呢..............
他的问题是针对CoIDE软件的........
keil是可以的

要回复文章请先登录注册