/* Compile with: gcc -Wall -o lvcard lvcard.c before this program set the baud rate to 38400, 8 bits, 1 stop bit, no parity: stty -F /dev/ttyUSB0 38400 cs8 -cstopb -parenb ixoff -icrnl -opost -onlcr -isig -icanon -echo -echoe To show settings: stty -a -F /dev/ttyUSB0 before use do: SCI SCS1 ROI3 Load dac >SCS5 >DI2 >DCR1 S00009E0000 8001 > DCLndddd > DI2 init to default values > DC0 - Dac Clear 5825 all (To TEST) > DC2 clear all channels (one by one) When settinsg off voltages do also set DAC to all 0, else will there be voltages though the SALTRO refvoltages. compile by: gcc -Wall -o currents currents-dev.c arguments: action,dump action: 0 - no action on power - read only 1 - switch on 1.5V/2.5V on lvcard (RA08) 2 - switch off 1.5V/2.5V on lvcard (RA00) 3 - switch on pasa & biasctrl 4 - switch on pasa 5 - switch on biasctrl dump: dump the result on stdout noread: read the adc after action performed (default is read) chip: chip number log: write to logfile (chip#-currents.log) check: (implicit read also) check and take action if outside limits logfile date,time,action, Init lvcard before power on: SCI S00009E0000 SCS1 S00009E0000 ROI3 S00009E0000 S00009E0000 to fix: First channel is read/sent first and last value. Program reads the ADCs of the lvcard, e.g: ARAn n=1 :: channel 1: input 2V1, channel 2-6: current 1V5 MCMA-E n=2 :: channel 1: input 3V1, channel 2-6: current 2V5 MCMA-E n=3 :: channel 1-5: output 1V5 MCMA-E n=4 :: channel 1-5: output 2V5 MCMA-E currents 2.5V (ARA2) output voltage 1.5V (ARA3) REPLY1: ARA1 0040 00004\r\n etc... 0000 00000 0000 00000 0000 00000 0000 00000 0010 00001 6AF0 01711 6F50 01781 0040 00004 > ENDREPLY1 REPLY2: ARA2 0030 00003 0000 00000 0000 00000 0000 00000 0000 00000 0000 00000 6A50 01701 6E30 01763 0030 00003 > ENDREPLY2 real 0m0,187s user 0m0,000s sys 0m0,003s >ARA1 C1B0 03099 0000 00000 0000 00000 0000 00000 0BC0 00188 0020 00002 69D0 01693 6E00 01760 C1B0 03099 > ARA2 C180 03096 0000 00000 0000 00000 0000 00000 01C0 00028 0000 00000 6A30 01699 6DE0 01758 C180 03096 > ARA3 0000 00000 0000 00000 0000 00000 5E20 01506 0000 00000 6AB0 01707 6AB0 01707 6EB0 01771 0000 00000 > ARA4 02C0 00044 0230 00035 02A0 00042 9C60 02502 02C0 00044 6B20 01714 6B80 01720 6EF0 01775 02C0 00044 > */ #include #include #include #include #include #include #include #include #include #include #include #define DEVICE "/dev/ttyUSB0" #define PROTOMCM_C 4 #define PROTOMCM_V 3 typedef struct { unsigned int adcvalue[8]; int converted[8]; } ADC; ADC adc[4]; int action = 0; int dumpdata = 0; int readadc = 1; int power = 0; int saltropower = 0; int checkvalues = 1; // default is to check values int checkstatus = 0; // status of checked values int logdata = 1; // default is to log data char csvfile[256]; int vin15_min = 2900; int vin15_max = 4000; int vin25_min = 2900; int vin25_max = 4000; int vout15_min = 1450; int vout15_max = 1550; int vout25_min = 2450; int vout25_max = 2550; int a15_min_on = 350; int a15_max_on = 450; int a25_min_on = 20; int a25_max_on = 30; int a15_min_off = 0; int a15_max_off = 100; int a25_min_off = 0; int a25_max_off = 30; int voff15_max = 50; int voff25_max = 50; int coff15_max = 50; int coff25_max = 50; #define NO_ACTION 0 #define LVCARD_INIT 1 #define LVCARD_READADC 2 #define LVCARD_SWITCH_ON 3 #define LVCARD_SWITCH_OFF 4 #define SALTRO_SWITCH_ON 5 #define SALTRO_SWITCH_OFF 6 int sendcommand(int devfd, char *cmd, char *reply); int DecodeAdc(char *reply,ADC *adc); int getParameters(int argc, char **argv); int getParameters(int argc, char **argv) { const char *short_options = "cdf:hip:rs:"; int option_index = 0; int c; int nbarg = 0; int nbactions = 0; const struct option long_options[]= { {"init",0,0,'i'}, {"filecsv",1,0,'f'}, {"nocheck",0,0,'c'}, {"power",1,0,'p'}, {"help",0,0,'h'}, {"read",0,0,'r'}, {"saltro",1,0,'s'}, {0,0,0,0}, }; action = 0; // default read only dumpdata = 0; // no dump of data readadc = 1; // read the adc checkvalues = 1; // check the values logdata = 0; // log the data on file if (argc > 1) { while ((c = getopt_long(argc, argv, short_options, long_options, &option_index)) != EOF) { switch (c) { case 'c': nbarg++; checkvalues = 0; break; case 'f': nbarg++; strcpy(csvfile,optarg); logdata = 1; break; case 'i': nbarg++; action = LVCARD_INIT; nbactions++; break; case 'l': nbarg++; logdata = 0; break; case 'h': puts("Arguments:"); puts("--nocheck,-c do not check data"); puts("--powdown,-d power down"); puts("--powup,-u power up"); puts("--help,-h"); puts("--init,-i initialize lvcard"); puts("--nolog,-l do not log data"); puts("--read,-r read adcs"); return(1); break; case 'r': nbarg++; action = LVCARD_READADC; nbactions++; break; case 'p': nbarg++; sscanf(optarg,"%d",&power); if (power == 0) action = LVCARD_SWITCH_OFF; else action = LVCARD_SWITCH_ON; nbactions++; break; case 's': nbarg++; nbactions++; sscanf(optarg,"%d",&saltropower); if (saltropower == 0) action = SALTRO_SWITCH_OFF; else action = SALTRO_SWITCH_ON; break; default: printf("%s: Unknown option %s\n",__FUNCTION__,argv[optind-1]); return 1; } } if (nbarg == 0) { puts("Arguments:"); puts("--nocheck,-c do not check data"); puts("--power,-p power 0-down, else up"); puts("--saltro,-s power is 0-down, else up"); puts("--help,-h"); puts("--init,-i initialize lvcard"); puts("--read,-r read adcs"); puts("--filecsv,-f csv logfile"); } } else { puts("Arguments:"); puts("--nocheck,-c do not check data"); puts("--powdown,-d power down"); puts("--powup,-u power up"); puts("--help,-h"); puts("--init,-i initialize lvcard"); puts("--nolog,-l do not log data"); puts("--read,-r read adcs"); return(1); } if ((checkvalues != 0) && (readadc == 0)) { printf("%s: check - force readout of ADC\n",__FUNCTION__); readadc = 1; } if (nbactions != 1) { printf("Can only have one actions specified %d\n",nbactions); return 1; } printf("LOG %s\n",csvfile); return 0; } int DecodeAdc(char *reply,ADC *adc) { char *ptr; char tmpbuf[512]; char *command; unsigned int adcvalue; int converted; int channel = 0; int n; strncpy(tmpbuf,reply,sizeof(tmpbuf)); ptr = strtok(tmpbuf,"\n"); // Returned command n = strlen(ptr); if (ptr[n-1] == '\r') ptr[n-1] = '\0'; // printf("CMD %s",ptr); // strncpy(command,ptr,sizeof(command)-1); command = ptr; while ((ptr = strtok(NULL,"\n")) != NULL) { // scan values n = strlen(ptr); if (ptr[n-1] == '\r') ptr[n-1] = '\0'; if (*ptr != '>') { // printf("PTR %s",ptr); sscanf(ptr,"%X %d",&adcvalue,&converted); // printf("CHANNEL: %d %X %d\n",channel,adcvalue,converted); if ((channel >= 0) && (channel <= 7)) { adc->adcvalue[channel] = adcvalue; adc->converted[channel] = converted; } else { fprintf(stderr,"%s: command %s, larger than 0-7 ADC channels found (%d)\n",__FUNCTION__,command,channel); return (1); } channel++; } puts(ptr); } return (0); } int sendcommand(int devfd, char *cmd, char *reply) { int istat; int iret; int pos = 0; istat = write(devfd,cmd,strlen(cmd)); if (istat < 0 ) { printf("WRITE returned %d error %s\n",istat,strerror(errno)); return (4); } else if (istat < strlen("cmd")) { printf("WRITE returned %d expected %ld\n",istat,strlen(cmd)); return (5); } usleep(40000); do { iret=read(devfd,&reply[pos],sizeof(reply)); if ( (iret != -1) && iret ) { pos += iret; reply[pos] = '\0'; // printf("%d %d %s\n",iret,pos,reply); } } while(iret > 0); if (strstr(reply,"collision") != NULL) return 1; else if (strstr(reply,"U?") != NULL) return 2; return(0); } int main(int argc, char *argv[]) { int devfd; int protomcm_c,protomcm_v; int iret = 0; char reply[512]; char cmd[64]; char mydev[64]; time_t ticks; struct tm *mytime; char datestring[128]; FILE *fcsv; // Parameters if (getParameters(argc,argv) != 0) exit(1); printf("Arguments: action %d dump %d\n",action,dumpdata); // Get date for logfile ticks = time(NULL); mytime = localtime(&ticks); strftime(datestring,sizeof(datestring),"%Y%m%d,%H%M%S",mytime); // printf("%s,%d\n",datestring,action); // return(0); memset(adc,0,sizeof(adc)); protomcm_c = PROTOMCM_C; protomcm_v = PROTOMCM_V; strcpy(mydev,DEVICE); // if ( argc != 2 ) return(0); // pstr = *++argv; devfd = open(mydev,O_RDWR | O_NONBLOCK); if (devfd < 0 ) { printf("OPEN returned %d error %s\n",devfd,strerror(errno)); return (3); } // Proper action reply[0] = '\n'; if (action == LVCARD_READADC) { strcpy(cmd,"SCS1\r"); if ((iret = sendcommand(devfd,cmd,reply)) != 0) { printf("Readadc %s failed %d: %s - EXITING\n",cmd,iret,reply); return 4; } } else if (action == LVCARD_SWITCH_ON) { strcpy(cmd,"SCS1\r"); if ((iret = sendcommand(devfd,cmd,reply)) != 0) { printf("Switch on %s failed %d: %s - EXITING\n",cmd,iret,reply); return 4; } strcpy(cmd,"ROA08\r"); if ((iret = sendcommand(devfd,cmd,reply)) != 0) { printf("Switch on %s failed %d: %s - EXITING\n",cmd,iret,reply); return 4; } strcpy(cmd,"SCS5\r"); if ((iret = sendcommand(devfd,cmd,reply)) != 0) { printf("Switch on %s failed %d: %s - EXITING\n",cmd,iret,reply); return 4; } strcpy(cmd,"DI2\r"); if ((iret = sendcommand(devfd,cmd,reply)) != 0) { printf("Switch on %s failed %d: %s - EXITING\n",cmd,iret,reply); return 4; } strcpy(cmd,"DCL30255\r"); if ((iret = sendcommand(devfd,cmd,reply)) != 0) { printf("Switch on %s failed %d: %s - EXITING\n",cmd,iret,reply); return 4; } strcpy(cmd,"DCL12221\r"); if ((iret = sendcommand(devfd,cmd,reply)) != 0) { printf("Switch on %s failed %d: %s - EXITING\n",cmd,iret,reply); return 4; } strcpy(cmd,"SCS1\r"); if ((iret = sendcommand(devfd,cmd,reply)) != 0) { printf("Switch on %s failed %d: %s - EXITING\n",cmd,iret,reply); return 4; } } // Initialize lvcard else if (action == LVCARD_INIT) { strcpy(cmd,"123\r"); sendcommand(devfd,cmd,reply); strcpy(cmd,"SCI\r"); if ((iret = sendcommand(devfd,cmd,reply)) != 0) { printf("Init %s failed %d: %s - EXITING\n",cmd,iret,reply); return 4; } strcpy(cmd,"SCS1\r"); if ((iret = sendcommand(devfd,cmd,reply)) != 0) { printf("Init %s failed %d: %s - EXITING\n",cmd,iret,reply); return 4; } strcpy(cmd,"ROI3\r"); if ((iret = sendcommand(devfd,cmd,reply)) != 0) { printf("Init %s failed %d: %s - EXITING\n",cmd,iret,reply); return 4; } } else if (action == LVCARD_SWITCH_OFF) { strcpy(cmd,"SCS5\r"); if ((iret = sendcommand(devfd,cmd,reply)) != 0) { printf("Switch off %s failed %d: %s - EXITING\n",cmd,iret,reply); return 4; } strcpy(cmd,"DC2\r"); if ((iret = sendcommand(devfd,cmd,reply)) != 0) { printf("Switch off %s failed %d: %s - EXITING\n",cmd,iret,reply); return 4; } strcpy(cmd,"SCS1\r"); if ((iret = sendcommand(devfd,cmd,reply)) != 0) { printf("Switch off %s failed %d: %s - EXITING\n",cmd,iret,reply); return 4; } strcpy(cmd,"ROA00\r"); if ((iret = sendcommand(devfd,cmd,reply)) != 0) { printf("Switch off %s failed %d: %s - EXITING\n",cmd,iret,reply); return 4; } } // read data - do not care about action if (readadc != 0) { strcpy(cmd,"ARA1\r"); if ((iret = sendcommand(devfd,cmd,reply)) != 0) { printf("Readadc %s failed %d: %s - EXITING\n",cmd,iret,reply); return 4; } DecodeAdc(reply,&adc[0]); adc[0].converted[protomcm_c] = 2 * adc[0].converted[protomcm_c]; // 10 milliohm resistor and an amplification of 50 before the ADC if (dumpdata != 0) { printf("REPLY1:\n %s\nENDREPLY1\n",reply); printf("VIN %X %d\n",adc[0].adcvalue[0],adc[0].converted[0]); printf("PROTOMCM %d: current 1.5V %X %d\n",protomcm_c,adc[0].adcvalue[protomcm_c],adc[0].converted[protomcm_c]); } strcpy(cmd,"ARA2\r"); if ((iret = sendcommand(devfd,cmd,reply)) != 0) { printf("Readadc %s failed %d: %s - EXITING\n",cmd,iret,reply); return 4; } DecodeAdc(reply,&adc[1]); if (dumpdata != 0) { printf("REPLY2:\n %s\nENDREPLY2\n",reply); printf("VIN %X %d\n",adc[1].adcvalue[0],adc[1].converted[0]); printf("PROTOMCM %d: current 2.5V %X %d\n",protomcm_c,adc[1].adcvalue[protomcm_c],adc[1].converted[protomcm_c]); } strcpy(cmd,"ARA3\r"); if ((iret = sendcommand(devfd,cmd,reply)) != 0) { printf("Readadc %s failed %d: %s - EXITING\n",cmd,iret,reply); return 4; } DecodeAdc(reply,&adc[2]); if (dumpdata != 0) { printf("REPLY3:\n %s\nENDREPLY1\n",reply); printf("VOUT PROTOMCM %d: 1.5V %X %d\n",protomcm_v,adc[2].adcvalue[protomcm_v],adc[2].converted[protomcm_v]); } strcpy(cmd,"ARA4\r"); if ((iret = sendcommand(devfd,cmd,reply)) != 0) { printf("Readadc %s failed %d: %s - EXITING\n",cmd,iret,reply); return 4; } DecodeAdc(reply,&adc[3]); if (dumpdata != 0) { printf("REPLY4:\n %s\nENDREPLY2\n",reply); printf("VOUT PROTOMCM %d: 2.5V %X %d\n",protomcm_v,adc[3].adcvalue[protomcm_v],adc[3].converted[protomcm_v]); } } // Checking values checkstatus = 0; if (checkvalues != 0) { if ((adc[0].converted[0] < vin15_min) || (adc[0].converted[0] > vin15_max)) { checkstatus |= 0x0001; } if ((adc[1].converted[0] < vin25_min) || (adc[1].converted[0] > vin25_max)) { checkstatus |= 0x0002; } if (checkstatus == 0) { if (action == LVCARD_SWITCH_ON) { if ((adc[2].converted[protomcm_v] < vout15_min) || (adc[2].converted[protomcm_v] > vout15_max)) { checkstatus |= 0x0004; } if ((adc[3].converted[protomcm_v] < vout25_min) || (adc[3].converted[protomcm_v] > vout25_max)) { checkstatus |= 0x0008; } if (adc[0].converted[protomcm_c] > a15_max_off) { checkstatus |= 0x0100; } if (adc[1].converted[protomcm_c] > a25_max_off) { checkstatus |= 0x0200; } } else if (action == LVCARD_SWITCH_OFF) { if (adc[2].converted[protomcm_v] > voff15_max) { checkstatus |= 0x0010; } if (adc[3].converted[protomcm_v] > voff25_max) { checkstatus |= 0x0020; } if (adc[0].converted[protomcm_c] > coff15_max) { checkstatus |= 0x0040; } if (adc[1].converted[protomcm_c] > coff25_max) { checkstatus |= 0x0080; } } else if (action == SALTRO_SWITCH_ON) { if ((adc[0].converted[protomcm_c] < a15_min_on) || (adc[0].converted[protomcm_c] > a15_max_on)) { checkstatus |= 0x0100; } if ((adc[1].converted[protomcm_c] < a25_min_on) || (adc[1].converted[protomcm_c] > a25_max_on)) { checkstatus |= 0x0200; } } else if (action == SALTRO_SWITCH_OFF) { if ((adc[0].converted[protomcm_c] < a15_min_off) || (adc[0].converted[protomcm_c] > a15_max_off)) { checkstatus |= 0x0400; } if ((adc[1].converted[protomcm_c] < a25_min_off) || (adc[1].converted[protomcm_c] > a25_max_off)) { checkstatus |= 0x0800; } } } } // Postwork close(devfd); if (logdata == 0) { // date,action,Vin1.5V,Vin2.5V,Vout1.5V,VoutV2.5V,A1.5V,A2.5V printf("%s,%d,%d,%d,%d,%d,%d,%d,%d\n", datestring,action,adc[0].converted[0],adc[1].converted[0],adc[2].converted[protomcm_v], adc[3].converted[protomcm_v],adc[0].converted[protomcm_c],adc[1].converted[protomcm_c],checkstatus); } else { // date,action,Vin1.5V,Vin2.5V,Vout1.5V,VoutV2.5V,A1.5V,A2.5V struct stat fileBuffer; int csvexist = 0; if (stat (csvfile, &fileBuffer) == 0) csvexist = 1; fcsv = fopen(csvfile,"a"); if (fcsv != NULL) { if (csvexist == 0) fprintf(fcsv,"date,time,action,Vin1.5V,Vin2.5V,Vout1.5V,VoutV2.5V,A1.5V,A2.5V,ErrorCode\n"); fprintf(fcsv,"%s,%d,%d,%d,%d,%d,%d,%d,%d\n", datestring,action,adc[0].converted[0],adc[1].converted[0],adc[2].converted[protomcm_v], adc[3].converted[protomcm_v],adc[0].converted[protomcm_c],adc[1].converted[protomcm_c],checkstatus); } else { printf("FILE: %s not opened\n",csvfile); return (1); } } if (checkstatus != 0) return (2); return (0); }