简体   繁体   English

Raspberry Pi:printf()与connectionPi不兼容

[英]Raspberry Pi: printf() doesn't work with wiringPi

I'm trying a simple code that using wiringPi as here: 我正在尝试一个简单的代码,如下所示使用connectioningPi:

#include<wiringPi.h>
#include<stdio.h>

int main(void){
    int i;

    wirintPiSetup();
    pinMode(0,OUTPUT);   //a single LED
    pinMode(8,INPUT);    //tactile switch

    for(;;){
        delay(500);
        //push tactile switch and LED is turning on
        if(digitalRead(8)) digitalWrite(0,0);
        else digitalWrite(0,1);
        printf("%d",digitalRead(8));
    }
}

I expected a result of printf() is output to console, but it doesn't work. 我希望将printf()的结果输出到控制台,但是不起作用。 printf() couldn't run in same time with wiringPi APIs? printf()无法与connectionPi API同时运行?

no warnings at compile. 编译时没有警告。 and CPU consumption is always under 4%. 并且CPU消耗始终低于4%。 running on Raspbian. 在Raspbian上运行。

Thanks for your time! 谢谢你的时间!

stdout by default is line-buffered , meaning it tries to put off writing data to the underlying file until a newline. 默认情况下, stdout行缓冲的 ,这意味着它将尝试推迟将数据写入基础文件,直到换行为止。 But since you never print a newline, stdout will just buffer your text until it runs out of space. 但是由于您从不打印换行符,因此stdout只会缓冲文本,直到空间用尽。

You can fix this by either adding a newline in the format string (ie "%d\\n" ), or calling fflush on stdout after printing. 您可以通过在格式字符串中添加换行符(即"%d\\n" )或在打印后在stdout上调用fflush来解决此问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM