/* This program will work by crontab */ #include #include #include #include #include #include #define READDATA "/proc/adc%d" #define ADCPIN (2) #define ADCPIN2 (3) #define LEVEL (900) #define TRUE (1) #define FALSE (0) #define BOOL int #define INTERVAL (60) /* Seconds */ #define INT2 (240) /* Seconds */ #define LOGFILE "/xx/xx/log.txt" #define SERVER "xxx.xxx.xxx.xxx" #define PORT (8000) #define ADJ (0.75) BOOL flag; extern int ConnectServer(char *,int); extern int SendDataToServer(int,char *,int); /* Signal Handler */ void timeover(int x) { flag = TRUE; } measureRAD(void) { char FileName[128]; char FileName2[128]; char FileName3[128]; FILE *fp,*fp2,*fp3; int exval; int cnt,cnth,cntl,cntx; float msv; time_t now; char *nowtime; char *p; char tmps[128]; int tmpd; char DataLine[2048]; int len; int fdx; sprintf(FileName,READDATA,ADCPIN); (void)signal(SIGALRM,timeover); alarm(INTERVAL); cnt = 0; flag = FALSE; for(exval = 0 ;;) { char buff[20]; char *buff2; int temp; int i; if((FILE *)NULL == (fp = fopen(FileName,"r"))) { printf("Cannot open FILE[%s]\n",FileName); exit(1); } (void)fgets(buff,10,fp); if(ferror(fp)) { printf("Cannot read FILE[%s]\n",FileName); exit(1); } fclose(fp); for(i = 0 ; i < 20 ; ++i) if(((char)('\n')) == buff[i]) buff[i] = (char)0; buff2 = buff + 5; temp = atoi(buff2); if(temp > LEVEL) { if(exval > LEVEL) { ; } else { cnt++; /* Rising Edge */ } } else ; exval = temp; if(flag) { alarm(0); break; } } (void)time(&now); /* Get time for now */ nowtime = ctime(&now); /* convert to strings */ for(p = nowtime ; (*p) != (char)0 ; p++) if((char)('\n') == (*p)) { *p = (char)0; break; } sprintf(FileName2,READDATA,ADCPIN2); if((FILE *)NULL == (fp2 = fopen(FileName2,"r"))) { printf("Cannot Open temp. file[%s]\n",FileName2); exit(1); } (void)fgets(tmps,10,fp2); if(ferror(fp2)) { printf("Cannot read FILE[%s]\n",FileName2); exit(1); } fclose(fp2); tmpd = atoi(tmps + 5); tmpd = (tmpd - 180) / 6; cntx = (cnt * 3) / 1000; cnth = cntx / 100; cntl = cntx % 100; msv = (((float)cnt) * ((float)(3.3))) / ((float)(100000)); msv = msv * ((float)ADJ); // sprintf(DataLine,"%s [%06d cpm][ %03d.%02d uSv/h ] Temp[%04d deg/cent.]\n",nowtime,cnt,cnth,cntl,tmpd); sprintf(DataLine,"%s [%06d cpm][ %6.2f uSv/h ] Temp[%04d deg/cent.]\n",nowtime,cnt,msv,tmpd); if((FILE *)NULL == (fp3 = fopen(LOGFILE,"a"))) { printf("Cannot open File[%s]\n",LOGFILE); return; } (void)fputs(DataLine,fp3); if(ferror(fp3)) { printf("Cannot write for add Line\n"); return; } fclose(fp3); if(0 > (fdx = ConnectServer(SERVER,PORT))) { printf("Cannot Connect Server[%s]\n",SERVER); exit(1); } if(0 > SendDataToServer(fdx,DataLine,strlen(DataLine))) { printf("Cannot send data to server.\n"); exit(1); } close(fdx); } main() { measureRAD(); }