sensConf funtion modified

master
Sayeth 5 years ago
parent 08ad9cddc9
commit 77ce5114b3

81
daq.c

@ -39,54 +39,53 @@ int DAQ(int t_hrs, int tm_s);
int main(int argc, char *argv[]){
//char bioxCsen[], OXsen[], monoxCsen[];
int T,t;
if(argc!=3){
printf("Invalid number of arguments,\n usage: daq <time(h)> <sampling period(s)>\nexiting!\n");
return -2;
}
//configuración de sensores
printf("Configuring CO2 sensor\n");
sensConf(GC0017, B9600, Pulling_Mode, " K 00002\r\n");
sensConf(GC0017, B9600, FILnUNFIL, " M 00006\r\n");
sensConf(GC0017, B9600, Pulling_Mode, " K 00002\r\n", 5);
sensConf(GC0017, B9600, FILnUNFIL, " M 00006\r\n", 5);
printf("Configuring CO sensor\n");
sensConf(CM31911, B9600, Pulling_Mode, "K 00002\r\n");
sensConf(CM31911, B9600, M_zZTHBD, "M 14406\r\n");
sensConf(CM31911, B9600, Pulling_Mode, "K 00002\r\n", 5);
sensConf(CM31911, B9600, M_zZTHBD, "M 14406\r\n", 5);
printf("Configuring O2 sensor\n");
sensConf(OX0052, B9600, OX_P_Mode, "M 01\r\n");
sensConf(OX0052, B9600, OX_P_Mode, "M 01\r\n", 5);
//adquisición
T = atoi(argv[1]);//str to int
t = atoi(argv[2]);
printf("Starting data acquisition with duration of %dh and every %ds\n", T, t);
DAQ(T, t);
sleep(1);
//finalización
//deinit uart
uartClose(GC0017);
uartClose(CM31911);
uartClose(OX0052);
printf("Exiting of the program...\n");
return 0;
}
int sensConf(unsigned char uartNumber, int baudRate, unsigned char mode[], unsigned char response[]){
int sensConf(unsigned char uartNumber, int baudRate, unsigned char mode[], unsigned char response[], int tries){
int count;
int i = 0;
uartConf(uartNumber, baudRate);
while( i < 10 ){
while(tries){
uartTransmit(uartNumber, mode);
tcdrain(uartFile[uartNumber]); //wait all data has been sent
printf("Command sended.\n");
@ -94,19 +93,19 @@ int sensConf(unsigned char uartNumber, int baudRate, unsigned char mode[], unsig
count = uartReceive(uartNumber);
if (count == 0) printf("There was no data available to read!\n");
else if (strcmp(receive[uartNumber], response) == 0) {
printf("Sensor configurated after %d tries.\n", i+1);
printf("Sensor configurated.\n");
return 0;
} else {
printf("The following was read in [%d]: %s\n",count,receive[uartNumber]);
char *c = receive[uartNumber];
while (*c != '\0'){
printf("%d = '%c'\n",*c, *c);
c++;
}
//char *c = receive[uartNumber];
//while (*c != '\0'){
// printf("%d = '%c'\n",*c, *c);
// c++;
//}
}
i ++;
tries --;
}
printf("CO2 Sensor configuration failed.\n");
printf("Sensor configuration failed.\n");
return -1;
}
@ -149,12 +148,12 @@ int DAQ(int t_hrs, int tm_s)
co2_temp[10]="", o2_temp[10]="",
co2_press[10]="", o2_press[10]="",
co2_relH[10]="", DATA[100]="";
clock_t start_t, end_t;
time_t new_time, prev_time, t0;
double tm_ms = tm_s*1e6 - 0.20e6;
double dif = tm_ms;
//registro datos de inicio
dfp = fopen(data_file_path, "w"); // open file for writing
time_t curtime;
@ -164,30 +163,30 @@ int DAQ(int t_hrs, int tm_s)
"\tPressure C02(.mBar)\tPresure 02(mBar)\tRelative Humidity(.)\n",
ctime(&curtime), '%'); // send the value to the file
fclose(dfp); // close the file using the file pointer
//ciclo
t0 = time(NULL) + tm_s;
new_time = t0;
for(int i = 0; i < (t_hrs*3600); i=i+tm_s){
if (dif <= 0) printf("T=%d Communication time exceded.\n", (int)(new_time-t0));
else usleep((int)dif);
if (time(NULL) < new_time) while(new_time != time(NULL));
else printf("T=%d time error.\n", (int)(new_time-t0));
start_t = clock();
new_time = time(NULL);
//command transmitions
uartTransmit(CM31911, get_readigns);
uartTransmit(OX0052, Readings_OX);
uartTransmit(GC0017, get_readigns);
//uart receptions
uartReceive(OX0052);
uartReceive(CM31911);
uartReceive(GC0017);
memcpy(co2_uf, gmeasures(receive[GC0017], 'z', 5), 5);
memcpy(co2_f, gmeasures(receive[GC0017], 'Z', 5), 5);
memcpy(co_uf, gmeasures(receive[CM31911], 'z', 5), 5);
@ -199,11 +198,11 @@ int DAQ(int t_hrs, int tm_s)
memcpy(co2_press,gmeasures(receive[CM31911], 'B', 5), 5);
memcpy(o2_press, gmeasures(receive[OX0052], 'P', 4), 4);
memcpy(co2_relH, gmeasures(receive[CM31911], 'H', 5), 5);
sprintf(DATA, "%d\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s",
(int)(new_time-t0), co2_uf, co2_f, co_uf, co_f, o2_ppm, o2_xcent,
co2_temp, o2_temp, co2_press, o2_press, co2_relH);
printf("time(s)\tCO2 unfil(ppm)\tCO2 fil(ppm)\tCO unfil(ppm)\tCO fil(ppm)"\
"\tO2(ppm)\tO2(%c)\tTemperaure C02(ºC*10)\tTemperaure 02(ºC)"\
"\tPressure C02(.mBar)\tPresure 02(mBar)\tRelative Humidity(.)\n", '%');
@ -214,19 +213,19 @@ int DAQ(int t_hrs, int tm_s)
fclose(dfp); // close the file using the file pointer
new_time += tm_s;
end_t = clock();
dif = tm_ms - ((double)(end_t - start_t)*1e6 / CLOCKS_PER_SEC);
}
return 0;
}
char *gmeasures(char src[], char fval, int nchar){
char * ptr = &src[0];
static char s[10]="";
while(*ptr != fval) ptr++;
ptr += 2;
memcpy(s, ptr, nchar);
return s;
}
}

Loading…
Cancel
Save