I have been trying to construct a program for reading the ttyS0 serial port once a second and saving the data to a file. This works for about 4,5h, then the readings start to get messed up, and stays that way. The datalogging looks like this;
I have no idea why it messes up the readings every ~4h and has to be restarted. Any help to resolve this problem is appreciated. The code written for the program looks like;
// Calculate how long to sleep in micro seconds
sleeptime = (nextsample.tv_sec-now.tv_sec)*1000000 + (nextsample.tv_usec-now.tv_usec);
// Check if we missed the sample (should never happen, except when freq is VERY high)
if ( sleeptime < 0 ) {
printf("Missed schedule for next sample\n");
lastsample = now;
}
else {
// Wait until it's time for the next sample
usleep(sleeptime);
// Timestamp for current sample
gettimeofday(&lastsample,NULL);
//Check for datefile change
if(strcmp(&dateold[0],&date[0]) != 0){
fprintf(stdout, "Open new date file \n");
close(result_fd); //Close
// Open new date fileset
if ( (result_fd = open(&date[0], O_CREAT|O_RDWR, S_IRUSR|S_IWUSR)) == -1 ) {
fprintf(stderr, "Could not open result file\n");
//fclose(samp_fd);
return 1;
}
}
//Update the time index
strcpy(&dateold[0],&date[0]);
// Write result
assert(write(result_fd, &outstring[0], strlen(outstring)) > 0);
// Print result to console
printf("%s",outstring);