Dear users,
I've got a problem witch is driving me crazy. I have a RS-485 connection with a platform. I use a FTDI chip in my usb port to connect to it. I get the data with open, read and write. This all works good, and does not make any problem.
After that, I push everything in a file (with only a write, because later on I send it to the next pi, witch is a server)
So, the program function is quit simple:
-Get the data from the platform
-Write it ins a file
On my Ubuntu laptop there is absolutely no problem, it runs smoothly.
This all works fine, but sometimes my raspberry freezes. It wan't do anything, and i need a hard reset. It's is not on a fixed time: the first time it runned for 1 hour, the second time for 5 minutes.
I already tried to:
1. Flush all the buffers
2. Searched for a memory leak (with top, and nothing appears to be)
3. A lot witch I don't remember now.
Can anyone please help me? It's giving me really big headaches!
Code:#include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <time.h> #include <fcntl.h> #define MY_ID_H 0x01 #define MY_ID_L 0x00 #define Try_Again_Max 0x04 #define Timout_Sec 0x04 /* This is made by Libpgeak. */ static unsigned char output[9]; static unsigned char input[100]; static int Create_file(long long unsigned ntptime, int Power, float Rotation); static int Serial_to_int(unsigned char *input, char NumberOfBytes); static long long unsigned ntp_time(void); static unsigned char Get_Checksum(int NumberOfBytes); static int my_itoa(int val, char* buf); int main() { int PSERIAL; int i,j; int TimeNow=0; int TimeSo=0; int TryCount=0; char succes=0; long long unsigned ntptime=0; unsigned char TargetID[2]; float Rotation=0; int Power=0; system("stty -F /dev/ttyUSB0 9600 -parenb -parodd cs8 -hupcl -cstopb cread clocal -crtscts" " ignbrk -brkint ignpar -parmrk -inpck -istrip -inlcr -igncr -icrnl -ixon -ixoff" " -iuclc -ixany -imaxbel -iutf8" " -opost -olcuc -ocrnl -onlcr -onocr -onlret -ofill -ofdel nl0 cr0 tab0 bs0 vt0 ff0" " -isig -icanon -iexten -echo -echoe -echok -echonl -noflsh -xcase -tostop -echoprt" " -echoctl -echoke time 1"); while(1) { usleep(50000); usleep(50000); PSERIAL = open("/dev/ttyUSB0", O_RDWR | O_NOCTTY | O_NDELAY); if(PSERIAL ==-1) { printf("Could not open write connection with FTDI-chip, closing program\n\r"); return 100; } printf("Connected to FTDI chip\n\r"); fcntl(PSERIAL, F_SETFL, FNDELAY); //Get the slave ID. output[0] = 0x00; output[1] = 0x00; output[2] = MY_ID_L; output[3] = MY_ID_H; output[4] = 0xA1; output[5] = 0xB0; output[6] = 0xC0; output[7] = 0x00; output[8] = Get_Checksum(8); succes=0; while(!succes) { i=write (PSERIAL, output, 9); if (i < 0) { printf("write() of C1 command failed!\n\r"); succes=0; TryCount++; } if(TryCount==Try_Again_Max) { printf("Error at sending, Errorcode 0x01\n\r"); close(PSERIAL); return 0x01; } else { printf("RS485 send was succesfull \n\r"); succes=1; } } usleep(50000); i=0; TimeNow =ntp_time(); TimeSo = TimeNow+Timout_Sec; while(i<1) //Timeout after a several secconds? { i=read(PSERIAL, input, sizeof(input)); TimeNow =ntp_time(); if(TimeNow==TimeSo) { printf("Timeout at receiving, Errorcode 0x20\n\r"); return 0x20; } } for(j=0; j<i; j++) printf("%x ",input[j]); printf("\n\r"); usleep(50000); TargetID[0] = input[2]; TargetID[1] = input[3]; // Now we know the ID, we can ask for monitordata output[0] = TargetID[0]; output[1] = TargetID[1]; output[2] = MY_ID_L; output[3] = MY_ID_H; output[4] = 0x07; output[5] = 0x10; output[6] = 0xA4; output[7] = 0x00; output[8] = Get_Checksum(8); i=0; TryCount=0; succes=0; while(!succes) { i=write (PSERIAL, output, 9); if (i < 0) { printf("write() of B6 command failed!\n\r"); succes=0; TryCount++; } if(TryCount==Try_Again_Max) { printf("Error at sending, Errorcode 0x02\n\r"); close(PSERIAL); return 0x02; } else { printf("RS485 send was succesfull \n\r"); succes=1; } } usleep(50000); printf("reading information\n\r"); i=0; TimeNow =ntp_time(); TimeSo = TimeNow+Timout_Sec; while(i<1) //Timeout after a several seconds? { i=read(PSERIAL, input, sizeof(input)); TimeNow = ntp_time(); if(TimeNow==TimeSo) { printf("Timeout at receiving, Errorcode 0x20\n\r"); close(PSERIAL); return 0x20; } } //for(j=0; j<i; j++) //printf("%x ",input[j]); //printf("\n\r"); close(PSERIAL); //Done, and close the connection Power = Serial_to_int(&input[18], 2); //printf("Actual Power is %d\n\r", Power); Rotation = Serial_to_int(&input[20], 3); //printf("Actual Rotation is %f\n\r", Rotation); ntptime=ntp_time(); ntptime= ntptime << 32; Create_file(ntptime, Power, Rotation); //this function has to be more complete usleep(50000); fflush(stdout); fflush(stdin); printf("C-code is finished %\n\r"); } return 1; } static long long unsigned ntp_time(void) { time_t sec=0; sec = time(NULL) + 2208988800; return sec; } static int Create_file(long long unsigned ntptime, int Power, float Rotation) { int errorhandler; FILE *PFILE; char buffer[10]; int lenght; int i=0; Rotation = Rotation / 100; printf("the timestamp is %llu\n\r",ntptime); lenght = my_itoa(Power, buffer); PFILE = fopen ("jsondata","w"); if (PFILE==NULL) { printf("Could not make JSONDATA, closing program\n\r"); return 200; } printf("Creating File\n\r"); errorhandler=fprintf(PFILE,"The Power is %d \n\r" "The Rotation is %F \n\r" "Timestamp is %llu\n\r",Power, Rotation, ntptime); if(errorhandler<1) { printf("fprintf failed, Errorcode 0x63\n\r"); fflush(PFILE); fclose(PFILE); return 0x63; } usleep(50000); errorhandler=fflush (PFILE); if(errorhandler !=0) { printf("failure 0x64\n\r"); return 0x64; } errorhandler=fclose (PFILE); if(errorhandler != 0) { printf("failure 0x65\n\r"); return 0x65; } printf("Done Filewriting \n\r"); return 1; } //Addres first byte from input string, and number of bytes static int Serial_to_int(unsigned char *SerialBytes, char NumberOfBytes) { int i=0; int j=0; int output=0; while(NumberOfBytes>0) { output+= (SerialBytes[j] << i); printf("byte is %x\n\r",SerialBytes[j]); i+=8; j++; NumberOfBytes--; } return output; } static unsigned char Get_Checksum(int NumberOfBytes) { int j=0; int i=0; unsigned char outputt=0; while(NumberOfBytes>0) { i += output[j]; j++; NumberOfBytes--; } outputt = (i & 0xff); return outputt; } static int my_itoa(int val, char* buf) { const unsigned int radix = 10; char* p; unsigned int a; //every digit int len; char* b; //start of the digit char char temp; unsigned int u; p = buf; if (val < 0) { *p++ = '-'; val = 0 - val; } u = (unsigned int)val; b = p; do { a = u % radix; u /= radix; *p++ = a + '0'; } while (u > 0); len = (int)(p - buf); *p-- = 0; //swap do { temp = *p; *p = *b; *b = temp; --p; ++b; } while (b < p); return len; }



2Likes
LinkBack URL
About LinkBacks


