ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

为什么C打印输出迟到?

2019-10-12 01:50:27  阅读:89  来源: 互联网

标签:c-3 linux raspberry-pi


我目前在Raspberry Pi计算机上运行一段C代码.它是一个随机数发生器,从连接到GPIO数字输入18的Geiger计数器读取.它生成随机位(请参见代码),并以8为一组的方式打印这些位.此外,每隔30秒,它会打印观察到的辐射的当前水平. .该代码似乎工作正常,除非带走了辐射源.随机数生成的速度较慢,但​​似乎也会减慢其余任务的速度.在程序开始时打印的消息不会出现,直到生成一个随机数.发生这种情况时,不会显示任何数字,但是会添加一个没有数字的换行符.即使程序正在运行,辐射水平似乎也会每30秒打印一次,但也会在下一个随机数上显示.为什么C以错误的顺序执行代码?

#include <wiringPi.h>//library for I/O
#include <stdlib.h>
int main(int argc, char *argv[])
{
    int lastRead;//if this is first time observing current pulse
    int pulseCount = 0;//number of total pulses from Geiger counter
    long timing[4] = {0,0,0,0};//values to compare to produce one bit
    int bits[8] = {0,0,0,0,0,0,0,0};//the newest number
    int bitCount = 0;//counts how many random bits have been made
    int i = 0;
    float startTime = 0;//start of the clock
    float currentSec = 0;
    float currentMin = 0;
    float cpm = 0;
    long elapsedTime = 0;

    wiringPiSetupGpio();//establish physical connection
    pinMode(18, INPUT);//set pin 18 to be input
    printf("random\tradiation");

    while(1)
    {
        if( millis()-startTime >= 30000)//30 sec passed?
        {
            startTime = millis();
            currentSec = startTime/1000;
            currentMin = currentSec/60;
            cpm = pulseCount/currentMin;//calculate counts/min in several steps
            printf("\t%f", cpm);//output counts/min
        }
        if( digitalRead(18) == HIGH )//pin is reading high
        {
            if(lastRead==0)//is not reading the same pulse again
            {
                lastRead = 1;//pulse has been identified
                timing[pulseCount%4] = millis();//save the time
                pulseCount++;//pulse detected

                if( pulseCount%4 == 0 )//if times have been collected
                {
                    if( timing[1]-timing[0] > timing[3]-timing[2] )//make a random bit
                    {
                        bits[bitCount%8] = 0;//nth bit of set of 8 is 0
                    }
                    else {
                        bits[bitCount%8] = 1;//it is one
                    }
                    bitCount++;//note that a bit was added

                    if( bitCount%8 == 0 )//every 8 bits made
                    {
                        printf("\n");

                        for( i = 0; i < 8; i++)//print them on a new line
                        {
                            printf("%d", bits[i]);
                        }//for
                    }//if(bitCount%8==0)
                }//if(pulseCount%4==0)
            }//if(lastRead==0)
        }//if(digitalRead(18)==TRUE)
        else {
            lastRead = 0;//ready to read new pulse
        }//else
    }//while
}//main()

解决方法:

默认情况下,stdout上的输出在写入终端时是行缓冲的.这意味着输出将一直保存在内存中,直到您打印换行符或调用fflush(stdout)(或输出缓冲区已满-通常为4K或8K字符)为止.

因此,将fflush(stdout)放在要显示累积输出的位置.或使用setbuf(stdout,NULL)完全禁用缓冲.

标签:c-3,linux,raspberry-pi
来源: https://codeday.me/bug/20191012/1896849.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有