master
Sayeth Rodriguez 4 years ago
parent b0b3f2d021
commit 0b821ba02b

88
daq.c

@ -1,73 +1,9 @@
/*
*/
#include "daq.h"
#include<stdio.h>
#include<fcntl.h>
#include<unistd.h>
#include<string.h>
#include<stdlib.h>
#include<time.h>
#include"uart.c"
//DEFINITIOS
#define data_file_path "./DATA/data.dat"
#define SprintIR 4 //co2 sensor
#define COAF 1 //co sensor
#define LOX_O2 2 //o2
#define Rep_Dev_ID "Y\r\n"
#define Pulling_Mode "K 2\r\n"
#define OX_P_Mode "M 1\r\n"
#define FILnUNFIL "M 6\r\n"
#define unf_gas_con "z\r\n"
#define fil_gas_con "Z\r\n"
#define Temperature "T\r\n"
#define get_readigns "Q\r\n"
#define percent_oxigen "%\r\n"
#define ppm_oxigen "O\r\n"
#define M_zZTHBD "M 14406\r\n"
#define M_zZTHB "M 12358\r\n"
#define Readings_OX "A\r\n"
int sensConf(unsigned char uartNumber, int baudRate, unsigned char mode[], unsigned char response[], int tries);
int measure(unsigned char uartNumber, unsigned char command[], int multiplier, int excess);
char *getMeasures(char src[], char fval, int nchar);
int str2int(unsigned char *ptr);
int DAQ(int t_hrs, int sp_s);
int main(int argc, char *argv[]){
int T_acq, t_samp;
if(argc!=3){
printf("Invalid number of arguments,\n"\
"Usage: daq <daq duration(h)> <sampling period(s)>\nexiting!\n");
return -2;
}
//configuración de sensores
printf("Configuring CO2 sensor\n");
sensConf(SprintIR, B9600, Pulling_Mode, " K 00002\r\n", 5);
sensConf(SprintIR, B9600, FILnUNFIL, " M 00006\r\n", 5);
printf("Configuring CO sensor\n");
sensConf(COAF, B9600, Pulling_Mode, "K 00002\r\n", 5);
sensConf(COAF, B9600, M_zZTHB, "M 14406\r\n", 5);
printf("Configuring O2 sensor\n");
sensConf(LOX_O2, B9600, OX_P_Mode, "M 01\r\n", 5);
//adquisición
T_acq = atoi(argv[1]);//str to int
t_samp = atoi(argv[2]);
printf("Starting data acquisition with duration of %dh every %ds\n", T_acq, t_samp);
DAQ(T, t);
//deinit uart
uartClose(SprintIR);
uartClose(COAF);
uartClose(LOX_O2);
//finalización
printf("Exiting of the program...\n");
return 0;
}
int sensConf(unsigned char uartNumber, int baudRate, unsigned char mode[], unsigned char response[], int tries)
{
@ -102,14 +38,14 @@ int DAQ(int t_hrs, int sp_s)
{
FILE* dfp; // create a file pointer
const char colums[200] = "t(s)\tCO2 uf(ppm)\tCO2(ppm)\tCO uf(ppm)\t"\
"CO(ppm)\tppO2(mbar)\tO2(%%)\tT C0(ºC*10)\t"\
"T 02(ºC)\tP C0(.mBar)\tP 02(mBar)\tRH(.)\n";
unsigned char co2_uf[10]="", co2_f[10]="",
"CO(ppm)\tppO2(mbar)\tO2(%%)\tT C0(°C*10)\t"\
"T O2(°C)\tP C0(.mBar)\tP 02(mBar)\tRH(.)\n";
char co2_uf[10]="", co2_f[10]="",
co_uf[10]="", co_f[10]="",
o2_ppm[10]="", o2_xcent[10]="",
co_temp[10]="", o2_temp[10]="",
co_press[10]="", o2_press[10]="",
co_relH[10]="", DATA[100]=""; //measurements varirables
co_relH[10]="", DATA[200]=""; //measurements varirables
time_t curtime; //current time (date)
clock_t start_t, end_t; //processing time measurements variables
time_t next_samp_time, t0; //time control variables
@ -120,7 +56,7 @@ int DAQ(int t_hrs, int sp_s)
//registro datos de inicio
dfp = fopen(data_file_path, "w"); // open file for writing
// send the value to the file
fprintf(dfp, "Starting DAQ at %s\n", ctime(&curtime);
fprintf(dfp, "Starting DAQ at %s\n", ctime(&curtime));
fclose(dfp); // close the file using the file pointer
printf("%s", colums); //display variables colums
//ciclo
@ -141,7 +77,7 @@ int DAQ(int t_hrs, int sp_s)
start_t = clock(); //saving start time
uartTransmit(COAF, get_readigns); //transmiting commands to sensors
uartTransmit(LOX_O2, Readings_OX);
uartTransmit(SprintIR, get_readigns
uartTransmit(SprintIR, get_readigns);
uartReceive(COAF); //receiving replys from
uartReceive(LOX_O2);
uartReceive(SprintIR);
@ -150,12 +86,12 @@ int DAQ(int t_hrs, int sp_s)
memcpy(co2_f, getMeasures(receive[SprintIR],'Z', 5), 5);
memcpy(co_uf, getMeasures(receive[COAF], 'z', 5), 5);
memcpy(co_f, getMeasures(receive[COAF], 'Z', 5), 5);
memcpy(o2_ppm, getMeasures(receive[LOX_O2], 'O', 6), 6);
memcpy(o2_xcent, getMeasures(receive[LOX_O2], '%', 6), 6);
memcpy(o2_ppm, getMeasures(receive[LOX_O2], 'O', 6), 6);
memcpy(o2_xcent, getMeasures(receive[LOX_O2], '%', 6), 6);
memcpy(co_temp, getMeasures(receive[COAF], 'T', 5), 5);
memcpy(o2_temp, getMeasures(receive[LOX_O2], 'T', 5), 5);
memcpy(o2_temp, getMeasures(receive[LOX_O2], 'T', 5), 5);
memcpy(co_press, getMeasures(receive[COAF], 'B', 5), 5);
memcpy(o2_press, getMeasures(receive[LOX_O2], 'P', 4), 4);
memcpy(o2_press, getMeasures(receive[LOX_O2], 'P', 4), 4);
memcpy(co_relH, getMeasures(receive[COAF], 'H', 5), 5);
//saving formated measurements in string DATA
sprintf(DATA,

41
daq.h

@ -0,0 +1,41 @@
#ifndef DAQ_H
#define DAQ_H
//HEADERS
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include "uart.h"
//DEFINITIONS
#define SprintIR 4 //co2 sensor
#define COAF 1 //co sensor
#define LOX_O2 2 //o2
//comands
#define Rep_Dev_ID "Y\r\n"
#define Pulling_Mode "K 2\r\n"
#define OX_P_Mode "M 1\r\n"
#define FILnUNFIL "M 6\r\n"
#define unf_gas_con "z\r\n"
#define fil_gas_con "Z\r\n"
#define Temperature "T\r\n"
#define get_readigns "Q\r\n"
#define percent_oxigen "%\r\n"
#define ppm_oxigen "O\r\n"
#define M_zZTHBD "M 14406\r\n"
#define M_zZTHB "M 12358\r\n"
#define Readings_OX "A\r\n"
//FUNCTIONS
int sensConf(unsigned char uartNumber, int baudRate, unsigned char mode[], unsigned char response[], int tries);
int measure(unsigned char uartNumber, unsigned char command[], int multiplier, int excess);
char *getMeasures(char src[], char fval, int nchar);
int str2int(unsigned char *ptr);
int DAQ(int t_hrs, int sp_s);
#endif

@ -0,0 +1,36 @@
#include "daq.h"
int main(int argc, char *argv[]){
int T_acq, t_samp;
if(argc!=3){
printf("Invalid number of arguments,\n"\
"Usage: daq <daq duration(h)> <sampling period(s)>\nexiting!\n");
return -2;
}
// configuring sensors
printf("Configuring CO2 sensor\n");
sensConf(SprintIR, B9600, Pulling_Mode, " K 00002\r\n", 5);
sensConf(SprintIR, B9600, FILnUNFIL, " M 00006\r\n", 5);
printf("Configuring CO sensor\n");
sensConf(COAF, B9600, Pulling_Mode, "K 00002\r\n", 5);
sensConf(COAF, B9600, M_zZTHB, "M 14406\r\n", 5);
printf("Configuring O2 sensor\n");
sensConf(LOX_O2, B9600, OX_P_Mode, "M 01\r\n", 5);
// acquiring
T_acq = atoi(argv[1]);//str to int
t_samp = atoi(argv[2]);
printf("Starting data acquisition with duration of %dh every %ds\n", T_acq, t_samp);
DAQ(T_acq, t_samp);
// deinit uart
uartClose(SprintIR);
uartClose(COAF);
uartClose(LOX_O2);
// finishing
printf("Exiting of the program...\n");
return 0;
}

@ -4,11 +4,8 @@
//libraries
#include "uart.h"
//paths and global variables
//file paths and global variables
#define incomplet_uart_path "/dev/ttyO"
int uartFile[6]; //file descriptor
unsigned char receive[6][100]; //declare a buffer for receiving data
struct termios options; //The termios structure
// *** FUNCTIONS ***
@ -23,7 +20,7 @@ int uartConf(unsigned char uartNumber, int baudRate)
else{
perror("Wrong UART number. " \
"UART numbers availables 1, 2, 4 or 5.\n");
return 2;
return UART_NUMBER_INCORRECT;
}
//openign uart file
printf("Configuring UART%d.\n", uartNumber);
@ -40,7 +37,7 @@ int uartConf(unsigned char uartNumber, int baudRate)
options.c_lflag = 0;
options.c_line = 0;
options.c_oflag = 0;
options.c_ospeed = 0
options.c_ospeed = 0;
// Set up the communications options:
// 8-bit, enable receiver, no modem control lines
options.c_cflag = baudRate | CS8 | CREAD | CLOCAL;
@ -51,14 +48,14 @@ int uartConf(unsigned char uartNumber, int baudRate)
tcsetattr(uartFile[uartNumber], TCSANOW, &options); //changes occur immmediately
printf("UART%d configurated.\n", uartNumber);
return 0;
return UART_FUNCTION_SUCCESSFUL;
}
int uartClose(unsigned char uartNumber)
{
printf("Closing UART%d.\n", uartNumber);
close(uartFile[uartNumber]);
return 0;
return UART_FUNCTION_SUCCESSFUL;
}
int uartTransmit(unsigned char uartNumber, unsigned char message[])
@ -72,7 +69,7 @@ int uartTransmit(unsigned char uartNumber, unsigned char message[])
}
tcflush(uartFile[uartNumber], TCOFLUSH);
return 0;
return UART_FUNCTION_SUCCESSFUL;
}
int uartReceive(unsigned char uartNumber)

@ -1,22 +1,3 @@
/*
* Copyright (C) 2019 Akshay Patwardhan
* akshay.patwardhan@outlook.com
*
* Please go through readme.md for usage and other instructions, if any.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
// Guarding macro start
#ifndef UART_H
#define UART_H
@ -31,22 +12,24 @@
#include <unistd.h>
#include <sys/types.h>
#include <string.h>
#include <stdint.h>
// Error codes and return values
#define UART_FUNCTION_SUCCESSFUL 0
#define UART_NUMBER_INCORRECT 1
#define UART_BAUDRATE_INCORRECT 2
/*#define UART_BAUDRATE_INCORRECT 2
#define UART_FIFO_ERROR 3
#define UART_INCORRECT_PATH 4
#define UART_INCORRECT_PATH 4*/
//VARIABLES
int uartFile[6]; //file descriptor
unsigned char receive[6][100]; //declare a buffer for receiving data
// Function declarations
/*
* uartNumber as unsigned char (8 bits) to reduce RAM usage
*/
int uartConf(unsigned char uartNumber, int baudRate);
int uartClose(unsigned char uartNumber);
int uartTransmit(unsigned char uartNumber, unsigned char message[]);
int uartReceive(unsigned char uartNumber);
int uartConf(uint8_t uartNumber, int baudRate);
int uartClose(uint8_t uartNumber);
int uartTransmit(uint8_t uartNumber, unsigned char message[]);
int uartReceive(uint8_t uartNumber);
// Guarding macro end
#endif // UART_H

Loading…
Cancel
Save