Hi, everyone.
I tried to write data into a text file at 100Hz per line. Sometimes there would be a Memory Fault and created a core dump. The core dump file implied that there was a error when calling the function _Lockfilelock().
Here’s the code:
#include <sys/types.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <termios.h>
int main( void )
{
int fd_ser;
FILE *fp_file;
char rbuffer[300]={""}; //store GPS data
char rbuffer_crc[12]={""}; //GPS CRC
char cbuffer[400]={""}; //store configuration data
char wbuffer[40]={“log inspvasa ontime 0.1\r”}; //command
struct termios termios_p; //the serrial port attribute
//////////////////////////////////////////////
//Open the data recording file
//////////////////////////////////////////////
if((fp_file = fopen("GPSdata.txt", "a+"))==NULL)
{
printf("open error.\n");
return EXIT_FAILURE;
}
///////////////////////////////////////
//Open the serial port
//////////////////////////////////////
fd_ser = open( “/dev/ser1”, O_RDWR);
///////////////////////////////////////////////////
//Set the forwarding character ''
///////////////////////////////////////////////////
tcgetattr(fd_ser, &termios_p);
termios_p.c_cc[VFWD] = 0x00;
tcsetattr(fd_ser, TCSANOW, &termios_p);
///////////////////////////////////////
//Check satellite Omnistar
///////////////////////////////////////
printf("Checking Omnistar...\t\t");
fflush(stdout);
do
{
write(fd_ser, "log bestgpsposa once\r", sizeof("log bestgpaposa once\r"));
readcond(fd_ser, cbuffer, sizeof(cbuffer), 300, 0, 5); //Read the data
printf("%s\n",cbuffer);
}while(!strstr(cbuffer, "OMNISTAR"));
printf("******Connected!!******\n");
memset(cbuffer, 0, 400); //clear buffer
///////////////////////////////////////////////////
//Set the forwarding character '*'
///////////////////////////////////////////////////
tcgetattr(fd_ser, &termios_p);
termios_p.c_cc[VFWD] = '*';
tcsetattr(fd_ser, TCSANOW, &termios_p);
sleep(3);
///////////////////////////
//Send command
//////////////////////////
write(fd_ser, wbuffer, sizeof(wbuffer));
////////////////////////////////
// Read OK
////////////////////////////////
do
{
readcond(fd_ser, cbuffer, 13, 13, 0, 5); //Read 'OK'
}while(!strstr(cbuffer, "OK"));
memset(cbuffer, 0, 400); //clear buffer
//////////////////////
//Read GPS data
//////////////////////
while(1)
{
readcond(fd_ser, rbuffer, sizeof(rbuffer), 0, 0, 10);
readcond(fd_ser, rbuffer_crc, 10, 10, 0, 10);
strcat(rbuffer, rbuffer_crc);
////////////////////////////////////////////////
//Write directly into GPSdata.txt
////////////////////////////////////////////////
fprintf(fp_file, "%s", rbuffer);
printf("******Data from GPS******\n%s\n", rbuffer);
memset(rbuffer, 0, 300); //clear buffer
memset(rbuffer_crc, 0, 12);
}
//////////////////////////////////////////////////////
//Clear the forwarding character '*'
//////////////////////////////////////////////////////
tcgetattr(fd_ser, &termios_p);
termios_p.c_cc[VFWD] = 0x00;
tcsetattr(fd_ser, TCSANOW, &termios_p);
////////////////////////
// Close the file
////////////////////////
close( fd_ser );
fclose(fp_file);
printf("GPS process done!\n");
return EXIT_SUCCESS;
}