Commit aeee6ab05fdd7665f8f4970ae4ca0da0d4e448b3
1 parent
c6289fa5
Ajout du fichier qui tournera sur la Raspberry
Showing
10 changed files
with
298 additions
and
0 deletions
Show diff stats
No preview for this file type
@@ -0,0 +1,85 @@ | @@ -0,0 +1,85 @@ | ||
1 | +#include <stdio.h> | ||
2 | +#include <stdlib.h> | ||
3 | +#include <unistd.h> | ||
4 | +#include <termios.h> | ||
5 | +#include <string.h> | ||
6 | +#include <sys/stat.h> | ||
7 | +#include <fcntl.h> | ||
8 | +#include <stropts.h> | ||
9 | + | ||
10 | +int pt; | ||
11 | +struct termios tty, old; /* Create the structure */ | ||
12 | + | ||
13 | +void reset_tty(){ | ||
14 | + tcsetattr(pt, TCSANOW, &old); | ||
15 | + close(pt); | ||
16 | +} | ||
17 | + | ||
18 | +int main(){ | ||
19 | + int r=-1; | ||
20 | + char * device = "/dev/ttyS3"; | ||
21 | + pt = open(device, O_RDWR | O_NOCTTY | O_SYNC); | ||
22 | + if(pt == -1){ | ||
23 | + perror("open"); | ||
24 | + exit(-1); | ||
25 | + } | ||
26 | + printf("open : %d\n",pt); | ||
27 | + ioctl(pt, I_SRDOPT, RMSGD); | ||
28 | + tcgetattr(pt, &old); | ||
29 | + atexit(reset_tty); | ||
30 | + | ||
31 | + tcgetattr(pt, &tty); // Get the current attributes of the Serial port | ||
32 | + | ||
33 | + //cfmakeraw(&tty); | ||
34 | + tty.c_iflag &= ~(BRKINT | ICRNL | INPCK | ISTRIP | IXON | IXOFF); | ||
35 | + printf("iflag = %x\n",tty.c_iflag); | ||
36 | + tty.c_oflag &= ~(OPOST); | ||
37 | + printf("oflag = %x\n",tty.c_oflag); | ||
38 | + tty.c_cflag |= (CS8); | ||
39 | + tty.c_cflag &= ~(CSIZE|PARENB); | ||
40 | + printf("cflag = %x\n",tty.c_cflag); | ||
41 | + tty.c_lflag &= ~(ECHO|ECHONL|ICANON|ISIG|IEXTEN); | ||
42 | + printf("lflag = %x\n",tty.c_lflag); | ||
43 | + | ||
44 | + tty.c_cc[VMIN] = 7; | ||
45 | + tty.c_cc[VTIME] = 0; | ||
46 | + cfsetispeed(&tty,B9600); // Setting the Baud rate | ||
47 | + cfsetospeed(&tty,B9600); | ||
48 | + | ||
49 | + /*Peut-être inutile si on est en liaison série et pas en USB : le TCSAFLUSH devrait suffir*/ | ||
50 | + sleep(1); | ||
51 | + | ||
52 | + if(tcflush(pt, TCIFLUSH)==-1){ | ||
53 | + perror("tcflush"); | ||
54 | + exit(-1); | ||
55 | + } | ||
56 | + | ||
57 | + if(tcsetattr(pt, TCSANOW, &tty)==-1){ | ||
58 | + perror("tcsetattr"); | ||
59 | + } | ||
60 | + | ||
61 | + int i = 0; | ||
62 | + char end[20]; | ||
63 | + | ||
64 | + if(cfgetispeed(&tty)!=B9600) | ||
65 | + { | ||
66 | + perror("cfgetispeed"); | ||
67 | + exit(-1); | ||
68 | + } | ||
69 | + | ||
70 | + int count = 0; | ||
71 | + char line[20]; | ||
72 | + memset(line,0,sizeof(line)); | ||
73 | + | ||
74 | + while(count<14){ | ||
75 | + r = read(pt ,line,1); | ||
76 | + printf("c = %c (characters read : %d)\n",line[0],r); | ||
77 | + //printf("l = %c\n",line[0]); | ||
78 | + count++; | ||
79 | + memset(line,0,sizeof(line)); | ||
80 | + printf("\n"); | ||
81 | + } | ||
82 | + | ||
83 | + //close(pt); | ||
84 | + return 0; | ||
85 | +} | ||
0 | \ No newline at end of file | 86 | \ No newline at end of file |
No preview for this file type
@@ -0,0 +1,196 @@ | @@ -0,0 +1,196 @@ | ||
1 | +#include <stdio.h> | ||
2 | +#include <stdlib.h> | ||
3 | +#include <unistd.h> | ||
4 | +#include <termios.h> | ||
5 | +#include <string.h> | ||
6 | +#include <sys/stat.h> | ||
7 | +#include <fcntl.h> | ||
8 | +#include <stropts.h> | ||
9 | + | ||
10 | +#include <pthread.h> | ||
11 | +#include <signal.h> | ||
12 | + | ||
13 | +#define MAX_SENSORS 10 | ||
14 | + | ||
15 | +int sig_received = 0; | ||
16 | + | ||
17 | +struct sigaction action; | ||
18 | +struct termios tty, old; | ||
19 | + | ||
20 | +typedef struct{ | ||
21 | + char sensor[30]; | ||
22 | + char port[30]; | ||
23 | + | ||
24 | +} SensorAndPort; | ||
25 | + | ||
26 | +void sig_handler(int sig) | ||
27 | +{ | ||
28 | + if (sig == SIGINT){ | ||
29 | + printf("\nSignal SIGINT received\n"); | ||
30 | + sig_received = 1; | ||
31 | + } | ||
32 | + else if (sig == SIGKILL){ | ||
33 | + printf("\nSignal SIGKILL received\n"); | ||
34 | + sig_received = 1; | ||
35 | + } | ||
36 | +} | ||
37 | + | ||
38 | +void create_error_file(char *str, char *msg){ | ||
39 | + FILE * file; | ||
40 | + char base[30] ="res/result_exp_"; | ||
41 | + strcat(base,str); | ||
42 | + strcat(base,".txt"); | ||
43 | + file = fopen(base,"w"); | ||
44 | + if(file==NULL){ | ||
45 | + perror("create_error_file(...) - fopen"); | ||
46 | + exit(-1); | ||
47 | + } | ||
48 | + fprintf(file, "%s",msg); | ||
49 | + fclose(file); | ||
50 | +} | ||
51 | + | ||
52 | +void save_data(int pt, char* sensor){ | ||
53 | + FILE * file; | ||
54 | + char base[26] ="res/result_exp_"; | ||
55 | + strcat(base, sensor); | ||
56 | + strcat(base, ".txt"); | ||
57 | + file = fopen(base,"w"); | ||
58 | + printf("base = %s\n",base); | ||
59 | + if(file==NULL){ | ||
60 | + perror("fopen"); | ||
61 | + exit(-1); | ||
62 | + } | ||
63 | + | ||
64 | + int count = 0; | ||
65 | + char line[2]; | ||
66 | + memset(line,0,sizeof(line)); | ||
67 | + while(1){ | ||
68 | + int r = read(pt ,line,1); | ||
69 | + printf("c = %c (characters read : %d)\n\n", line[0], r); | ||
70 | + r = fprintf(file, "%c",line[0]); | ||
71 | + fflush(file); | ||
72 | + memset(line,0,sizeof(line)); | ||
73 | + count++; | ||
74 | + if(sig_received == 1){ | ||
75 | + break; | ||
76 | + } | ||
77 | + } | ||
78 | + fclose(file); | ||
79 | + printf("end save_data\n"); | ||
80 | +} | ||
81 | + | ||
82 | +void reset_serial(int pt){ | ||
83 | + tcsetattr(pt, TCSANOW, &old); | ||
84 | + close(pt); | ||
85 | + printf("end reset_serial\n"); | ||
86 | +} | ||
87 | + | ||
88 | +void * set_serial(void * sensorAndPort2){ | ||
89 | + char *device = ((SensorAndPort *)sensorAndPort2)->port; | ||
90 | + printf("device : %s\n",device); | ||
91 | + //char * device = (char *)deviceV; | ||
92 | + 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) | ||
93 | + if(pt == -1){ | ||
94 | + create_error_file(((SensorAndPort *)sensorAndPort2)->sensor, "impossible de se connecter au port série, veuillez vérifier le microprocesseur et la raspberry"); | ||
95 | + perror("in set_serial(...) - open"); | ||
96 | + pthread_exit(NULL); | ||
97 | + } | ||
98 | + ioctl(pt, I_SRDOPT, RMSGD); | ||
99 | + //fcntl(pt, F_SETFL, 0); | ||
100 | + tcgetattr(pt, &old); | ||
101 | + tcgetattr(pt, &tty); // Get the current attributes of the Serial port | ||
102 | + // cfmakeraw(&tty); | ||
103 | + tty.c_iflag &= ~(BRKINT | ICRNL | INPCK | ISTRIP | IXON | IXOFF); | ||
104 | + tty.c_oflag &= ~(OPOST); | ||
105 | + tty.c_cflag |= (CS8); | ||
106 | + tty.c_cflag &= ~(CSIZE|PARENB); | ||
107 | + tty.c_lflag &= ~(ECHO|ECHONL|ICANON|ISIG|IEXTEN); | ||
108 | + tty.c_cc[VMIN] = 1; | ||
109 | + tty.c_cc[VTIME] = 0; | ||
110 | + cfsetispeed(&tty,B9600); | ||
111 | + cfsetospeed(&tty,B9600); | ||
112 | + | ||
113 | + sleep(1); | ||
114 | + if(tcflush(pt, TCIFLUSH)==-1){ | ||
115 | + perror("in set_serial(...) - tcflush"); | ||
116 | + pthread_exit(NULL); | ||
117 | + } | ||
118 | + if(tcsetattr(pt, TCSANOW, &tty)==-1){ | ||
119 | + perror("in set_serial(...) - tcsetattr"); | ||
120 | + pthread_exit(NULL); | ||
121 | + } | ||
122 | + if(cfgetispeed(&tty)!=B9600) | ||
123 | + { | ||
124 | + perror("in set_serial(...) - cfgetispeed"); | ||
125 | + pthread_exit(NULL); | ||
126 | + } | ||
127 | + | ||
128 | + save_data(pt, ((SensorAndPort *)sensorAndPort2)->sensor); | ||
129 | + reset_serial(pt); | ||
130 | + pthread_exit(NULL); | ||
131 | +} | ||
132 | + | ||
133 | +int read_file_by_line(char list_lines[MAX_SENSORS][30], char * file_name){ | ||
134 | + int count_line=0, i=0; | ||
135 | + char str[30]; | ||
136 | + FILE * conf = fopen(file_name,"r"); | ||
137 | + while(fgets(str, 30, conf) != NULL){ | ||
138 | + 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. | ||
139 | + //printf("line : %s\n",str); | ||
140 | + strcpy(list_lines[i],str); | ||
141 | + i++; | ||
142 | + count_line++; | ||
143 | + } | ||
144 | + return count_line; | ||
145 | +} | ||
146 | + | ||
147 | +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]){ | ||
148 | + int i=0, j=0, is_equal=-1; | ||
149 | + char * port,* sensor; | ||
150 | + char *s="="; | ||
151 | + for(i=0; i<count_sensors; i++){ | ||
152 | + while(is_equal!=0 && j<count_serial_port){ //on parcours la liste des ports series : tant que le nom du capteur de list_sensors[][] n'est pas trouvé on continue | ||
153 | + sensor = strtok(list_serial_port[j], s); | ||
154 | + //printf("sensor = %s\n",sensor); | ||
155 | + is_equal=strcmp(list_sensors[i],sensor); | ||
156 | + j++; | ||
157 | + } | ||
158 | + if(j==count_serial_port){ | ||
159 | + printf("Il n'y a pas de capteurs avec ce nom : %s\n",list_sensors[i]); | ||
160 | + create_error_file(list_sensors[i],"Il n'y a pas de capteurs avec ce nom"); | ||
161 | + } | ||
162 | + j=0; | ||
163 | + is_equal=-1; | ||
164 | + port = strtok(NULL,s); | ||
165 | + //printf("port = %s\n",port); | ||
166 | + strcpy(sensors_ports[i], port); | ||
167 | + } | ||
168 | +} | ||
169 | + | ||
170 | + | ||
171 | +int main(){ | ||
172 | + SensorAndPort sensorAndPort[MAX_SENSORS]; | ||
173 | + | ||
174 | + char device[MAX_SENSORS][12]; | ||
175 | + pthread_t threads[MAX_SENSORS]; | ||
176 | + | ||
177 | + char list_sensors[MAX_SENSORS][30], list_serial_port[MAX_SENSORS][30], sensors_ports[MAX_SENSORS][30]; | ||
178 | + int i=0, count_sensors=0, count_serial_port=0; | ||
179 | + count_sensors = read_file_by_line(list_sensors, "config_experimentation.txt"); | ||
180 | + printf("numbers of sensors = %d\n",count_sensors); | ||
181 | + count_serial_port = read_file_by_line(list_serial_port, "config_raspberry.txt"); | ||
182 | + get_ports_to_read(list_sensors,list_serial_port,count_sensors, count_serial_port, sensors_ports); | ||
183 | + | ||
184 | + action.sa_handler=sig_handler; | ||
185 | + sigaction(SIGINT, &action, NULL); | ||
186 | + | ||
187 | + for(i=0;i<count_sensors;i++){ | ||
188 | + strcpy(sensorAndPort[i].sensor,list_sensors[i]); | ||
189 | + strcpy(sensorAndPort[i].port,sensors_ports[i]); | ||
190 | + pthread_create(&(threads[i]), NULL, set_serial, (void *)(&(sensorAndPort[i]))); | ||
191 | + } | ||
192 | + for(i=0;i<count_sensors;i++){ | ||
193 | + pthread_join(threads[i],NULL); | ||
194 | + } | ||
195 | + return 0; | ||
196 | +} | ||
0 | \ No newline at end of file | 197 | \ No newline at end of file |