From aeee6ab05fdd7665f8f4970ae4ca0da0d4e448b3 Mon Sep 17 00:00:00 2001 From: sfeutrie Date: Fri, 15 Feb 2019 07:50:04 +0100 Subject: [PATCH] Ajout du fichier qui tournera sur la Raspberry --- Raspberry/config_experimentation.txt | 3 +++ Raspberry/config_raspberry.txt | 4 ++++ Raspberry/data | Bin 0 -> 13296 bytes Raspberry/data.c | 85 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ Raspberry/data.cpp | 8 ++++++++ Raspberry/listener | Bin 0 -> 18232 bytes Raspberry/listener.c | 196 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ Raspberry/res/result_exp_mouvement_2.txt | Bin 0 -> 245793 bytes Raspberry/res/result_exp_temperature_1.txt | 1 + Raspberry/res/result_exp_wrong_file.txt | 1 + 10 files changed, 298 insertions(+), 0 deletions(-) create mode 100644 Raspberry/config_experimentation.txt create mode 100644 Raspberry/config_raspberry.txt create mode 100644 Raspberry/data create mode 100644 Raspberry/data.c create mode 100644 Raspberry/data.cpp create mode 100644 Raspberry/listener create mode 100644 Raspberry/listener.c create mode 100644 Raspberry/res/result_exp_mouvement_2.txt create mode 100644 Raspberry/res/result_exp_temperature_1.txt create mode 100644 Raspberry/res/result_exp_wrong_file.txt diff --git a/Raspberry/config_experimentation.txt b/Raspberry/config_experimentation.txt new file mode 100644 index 0000000..07c3652 --- /dev/null +++ b/Raspberry/config_experimentation.txt @@ -0,0 +1,3 @@ +temperature_1 +mouvement_2 +wrong_file \ No newline at end of file diff --git a/Raspberry/config_raspberry.txt b/Raspberry/config_raspberry.txt new file mode 100644 index 0000000..4e3e59f --- /dev/null +++ b/Raspberry/config_raspberry.txt @@ -0,0 +1,4 @@ +temperature_1=/dev/ttyS3 +mouvement_1=/dev/ttyS7 +mouvement_2=/dev/ttyS4 +pression_1=/dev/ttyS9 \ No newline at end of file diff --git a/Raspberry/data b/Raspberry/data new file mode 100644 index 0000000..95dca7f Binary files /dev/null and b/Raspberry/data differ diff --git a/Raspberry/data.c b/Raspberry/data.c new file mode 100644 index 0000000..a01c254 --- /dev/null +++ b/Raspberry/data.c @@ -0,0 +1,85 @@ +#include +#include +#include +#include +#include +#include +#include +#include + +int pt; +struct termios tty, old; /* Create the structure */ + +void reset_tty(){ + tcsetattr(pt, TCSANOW, &old); + close(pt); +} + +int main(){ + int r=-1; + char * device = "/dev/ttyS3"; + pt = open(device, O_RDWR | O_NOCTTY | O_SYNC); + if(pt == -1){ + perror("open"); + exit(-1); + } + printf("open : %d\n",pt); + ioctl(pt, I_SRDOPT, RMSGD); + tcgetattr(pt, &old); + atexit(reset_tty); + + tcgetattr(pt, &tty); // Get the current attributes of the Serial port + + //cfmakeraw(&tty); + tty.c_iflag &= ~(BRKINT | ICRNL | INPCK | ISTRIP | IXON | IXOFF); + printf("iflag = %x\n",tty.c_iflag); + tty.c_oflag &= ~(OPOST); + printf("oflag = %x\n",tty.c_oflag); + tty.c_cflag |= (CS8); + tty.c_cflag &= ~(CSIZE|PARENB); + printf("cflag = %x\n",tty.c_cflag); + tty.c_lflag &= ~(ECHO|ECHONL|ICANON|ISIG|IEXTEN); + printf("lflag = %x\n",tty.c_lflag); + + tty.c_cc[VMIN] = 7; + tty.c_cc[VTIME] = 0; + cfsetispeed(&tty,B9600); // Setting the Baud rate + cfsetospeed(&tty,B9600); + + /*Peut-être inutile si on est en liaison série et pas en USB : le TCSAFLUSH devrait suffir*/ + sleep(1); + + if(tcflush(pt, TCIFLUSH)==-1){ + perror("tcflush"); + exit(-1); + } + + if(tcsetattr(pt, TCSANOW, &tty)==-1){ + perror("tcsetattr"); + } + + int i = 0; + char end[20]; + + if(cfgetispeed(&tty)!=B9600) + { + perror("cfgetispeed"); + exit(-1); + } + + int count = 0; + char line[20]; + memset(line,0,sizeof(line)); + + while(count<14){ + r = read(pt ,line,1); + printf("c = %c (characters read : %d)\n",line[0],r); + //printf("l = %c\n",line[0]); + count++; + memset(line,0,sizeof(line)); + printf("\n"); + } + + //close(pt); + return 0; +} \ No newline at end of file diff --git a/Raspberry/data.cpp b/Raspberry/data.cpp new file mode 100644 index 0000000..bd4d97f --- /dev/null +++ b/Raspberry/data.cpp @@ -0,0 +1,8 @@ +#include +using namespace std; + +int main() +{ + cout << "Hello, World!"; + return 0; +} \ No newline at end of file diff --git a/Raspberry/listener b/Raspberry/listener new file mode 100644 index 0000000..deb7b19 Binary files /dev/null and b/Raspberry/listener differ diff --git a/Raspberry/listener.c b/Raspberry/listener.c new file mode 100644 index 0000000..2358f4c --- /dev/null +++ b/Raspberry/listener.c @@ -0,0 +1,196 @@ +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include + +#define MAX_SENSORS 10 + +int sig_received = 0; + +struct sigaction action; +struct termios tty, old; + +typedef struct{ + char sensor[30]; + char port[30]; + +} SensorAndPort; + +void sig_handler(int sig) +{ + if (sig == SIGINT){ + printf("\nSignal SIGINT received\n"); + sig_received = 1; + } + else if (sig == SIGKILL){ + printf("\nSignal SIGKILL received\n"); + sig_received = 1; + } +} + +void create_error_file(char *str, char *msg){ + FILE * file; + char base[30] ="res/result_exp_"; + strcat(base,str); + strcat(base,".txt"); + file = fopen(base,"w"); + if(file==NULL){ + perror("create_error_file(...) - fopen"); + exit(-1); + } + fprintf(file, "%s",msg); + fclose(file); +} + +void save_data(int pt, char* sensor){ + FILE * file; + char base[26] ="res/result_exp_"; + strcat(base, sensor); + strcat(base, ".txt"); + file = fopen(base,"w"); + printf("base = %s\n",base); + if(file==NULL){ + perror("fopen"); + exit(-1); + } + + int count = 0; + char line[2]; + memset(line,0,sizeof(line)); + while(1){ + int r = read(pt ,line,1); + printf("c = %c (characters read : %d)\n\n", line[0], r); + r = fprintf(file, "%c",line[0]); + fflush(file); + memset(line,0,sizeof(line)); + count++; + if(sig_received == 1){ + break; + } + } + fclose(file); + printf("end save_data\n"); +} + +void reset_serial(int pt){ + tcsetattr(pt, TCSANOW, &old); + close(pt); + printf("end reset_serial\n"); +} + +void * set_serial(void * sensorAndPort2){ + char *device = ((SensorAndPort *)sensorAndPort2)->port; + printf("device : %s\n",device); + //char * device = (char *)deviceV; + int pt = open(device, O_RDWR | O_NOCTTY | O_SYNC); // retourne une erreur si le terminal n'est connecté à rien (pas de STM32 ni de Arduino dans notre cas) + if(pt == -1){ + create_error_file(((SensorAndPort *)sensorAndPort2)->sensor, "impossible de se connecter au port série, veuillez vérifier le microprocesseur et la raspberry"); + perror("in set_serial(...) - open"); + pthread_exit(NULL); + } + ioctl(pt, I_SRDOPT, RMSGD); + //fcntl(pt, F_SETFL, 0); + tcgetattr(pt, &old); + tcgetattr(pt, &tty); // Get the current attributes of the Serial port + // cfmakeraw(&tty); + tty.c_iflag &= ~(BRKINT | ICRNL | INPCK | ISTRIP | IXON | IXOFF); + tty.c_oflag &= ~(OPOST); + tty.c_cflag |= (CS8); + tty.c_cflag &= ~(CSIZE|PARENB); + tty.c_lflag &= ~(ECHO|ECHONL|ICANON|ISIG|IEXTEN); + tty.c_cc[VMIN] = 1; + tty.c_cc[VTIME] = 0; + cfsetispeed(&tty,B9600); + cfsetospeed(&tty,B9600); + + sleep(1); + if(tcflush(pt, TCIFLUSH)==-1){ + perror("in set_serial(...) - tcflush"); + pthread_exit(NULL); + } + if(tcsetattr(pt, TCSANOW, &tty)==-1){ + perror("in set_serial(...) - tcsetattr"); + pthread_exit(NULL); + } + if(cfgetispeed(&tty)!=B9600) + { + perror("in set_serial(...) - cfgetispeed"); + pthread_exit(NULL); + } + + save_data(pt, ((SensorAndPort *)sensorAndPort2)->sensor); + reset_serial(pt); + pthread_exit(NULL); +} + +int read_file_by_line(char list_lines[MAX_SENSORS][30], char * file_name){ + int count_line=0, i=0; + char str[30]; + FILE * conf = fopen(file_name,"r"); + while(fgets(str, 30, conf) != NULL){ + str[strcspn(str,"\r\n")] = 0; // strcspn parcours la chaine str jusqu'à trouvé l'un des charactères indiqués et renvoie l'indice du charactère. + //printf("line : %s\n",str); + strcpy(list_lines[i],str); + i++; + count_line++; + } + return count_line; +} + +void get_ports_to_read(char list_sensors[MAX_SENSORS][30], char list_serial_port[MAX_SENSORS][30], int count_sensors, int count_serial_port, char sensors_ports[MAX_SENSORS][30]){ + int i=0, j=0, is_equal=-1; + char * port,* sensor; + char *s="="; + for(i=0; i