为什么是C打印输出迟到? [英] Why is C printing outputs late?

查看:137
本文介绍了为什么是C打印输出迟到?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在运行的我的树莓派电脑上的一块C $ C $的c。这是一个随机数发生器,从连接到GPIO数字输入上的盖革计数器读取18.这使得随机位(见code),并打印在套8.位,每30秒,它打印当前水平观察辐射。的code似乎是精细的工作,当辐射源带走除外。随机数生成少快,但它也似乎的任务的其余部分慢下来。直到产生的一个随机数在程序开始打印的信息不显示。当这种情况发生时,没有数字显示,但有没有号码添加一个新行。在程序运行即使,辐射的水平似乎要打印每30秒,而且对下一个随机数。为什么为C执行code以错误的顺序?

 的#include< wiringPi.h> //库I / O
#包括LT&;&stdlib.h中GT;
INT主(INT ARGC,CHAR *的argv [])
{
    INT lastRead; //如果这是第一次观察电流脉冲
    INT脉冲计数= 0; //从盖革计数器总人数的脉冲
    长定时[4] = {0,0,0,0}; //值进行比较,以产生一个位
    INT位[8] = {0,0,0,0,0,0,0,0}; //最新数
    INT比特计数= 0; //计数多少随机位已进行
    INT I = 0;
    浮STARTTIME = 0; //启动时钟
    浮currentSec = 0;
    浮currentMin = 0;
    浮CPM = 0;
    长ela​​psedTime = 0;    wiringPiSetupGpio(); //建立物理连接
    pinMode(18,INPUT); //组销18被输入
    的printf(随机\\ tradiation);    而(1)
    {
        如果(米利斯() - startTime时> = 30000)// 30秒过去了?
        {
            STARTTIME =米利斯();
            currentSec =的startTime / 1000;
            currentMin = currentSec / 60;
            CPM =脉冲计数/ currentMin; //计算数/分几个步骤
            的printf(\\ t%F,CPM); //输出计数/分钟
        }
        如果(digitalRead(18)==高)//引脚读高
        {
            如果(lastRead == 0)//不会被再次读取相同的脉冲
            {
                lastRead = 1; //脉冲已被确定
                时序[脉冲计数%4] =米利斯(); //保存时间
                脉冲计数++; //脉冲检测                如果(脉冲计数%4 == 0)//如果时间已收集
                {
                    如果(时刻[1] -timing [0]≥定时[3] -timing [2])//使一个随机比特
                    {
                        位[比特计数%8] = 0;组8的//第n位为0时
                    }
                    其他{
                        位[比特计数%8] = 1; //它一个
                    }
                    位计数++; //注意添加了一点                    如果(位计数%8 == 0)//每8位制作
                    {
                        的printf(\\ n);                        对于(i = 0; I< 8;我++)//打印他们在新行
                        {
                            的printf(%D位[I]);
                        }//对于
                    } //如果(位计数%8 == 0)
                } //如果(脉冲计数%4 == 0)
            } //如果(lastRead == 0)
        } //如果(digitalRead(18)== TRUE)
        其他{
            lastRead = 0; //准备读取新的脉冲
        }//其他
    }//而
}//主要()


解决方案

默认情况下,输出在标准输出是行缓冲时,它的写入终端。这意味着输出将在内存中,直到你打印一个换行符或致电 fflush(标准输出)举行(或输出缓冲区填满了 - 这是典型的4K或8K字)

所以把 fflush(标准输出)在您要显示的累计输出的地方。或者使用则setbuf(标准输出,NULL)禁用完全缓冲。

I am currently running a piece of C code on my Raspberry Pi computer. It is a random number generator that reads from a Geiger counter connected to GPIO digital input 18. It makes randomized bits(see code) and prints the bits in sets of 8. Also, every 30 seconds, it prints the current level of observed radiation. The code seems to be working fine, except when the radiation source is taken away. The random numbers are generated less quickly, but it also seems to slow down the rest of the tasks. The message printed at the beginning of the program does not show up until one random number is generated. When this happens, no number shows, but there is a newline added without a number. Even while the program is running, the level of radiation appears to print every 30 seconds, but also on the next random number. Why is C executing the code in the wrong order?

#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()

解决方案

By default, output on stdout is line-buffered when it's writing to a terminal. This means that the output will be held in memory until you print a newline or call fflush(stdout) (or the output buffer fills up -- that's typically 4K or 8K characters).

So put fflush(stdout) at the places where you want the accumulated output to be displayed. Or use setbuf(stdout, NULL) to disable buffering entirely.

这篇关于为什么是C打印输出迟到?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆