简体   繁体   中英

Raspberry Pi - C program not writing to text file

I am new to programming with linux/raspbian and I am trying to make a program that extracts the Pi system time and writes it to a text file whenever pin 23 goes high. Pin 23 is connected to an SR Latch and pin 24 sends out a reset signal to reset the latch.

The problem I am having is that it doesn't seem to write anything to the text file that is created. The program creates the file fine but doesn't write anything to it. Here is my code:

using namespace std; 
FILE *f; 
struct timeval curTime;

int main(int argc, char *argv[]){
char dateiname[256] = "";
int i=0;
int milli;
int seconds_in_day;

wiringPiSetupGpio();

time_t t = time(0);
struct tm * now = localtime(&t);

//Create and open file
sprintf(dateiname, "/home/raspbian/Desktop/%02d%02d%02d_%02d_%02d.txt", 
                       now -> tm_year+1900, 
                       now -> tm_mon+1, 
                       now -> tm_mday, 
                       now -> tm_hour, 
                       now -> tm_min); 

f = fopen(dateiname, "w");
//write heading to file before loop
fprintf(f, "Picture, system time\n");

//Set 23 & 24 as input/ output
pinMode(23, INPUT);
pullUpDnControl(23, PUD_DOWN);
pinMode(24, OUTPUT);

while(1){ 
if(digitalRead(23)){ //If 23 is high
   i=i+1;
   gettimeofday(&curTime, NULL);
   milli = curTime.tv_usec / 1000; //Get time in milliseconds
   seconds_in_day = curTime.tv_sec % 86400; //Get seconds since midnight
   fprintf(f, "&d &d.%d\n", i, seconds_in_day, milli); //Write to file

   //send out reset signal
   digitalWrite(24, HIGH); 
   //pause for 1 second
   delay(1000);
}
}
fclose(f);
return(0);
}

Does anyone see any obvious errors here? I am also running the program in the terminal through

sudo /home/raspbian/Desktop/program 

and just quitting the program by exiting the terminal window. Thanks

It's probably buffering the output. The buffer isn't certain to be written until the fclose executes, which is never.

If you want the file updated once per second with a single line when pin 23 is high, put the file fopen and fclose inside the loop. If you want to add a line every second, then add fflush(f); after the fprintf .

I suspect the output on pin 24 needs to be a 'short' period of 'high' to cause the latch to reset, followed by a return to 'low' in preparation for the next time the latch needs to be reset.

the lines like: while( !digitalRead(23) ); will burn a lot of CPU cycles, so may want to put some 'delay()' or yield() into a body for each of those loops

using namespace std;
FILE *f;
struct timeval curTime;

int main(int argc, char *argv[]){
    char dateiname[256] = "";
    int i=0;
    int milli;
    int seconds_in_day;

    wiringPiSetupGpio();

    time_t t = time(0);
    struct tm * now = localtime(&t);

    //Create and open file
    sprintf(dateiname, "/home/raspbian/Desktop/%02d%02d%02d_%02d_%02d.txt",
                           now -> tm_year+1900,
                           now -> tm_mon+1,
                           now -> tm_mday,
                           now -> tm_hour,
                           now -> tm_min);

    if( NULL == (f = fopen(dateiname, "w") )
    { // then fopen failed
        perror( "fopen failed for output file");
        exit(EXIT_FAILURE);
    }

    // implied else, fopen successful

    //write heading to file before loop
    fprintf(f, "Picture, system time\n");
    fflush( f );

    //Set 23 & 24 as input/ output
    pinMode(23, INPUT);
    pullUpDnControl(23, PUD_DOWN);
    pinMode(24, OUTPUT);

    // assure latch is reset
    digitalWrite(24, LOW);
    digitalWrite(24, HIGH);
    digitalWrite(24, LOW);

    while(1)
    {
        // wait while pin23 is low
        while( !digitalRead(23));

        // 23 is high
        i=i+1;
        gettimeofday(&curTime, NULL);
        milli = curTime.tv_usec / 1000; //Get time in milliseconds
        seconds_in_day = curTime.tv_sec % 86400; //Get seconds since midnight
        fprintf(f, "&d &d.%d\n", i, seconds_in_day, milli); //Write to file
        fflush( f );

        // if a latch 'set' signal is received during the following
        // three instructions, then could get locked into 
        // the while pin23 high loop

        // reset latch
        digitalWrite(24, HIGH);
        digitalWrite(24, LOW);

        // wait for pin 23 to be low
        while( digitalRead(23) );
    } // end while

    fclose(f);
    return(0);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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