//#include "/home/eds7eb/Downloads/CAENCLib/5.77Wrapper/Linux/include/CAENHVWrapper.h" #include "CAENHVWrapper.h" #include "stdio.h" #include "gpib/ib.h" #include #include #include //Time to monitor each channel, in seconds #define MON_TIME 43200 struct timeval tval_before, tval_during, tval_after, tval_keith, tval_snmp, tval_total; unsigned short ChList[16]; float VoltMon[16]; float VoltOn[16],VoltOff[16]; //float tempVoltage[1]; //unsigned short tempChan[1]; unsigned int tempPowerOn[16],tempPowerOff[16]; unsigned short NrOfSl,*SerNumList,*NrOfCh; char *ModelList,*DescriptionList; unsigned char *FmwRelMinList,*FmwRelMaxList; int handle=-1; CAENHVRESULT status; CAENHVRESULT ret; volatile int ibstatest; int devdesc; unsigned char devbuf[2048]; int devdatalen=0; void gpiberr (char *msg); void *CAENThread (const int* funcCount); void *KeithThread (const int* funcCount); sem_t KeithStart,CAENStart,KeithDone,CAENDone; int j=0,timer=0,loadChannel=0,KeithErr=0; int main () { //freopen("./err.txt","a",stderr); //freopen("./out.txt","a",stdout); pthread_t keith_id; pthread_t CAEN_id; sem_init(&KeithStart,0,0); sem_init(&CAENStart,0,0); sem_init(&KeithDone,0,0); sem_init(&CAENDone,0,0); pthread_create(&CAEN_id,NULL,CAENThread,&timer); pthread_create(&keith_id,NULL,KeithThread,&timer); //GPIB devdesc = ibdev(0, 16, 0, 10, 1, 0x0000); //Files FILE* fout_CAEN = fopen("./temp_CAENMTnew1","w"); FILE* fout_keith = fopen("./temp_KeithMTnew1","w"); FILE* fout_time = fopen("./temp_TimeMTnew1","w"); char timeCurrent[23],timeInfo[70],errtext[35]; int errflag=0; FILE* ferr = fopen("./err.txt","w"); int pin; //Initializations for (j=0;j<16;j++){ ChList[j]=j; } for (j=0;j<16;j++){ VoltOn[j]=65; VoltOff[j]=0; tempPowerOn[j]=1; tempPowerOff[j]=0; } for (loadChannel=1;loadChannel<16;loadChannel++) { //Connection is closed by client automatically, so, close and re-open between channels if (loadChannel%2==0) pin=12-loadChannel/2; if (loadChannel%2==1) pin=24-(loadChannel-1)/2; printf("Please set to Channel %i, which is pin %i\n",loadChannel,pin); fflush(NULL); getchar(); //Open connection status = CAENHV_InitSystem(3,LINKTYPE_TCPIP,"192.168.250.101","eds7eb","KillerWorm",&handle); printf("Initialized. %s Status: %#08x, Handle: %i\n",CAENHV_GetError(handle),status,handle); fflush(NULL); ret = CAENHV_GetCrateMap(handle, &NrOfSl, &NrOfCh, &ModelList, &DescriptionList, &SerNumList,&FmwRelMinList, &FmwRelMaxList); printf("CAENHV_GetCrateMap: %s (num. %d)\n", CAENHV_GetError(handle), ret); //Some basic error recognition if (status == CAENHV_OK) {printf("All Systems Go\n");} if (status == CAENHV_SYSERR) {printf("SYSERR");} if (status == CAENHV_WRITEERR) {printf("WRITEERR");} if (status == CAENHV_READERR) {printf("READERR");} if (status == CAENHV_TIMEERR) {printf("TIMERR");} if (status == CAENHV_DOWN) {printf("DOWN");} if (status == CAENHV_NOTPRES) {printf("NOTPRES");} if (status == CAENHV_NODATA) {printf("NODATA");} if (status == CAENHV_SOCKETERROR) {printf("SOCKETERROR");} if (status == CAENHV_COMMUNICATIONERROR) {printf("COMMUNICATIONERROR");} if (status == CAENHV_CONNECTED) {printf("CONNECTED");} if (status == CAENHV_NOTCONNECTED) {printf("NOTCONNECTED");} if (status == CAENHV_LOGINFAILED) {printf("LOGINFAILED");} //Set Channels to 0 //status = CAENHV_SetChParam(handle,4,"V0Set",16,ChList,VoltOff); //printf("Set Status: %#08X\n",status); //Monitor Channels //Set the voltage and turn on the channel //tempChan[0]=loadChannel; //tempVoltage[0]=65; CAENHV_SetChParam(handle,4,"V0Set",16,ChList,VoltOn); CAENHV_SetChParam(handle,4,"Pw",16,ChList,tempPowerOn); printf("Ramping up\n"); sleep(10); //Take data for (timer=0;timer66) { errflag=1; sprintf(errtext,"Line: %i Voltage: %f\n",timer,VoltMon[loadChannel]); fwrite(errtext,1,35,ferr); } fwrite(timeCurrent,1,23,fout_keith); fwrite(devbuf,1,ibcnt,fout_keith); sprintf(timeInfo,"Current:%ld.%06ld Keith:%ld.%06ld SNMP:%ld.%06ld Total:%ld.%06ld\n",(long int)tval_during.tv_sec,(long int)tval_during.tv_usec, (long int)tval_keith.tv_sec, (long int)tval_keith.tv_usec, (long int)tval_snmp.tv_sec, (long int)tval_snmp.tv_usec, (long int)tval_total.tv_sec, (long int)tval_total.tv_usec); fwrite(timeInfo,1,70,fout_time); fflush(stdout); } if(KeithErr==1) timer--; //Pretend nothing happened KeithErr=0; } //Turn off this channel CAENHV_SetChParam(handle,4,"V0Set",16,ChList,VoltOff); printf("Ramping down\n"); sleep(10); CAENHV_SetChParam(handle,4,"Pw",16,ChList,tempPowerOff); //fwrite(VoltOff,4,16,fout_CAEN); fwrite("EndOfChannel\n",1,13,fout_keith); //Shut Down CAENHV_DeinitSystem(handle); } printf("Shutting Down\n"); } void *CAENThread (const int* funcCount){ //int f=0; while (*funcCount