suppression affichage caracteres exotiques par insertion caractere fin de chaine

Dependencies:   MODSERIAL mbed

Fork of GPS_GSM by TeamLegrand

Committer:
LouisReynier
Date:
Mon May 28 17:37:57 2018 +0000
Revision:
1:96183a37548e
Parent:
0:2b9408ff3a1d
Child:
2:2299fb36bdd0
Correction bug caractere exotiques

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Quentin19 0:2b9408ff3a1d 1 #include "mbed.h"
Quentin19 0:2b9408ff3a1d 2 #include "MODSERIAL.h"
Quentin19 0:2b9408ff3a1d 3 #include "MACROS.h"
Quentin19 0:2b9408ff3a1d 4 #include <string>
Quentin19 0:2b9408ff3a1d 5 #include "stdio.h"
Quentin19 0:2b9408ff3a1d 6 #include "Serial.h"
Quentin19 0:2b9408ff3a1d 7 #include <cstdlib>
Quentin19 0:2b9408ff3a1d 8
Quentin19 0:2b9408ff3a1d 9
Quentin19 0:2b9408ff3a1d 10 char position[16]={"45.1234,1.12345"};
Quentin19 0:2b9408ff3a1d 11 char reponse_gsm[256];
Quentin19 0:2b9408ff3a1d 12 int element=0;
Quentin19 0:2b9408ff3a1d 13
Quentin19 0:2b9408ff3a1d 14 // variables gps
Quentin19 0:2b9408ff3a1d 15
Quentin19 0:2b9408ff3a1d 16 DigitalOut led5(p7);
Quentin19 0:2b9408ff3a1d 17
Quentin19 0:2b9408ff3a1d 18
Quentin19 0:2b9408ff3a1d 19 char debut_trame[7]="XXXXXX";
Quentin19 0:2b9408ff3a1d 20 char trame[81] ;
Quentin19 0:2b9408ff3a1d 21 char lat[8];
Quentin19 0:2b9408ff3a1d 22 char lat_NS;
Quentin19 0:2b9408ff3a1d 23 char lon[9];
Quentin19 0:2b9408ff3a1d 24 char lon_EW;
Quentin19 0:2b9408ff3a1d 25 float lat_cnv;
Quentin19 0:2b9408ff3a1d 26 float lon_cnv;
Quentin19 0:2b9408ff3a1d 27 int lat_deg = 0;
Quentin19 0:2b9408ff3a1d 28 float lat_dec;
Quentin19 0:2b9408ff3a1d 29 int lon_deg = 0;
Quentin19 0:2b9408ff3a1d 30 float lon_dec;
Quentin19 0:2b9408ff3a1d 31 char GPS [25];
Quentin19 0:2b9408ff3a1d 32 char rx ;
Quentin19 0:2b9408ff3a1d 33 int e ;
Quentin19 0:2b9408ff3a1d 34 int pos_ok = 0 ;
Quentin19 0:2b9408ff3a1d 35
Quentin19 0:2b9408ff3a1d 36 // liaisons series
Quentin19 0:2b9408ff3a1d 37 Serial pc(USBTX, USBRX, 115200);
Quentin19 0:2b9408ff3a1d 38 MODSERIAL gsm(p9,p10);
Quentin19 0:2b9408ff3a1d 39 Serial gps(p13, p14, 4800);
Quentin19 0:2b9408ff3a1d 40
Quentin19 0:2b9408ff3a1d 41
Quentin19 0:2b9408ff3a1d 42
Quentin19 0:2b9408ff3a1d 43 DigitalOut pwrkey(p21);
Quentin19 0:2b9408ff3a1d 44 DigitalOut emerg_off(p22);
Quentin19 0:2b9408ff3a1d 45 DigitalIn status(p11);
Quentin19 0:2b9408ff3a1d 46
Quentin19 0:2b9408ff3a1d 47 void reception();
Quentin19 0:2b9408ff3a1d 48 void clear_pc(void);
Quentin19 0:2b9408ff3a1d 49 void clear_buffer(void);
Quentin19 0:2b9408ff3a1d 50 void power_on();
Quentin19 0:2b9408ff3a1d 51 void init_gsm();
Quentin19 0:2b9408ff3a1d 52 void data_gsm();
Quentin19 0:2b9408ff3a1d 53
Quentin19 0:2b9408ff3a1d 54 int main() {
Quentin19 0:2b9408ff3a1d 55
Quentin19 0:2b9408ff3a1d 56 for (int i=0;i<20;i++) reponse_gsm[i]=0; //initialisation tableau réponse
Quentin19 0:2b9408ff3a1d 57
Quentin19 0:2b9408ff3a1d 58 gsm.baud(115200);
Quentin19 0:2b9408ff3a1d 59 // pc.baud(115200);
Quentin19 0:2b9408ff3a1d 60
Quentin19 0:2b9408ff3a1d 61 clear_buffer();
Quentin19 0:2b9408ff3a1d 62 pc.printf("Le buffer serie est vide\r\n");
Quentin19 0:2b9408ff3a1d 63 wait(2);
Quentin19 0:2b9408ff3a1d 64 clear_pc();
Quentin19 0:2b9408ff3a1d 65 pc.printf("Le buffer pc est vide\r\n");
Quentin19 0:2b9408ff3a1d 66
Quentin19 0:2b9408ff3a1d 67 init_gsm();
Quentin19 0:2b9408ff3a1d 68 pc.printf("gsm initialised \r\n");
Quentin19 0:2b9408ff3a1d 69 clear_buffer();
Quentin19 0:2b9408ff3a1d 70 /*
Quentin19 0:2b9408ff3a1d 71 gsm.printf("AT+CMGD=1,4\r");
Quentin19 0:2b9408ff3a1d 72 wait(0.5);
Quentin19 0:2b9408ff3a1d 73 pc.printf("Memoire sms est vide\r\n");
Quentin19 0:2b9408ff3a1d 74 */
Quentin19 0:2b9408ff3a1d 75 while(1)
Quentin19 0:2b9408ff3a1d 76 {
Quentin19 0:2b9408ff3a1d 77 // debut gps
Quentin19 0:2b9408ff3a1d 78 led5=0;
Quentin19 0:2b9408ff3a1d 79 pc.printf("debut GPS ");
Quentin19 0:2b9408ff3a1d 80 e = 0 ;
Quentin19 0:2b9408ff3a1d 81 pos_ok= 0 ;
Quentin19 0:2b9408ff3a1d 82
Quentin19 0:2b9408ff3a1d 83 while(pos_ok==0 ) {
Quentin19 0:2b9408ff3a1d 84 if (gps.readable())
Quentin19 0:2b9408ff3a1d 85 {
Quentin19 0:2b9408ff3a1d 86 rx = gps.getc();
Quentin19 0:2b9408ff3a1d 87 // pc.printf("%c",rx);
Quentin19 0:2b9408ff3a1d 88 for(int i = 0; i <80; i++)
Quentin19 0:2b9408ff3a1d 89 {
Quentin19 0:2b9408ff3a1d 90 trame[i] = trame[i+1];
Quentin19 0:2b9408ff3a1d 91
Quentin19 0:2b9408ff3a1d 92 }
Quentin19 0:2b9408ff3a1d 93 trame[76]=rx ;
Quentin19 0:2b9408ff3a1d 94 }
Quentin19 0:2b9408ff3a1d 95 strncpy(debut_trame,trame,6);
Quentin19 0:2b9408ff3a1d 96
Quentin19 0:2b9408ff3a1d 97 if (strcmp(debut_trame,"$GPGGA") == 0)
Quentin19 0:2b9408ff3a1d 98 {
Quentin19 0:2b9408ff3a1d 99
Quentin19 0:2b9408ff3a1d 100
Quentin19 0:2b9408ff3a1d 101 for (int i = 0; i<=8; i++)
Quentin19 0:2b9408ff3a1d 102 {
Quentin19 0:2b9408ff3a1d 103 lat[i] = trame [18+i];
Quentin19 0:2b9408ff3a1d 104 lat_cnv = atof(lat);
Quentin19 0:2b9408ff3a1d 105 }
Quentin19 0:2b9408ff3a1d 106 lat_NS = trame [28];
Quentin19 0:2b9408ff3a1d 107
Quentin19 0:2b9408ff3a1d 108 for (int a=0; a<=9; a++)
Quentin19 0:2b9408ff3a1d 109 {
Quentin19 0:2b9408ff3a1d 110 lon[a] = trame [30+a];
Quentin19 0:2b9408ff3a1d 111 lon_cnv = atof(lon);
Quentin19 0:2b9408ff3a1d 112 }
Quentin19 0:2b9408ff3a1d 113 lon_EW = trame [41];
Quentin19 0:2b9408ff3a1d 114
Quentin19 0:2b9408ff3a1d 115 lat_cnv = lat_cnv/100;
Quentin19 0:2b9408ff3a1d 116 lat_deg = (int)lat_cnv;
Quentin19 0:2b9408ff3a1d 117 lat_dec = lat_cnv - lat_deg;
Quentin19 0:2b9408ff3a1d 118 lat_dec = (lat_dec/60)*100;
Quentin19 0:2b9408ff3a1d 119 lat_cnv = lat_deg + lat_dec;
Quentin19 0:2b9408ff3a1d 120
Quentin19 0:2b9408ff3a1d 121 lon_cnv = lon_cnv/100;
Quentin19 0:2b9408ff3a1d 122 lon_deg = (int)lon_cnv;
Quentin19 0:2b9408ff3a1d 123 lon_dec = lon_cnv - lon_deg;
Quentin19 0:2b9408ff3a1d 124 lon_dec = (lon_dec/60)*100;
Quentin19 0:2b9408ff3a1d 125 lon_cnv = lon_deg + lon_dec;
Quentin19 0:2b9408ff3a1d 126
Quentin19 0:2b9408ff3a1d 127 if (lat_NS=='N')
Quentin19 0:2b9408ff3a1d 128 {
Quentin19 0:2b9408ff3a1d 129 lat_cnv = lat_cnv*1;
Quentin19 0:2b9408ff3a1d 130 }
Quentin19 0:2b9408ff3a1d 131
Quentin19 0:2b9408ff3a1d 132 else if (lat_NS=='S')
Quentin19 0:2b9408ff3a1d 133 {
Quentin19 0:2b9408ff3a1d 134 lat_cnv = lat_cnv*(-1);
Quentin19 0:2b9408ff3a1d 135 }
Quentin19 0:2b9408ff3a1d 136
Quentin19 0:2b9408ff3a1d 137 if (lon_EW=='E')
Quentin19 0:2b9408ff3a1d 138 {
Quentin19 0:2b9408ff3a1d 139 lat_cnv = lat_cnv*1;
Quentin19 0:2b9408ff3a1d 140 }
Quentin19 0:2b9408ff3a1d 141
Quentin19 0:2b9408ff3a1d 142 else if (lon_EW=='W')
Quentin19 0:2b9408ff3a1d 143 {
Quentin19 0:2b9408ff3a1d 144 lat_cnv = lat_cnv*(-1);
Quentin19 0:2b9408ff3a1d 145 }
Quentin19 0:2b9408ff3a1d 146 sprintf(GPS, "POS: %f,%f", lat_cnv, lon_cnv);
Quentin19 0:2b9408ff3a1d 147
Quentin19 0:2b9408ff3a1d 148 pos_ok = 1 ;
Quentin19 0:2b9408ff3a1d 149 }
Quentin19 0:2b9408ff3a1d 150
Quentin19 0:2b9408ff3a1d 151 } // fin while pos_ok
Quentin19 0:2b9408ff3a1d 152
Quentin19 0:2b9408ff3a1d 153 pc.printf("%s \n\r", GPS);
Quentin19 0:2b9408ff3a1d 154 reception();
Quentin19 0:2b9408ff3a1d 155 wait(5);
Quentin19 0:2b9408ff3a1d 156 }
Quentin19 0:2b9408ff3a1d 157
Quentin19 0:2b9408ff3a1d 158 }
Quentin19 0:2b9408ff3a1d 159
Quentin19 0:2b9408ff3a1d 160
Quentin19 0:2b9408ff3a1d 161 void init_gsm(){
Quentin19 0:2b9408ff3a1d 162 /*
Quentin19 0:2b9408ff3a1d 163 pwrkey=1;
Quentin19 0:2b9408ff3a1d 164 wait(5);
Quentin19 0:2b9408ff3a1d 165 pwrkey=0;
Quentin19 0:2b9408ff3a1d 166 */
Quentin19 0:2b9408ff3a1d 167 power_on();
Quentin19 0:2b9408ff3a1d 168 wait(3);
Quentin19 0:2b9408ff3a1d 169 pc.printf("AT\r\n");
Quentin19 0:2b9408ff3a1d 170 gsm.printf("AT\r");
Quentin19 0:2b9408ff3a1d 171 // wait(0.5);
Quentin19 0:2b9408ff3a1d 172 pc.printf("AT\r\n");
Quentin19 0:2b9408ff3a1d 173 data_gsm();
Quentin19 0:2b9408ff3a1d 174
Quentin19 0:2b9408ff3a1d 175 gsm.printf("AT+CPIN=1234\r"); // je rentre le code pin
Quentin19 0:2b9408ff3a1d 176 wait(5);
Quentin19 0:2b9408ff3a1d 177 pc.printf("AT+CPIN=1234\r\n");
Quentin19 0:2b9408ff3a1d 178 data_gsm();
Quentin19 0:2b9408ff3a1d 179
Quentin19 0:2b9408ff3a1d 180 gsm.printf("ATE0\r"); // pas d'echo vers le DTE pour les l'envoi des commandes
Quentin19 0:2b9408ff3a1d 181 wait(5);
Quentin19 0:2b9408ff3a1d 182
Quentin19 0:2b9408ff3a1d 183
Quentin19 0:2b9408ff3a1d 184 gsm.printf("AT+CMGF=1\r"); // je passe en mode texte
Quentin19 0:2b9408ff3a1d 185 wait(2);
Quentin19 0:2b9408ff3a1d 186
Quentin19 0:2b9408ff3a1d 187
Quentin19 0:2b9408ff3a1d 188 gsm.printf("AT+CNMI=2,1,0,0,0\r");
Quentin19 0:2b9408ff3a1d 189 wait(2);
Quentin19 0:2b9408ff3a1d 190
Quentin19 0:2b9408ff3a1d 191
Quentin19 0:2b9408ff3a1d 192 }
Quentin19 0:2b9408ff3a1d 193
Quentin19 0:2b9408ff3a1d 194
Quentin19 0:2b9408ff3a1d 195 void data_gsm(){
Quentin19 0:2b9408ff3a1d 196 for(int i=0;i<255;i++) reponse_gsm[i]=0;
Quentin19 0:2b9408ff3a1d 197 element=0;
Quentin19 0:2b9408ff3a1d 198 while(gsm.readable()){
Quentin19 0:2b9408ff3a1d 199 reponse_gsm[element]=gsm.getc();
Quentin19 0:2b9408ff3a1d 200 element++;
Quentin19 0:2b9408ff3a1d 201 }
Quentin19 0:2b9408ff3a1d 202 for(int i=0; i<element; i++)pc.printf("%c",reponse_gsm[i]);
Quentin19 0:2b9408ff3a1d 203 pc.printf("\n\r");
Quentin19 0:2b9408ff3a1d 204
Quentin19 0:2b9408ff3a1d 205 }
Quentin19 0:2b9408ff3a1d 206
Quentin19 0:2b9408ff3a1d 207 void power_on(){
Quentin19 0:2b9408ff3a1d 208 pwrkey=1;
Quentin19 0:2b9408ff3a1d 209 wait(3);
Quentin19 0:2b9408ff3a1d 210 while(status==0);
Quentin19 0:2b9408ff3a1d 211 wait(1);
Quentin19 0:2b9408ff3a1d 212 pwrkey=0;
Quentin19 0:2b9408ff3a1d 213 pc.printf("Le module est connecter ! \r\n");
Quentin19 0:2b9408ff3a1d 214 }
Quentin19 0:2b9408ff3a1d 215
Quentin19 0:2b9408ff3a1d 216 void clear_buffer(){
Quentin19 0:2b9408ff3a1d 217 char char1;
Quentin19 0:2b9408ff3a1d 218 while(gsm.readable()){
Quentin19 0:2b9408ff3a1d 219 char1 = gsm.getc();
Quentin19 0:2b9408ff3a1d 220 }
Quentin19 0:2b9408ff3a1d 221 return;
Quentin19 0:2b9408ff3a1d 222 }
Quentin19 0:2b9408ff3a1d 223
Quentin19 0:2b9408ff3a1d 224 void clear_pc(){
Quentin19 0:2b9408ff3a1d 225 char char1;
Quentin19 0:2b9408ff3a1d 226 while(pc.readable()){
Quentin19 0:2b9408ff3a1d 227 char1 = pc.getc();
Quentin19 0:2b9408ff3a1d 228 pc.printf("Le buffer pc est vide\r\n");
Quentin19 0:2b9408ff3a1d 229 }
Quentin19 0:2b9408ff3a1d 230 return;
Quentin19 0:2b9408ff3a1d 231 }
Quentin19 0:2b9408ff3a1d 232
Quentin19 0:2b9408ff3a1d 233 void reception(){
Quentin19 0:2b9408ff3a1d 234 int nouveau_message=0 ;
Quentin19 0:2b9408ff3a1d 235 char sms[256];
Quentin19 0:2b9408ff3a1d 236 char sms_brut[256];
Quentin19 0:2b9408ff3a1d 237 char num_mem[3];
Quentin19 0:2b9408ff3a1d 238 int longueur_msg1=0;
Quentin19 0:2b9408ff3a1d 239 int longueur_msg2=0;
Quentin19 0:2b9408ff3a1d 240 pc.printf("Reception ! \n");
Quentin19 0:2b9408ff3a1d 241 // attente notification
Quentin19 0:2b9408ff3a1d 242 while(gsm.readable()){
Quentin19 0:2b9408ff3a1d 243 sms[longueur_msg1] = gsm.getc();
Quentin19 0:2b9408ff3a1d 244 pc.printf("%c ",sms[longueur_msg1]);
Quentin19 0:2b9408ff3a1d 245 longueur_msg1++;
Quentin19 0:2b9408ff3a1d 246 nouveau_message = 1 ;
Quentin19 0:2b9408ff3a1d 247 }
Quentin19 0:2b9408ff3a1d 248 // si notification
Quentin19 0:2b9408ff3a1d 249 if (nouveau_message == 1)
Quentin19 0:2b9408ff3a1d 250 {
Quentin19 0:2b9408ff3a1d 251 num_mem[0]=sms[14];
Quentin19 0:2b9408ff3a1d 252 num_mem[1]=sms[15];
Quentin19 0:2b9408ff3a1d 253 num_mem[2]='\0';
Quentin19 0:2b9408ff3a1d 254 pc.printf("Notification recue : %s, longueur_msg= %d", sms, longueur_msg1);
Quentin19 0:2b9408ff3a1d 255 pc.printf("Numero memoire : %s", num_mem );
Quentin19 0:2b9408ff3a1d 256 // lecture memoire sms
Quentin19 0:2b9408ff3a1d 257 pc.printf("AT+CMGR=%s",num_mem );
Quentin19 0:2b9408ff3a1d 258 gsm.printf("AT+CMGR=%s\r",num_mem );
Quentin19 0:2b9408ff3a1d 259 wait(1);
Quentin19 0:2b9408ff3a1d 260 longueur_msg2 = 0 ;
Quentin19 0:2b9408ff3a1d 261 while(gsm.readable()){
Quentin19 0:2b9408ff3a1d 262 sms[longueur_msg2] = gsm.getc();
Quentin19 0:2b9408ff3a1d 263 // pc.printf("%c ",sms[longueur_msg2]);
Quentin19 0:2b9408ff3a1d 264 longueur_msg2++;
Quentin19 0:2b9408ff3a1d 265 }
LouisReynier 1:96183a37548e 266 sms[longueur_msg2] = '\0'; // le dernier caractere = fin de chaine
Quentin19 0:2b9408ff3a1d 267 pc.printf("sms recu : %s, longueur_msg= %d \n", sms, longueur_msg2);
Quentin19 0:2b9408ff3a1d 268 // suppression en tete
Quentin19 0:2b9408ff3a1d 269 for (int i=0;i<100;i++)
Quentin19 0:2b9408ff3a1d 270 {
Quentin19 0:2b9408ff3a1d 271 sms_brut[i]= sms[66+i];
Quentin19 0:2b9408ff3a1d 272 }
Quentin19 0:2b9408ff3a1d 273 // affichage message brut
Quentin19 0:2b9408ff3a1d 274 pc.printf("sms brut : %s", sms_brut);
Quentin19 0:2b9408ff3a1d 275
Quentin19 0:2b9408ff3a1d 276 if (strncmp(sms_brut,"POS?",4)==0)
Quentin19 0:2b9408ff3a1d 277 { pc.printf("Demande de position !");
Quentin19 0:2b9408ff3a1d 278 gsm.printf("AT+CMGS=\"0618981390\"\r");
Quentin19 0:2b9408ff3a1d 279 gsm.printf("%s",GPS);
Quentin19 0:2b9408ff3a1d 280 gsm.putc(0x1A);
Quentin19 0:2b9408ff3a1d 281 nouveau_message = 0 ;
Quentin19 0:2b9408ff3a1d 282 }
Quentin19 0:2b9408ff3a1d 283
Quentin19 0:2b9408ff3a1d 284 }
Quentin19 0:2b9408ff3a1d 285
Quentin19 0:2b9408ff3a1d 286
Quentin19 0:2b9408ff3a1d 287
Quentin19 0:2b9408ff3a1d 288
Quentin19 0:2b9408ff3a1d 289
Quentin19 0:2b9408ff3a1d 290 }