Messed up readings using ttyS0

Hello,

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;

Time: Freq:
------------------
140852 50.0768
140853 50.0773
140854 50.0780
.
(long time, over 4h)
.
183707 50.0039
183708 50.0032
183709 50.0028
183710 50.0020
183711 50.0015
183712 50.0007
183713 50.0000
183714 49.9996
183715 49. 49.
183716 61

49.
183717 57

49.
183718 52

49.
183719 48
----------------

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;

----------------

#include <assert.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <sys/time.h>
#include <string.h>
#include <errno.h>
#include <stdio.h>

int main(int argc, char **argv) {
int freq = 1;
FILE *samp_fd;
int result_fd;
useconds_t sleeptime;
struct timeval lastsample;
struct timeval nextsample;
struct timeval now;
struct tm *datetime;
char data[11]; //Number of transmitted data from serial port
char outstring[128];
char date[8];
char dateold[8];
// char sz;

// Open the device to sample from
if ( (samp_fd = fopen("/dev/ttyS0","r")) == NULL ) {
fprintf(stderr, "Could not open serial port\n");
return 1;
}

gettimeofday(&lastsample,NULL)==0;

for(;;) {
// Calculate when next sample should be taken
nextsample.tv_sec = lastsample.tv_sec;
nextsample.tv_usec = lastsample.tv_usec + 1000000/freq;

if ( nextsample.tv_usec >= 1000000 ) {
nextsample.tv_sec ++;
nextsample.tv_usec -= 1000000;
}

// Get current time
gettimeofday(&now,NULL);

// 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);

// Get sample data, wait for full read

while(fread(&data[0] , 1, 10, samp_fd) < 10 ){
printf("Error reading, wrong datalength. Reading again. \n");
}

/* if (sz < sizeof(data) ) {
fprintf(stderr, "Did not get an entire sample from device (%d): %s\n", sz, strerror(errno));
continue;
}
*/

// Add NULL termination for ASCII-string in the data
data[8] = 0;

// Get the year, month, ... components out of our timestamp
datetime = gmtime(&(lastsample.tv_sec));

// Create a formatted log entry into "outstring"
sprintf(outstring, "%02d%02d%02d %s\n",
datetime->tm_hour,
datetime->tm_min,
datetime->tm_sec,
&data[0]);

sprintf(date, "%04d%02d%02d",
1900+datetime->tm_year,
datetime->tm_mon,
datetime->tm_mday);

//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);

// Sync data to disk
fdatasync(result_fd);

}
}
}
Topic archived. No new replies allowed.