listener.c
5.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <termios.h>
#include <string.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stropts.h>
#include <pthread.h>
#include <signal.h>
#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<count_sensors; i++){
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
sensor = strtok(list_serial_port[j], s);
//printf("sensor = %s\n",sensor);
is_equal=strcmp(list_sensors[i],sensor);
j++;
}
if(j==count_serial_port){
printf("Il n'y a pas de capteurs avec ce nom : %s\n",list_sensors[i]);
create_error_file(list_sensors[i],"Il n'y a pas de capteurs avec ce nom");
}
j=0;
is_equal=-1;
port = strtok(NULL,s);
//printf("port = %s\n",port);
strcpy(sensors_ports[i], port);
}
}
int main(){
SensorAndPort sensorAndPort[MAX_SENSORS];
char device[MAX_SENSORS][12];
pthread_t threads[MAX_SENSORS];
char list_sensors[MAX_SENSORS][30], list_serial_port[MAX_SENSORS][30], sensors_ports[MAX_SENSORS][30];
int i=0, count_sensors=0, count_serial_port=0;
count_sensors = read_file_by_line(list_sensors, "config_experimentation.txt");
printf("numbers of sensors = %d\n",count_sensors);
count_serial_port = read_file_by_line(list_serial_port, "config_raspberry.txt");
get_ports_to_read(list_sensors,list_serial_port,count_sensors, count_serial_port, sensors_ports);
action.sa_handler=sig_handler;
sigaction(SIGINT, &action, NULL);
for(i=0;i<count_sensors;i++){
strcpy(sensorAndPort[i].sensor,list_sensors[i]);
strcpy(sensorAndPort[i].port,sensors_ports[i]);
pthread_create(&(threads[i]), NULL, set_serial, (void *)(&(sensorAndPort[i])));
}
for(i=0;i<count_sensors;i++){
pthread_join(threads[i],NULL);
}
return 0;
}