ESTE PROGRAMA CONFORMA SUBCADENAS A PARTIR DE UNA CADENA GRANDE SEPARADA POR ESPACIOS. ÚTIL PARA RECIBIR DATOS Y ORDENES DESDE APP INVENTOR.

Dependencies:   mbed

Committer:
tony63
Date:
Fri Feb 22 07:56:13 2019 +0000
Revision:
0:2502bdbfc7ae
PARA CONFORMAR SUBCADENAS

Who changed what in which revision?

UserRevisionLine numberNew contents of line
tony63 0:2502bdbfc7ae 1
tony63 0:2502bdbfc7ae 2 //PROGRAMA PARA RECIBIR UNA CADENA Y SEPARARLA O ASIGNARLA A SUBCADENAS
tony63 0:2502bdbfc7ae 3 //ENVIAR LOS VALORES SEPARADOS POR ESPACIOS
tony63 0:2502bdbfc7ae 4 //UTIL EN LECTURA DE CADENAS PARA APP INVENTOR
tony63 0:2502bdbfc7ae 5 #include "mbed.h"
tony63 0:2502bdbfc7ae 6 #include "stdio.h"
tony63 0:2502bdbfc7ae 7 #include "string.h"
tony63 0:2502bdbfc7ae 8 #include <stdlib.h>
tony63 0:2502bdbfc7ae 9
tony63 0:2502bdbfc7ae 10 Serial GSM(PTE0,PTE1); //puertos del FRDM para el modem
tony63 0:2502bdbfc7ae 11 Serial pc(USBTX,USBRX); //puertos del PC
tony63 0:2502bdbfc7ae 12 char buffer[20];// TAMAÑO DEL BUFER
tony63 0:2502bdbfc7ae 13 Timer t; //VALOR DEL TIEMPO
tony63 0:2502bdbfc7ae 14 int count;
tony63 0:2502bdbfc7ae 15 int i = 0;
tony63 0:2502bdbfc7ae 16 int c=0;
tony63 0:2502bdbfc7ae 17 int C,P,I,D;
tony63 0:2502bdbfc7ae 18 char r[]="";
tony63 0:2502bdbfc7ae 19 DigitalOut LedRojo(LED1);
tony63 0:2502bdbfc7ae 20 DigitalOut LedVerde(LED2);
tony63 0:2502bdbfc7ae 21 DigitalOut LedAzul(LED3);
tony63 0:2502bdbfc7ae 22
tony63 0:2502bdbfc7ae 23 int readBuffer(char *buffer,int count) //esta funcion lee un bufer de datos
tony63 0:2502bdbfc7ae 24 {
tony63 0:2502bdbfc7ae 25 int i=0;
tony63 0:2502bdbfc7ae 26 t.start(); //CUENTA EL TIEMPO DE CONEXION E INICIA
tony63 0:2502bdbfc7ae 27 while(1) {
tony63 0:2502bdbfc7ae 28 while (GSM.readable()) {
tony63 0:2502bdbfc7ae 29 char c = GSM.getc();
tony63 0:2502bdbfc7ae 30 if (c == '\r' || c == '\n') c = '$';//si se envia fin de linea o de caracter inserta $
tony63 0:2502bdbfc7ae 31 buffer[i++] = c;//mete al bufer el caracter leido
tony63 0:2502bdbfc7ae 32 if(i > count)break;//sale del loop si ya detecto terminacion
tony63 0:2502bdbfc7ae 33 }
tony63 0:2502bdbfc7ae 34 if(i > count)break;
tony63 0:2502bdbfc7ae 35 if(t.read() > 1) { //MAS DE UN SEGUNDO DE ESPERA SE SALE Y REINICA EL RELOJ Y SE SALE
tony63 0:2502bdbfc7ae 36 t.stop();
tony63 0:2502bdbfc7ae 37 t.reset();
tony63 0:2502bdbfc7ae 38 break;
tony63 0:2502bdbfc7ae 39 }
tony63 0:2502bdbfc7ae 40 }
tony63 0:2502bdbfc7ae 41 return 0;
tony63 0:2502bdbfc7ae 42 }
tony63 0:2502bdbfc7ae 43
tony63 0:2502bdbfc7ae 44 void cleanBuffer(char *buffer, int count) //esta funcion limpia el bufer
tony63 0:2502bdbfc7ae 45 {
tony63 0:2502bdbfc7ae 46 for(int i=0; i < count; i++) {
tony63 0:2502bdbfc7ae 47 buffer[i] = '\0';
tony63 0:2502bdbfc7ae 48 }
tony63 0:2502bdbfc7ae 49 }
tony63 0:2502bdbfc7ae 50
tony63 0:2502bdbfc7ae 51
tony63 0:2502bdbfc7ae 52 int main(void)
tony63 0:2502bdbfc7ae 53 {
tony63 0:2502bdbfc7ae 54 LedVerde=1;
tony63 0:2502bdbfc7ae 55 LedRojo=1;
tony63 0:2502bdbfc7ae 56 LedAzul=1;
tony63 0:2502bdbfc7ae 57 LedVerde=0;
tony63 0:2502bdbfc7ae 58 wait(2); //PRENDE EL LED VERDE POR 2 SEGUNDOS
tony63 0:2502bdbfc7ae 59 LedVerde=1;
tony63 0:2502bdbfc7ae 60 GSM.baud(9600);
tony63 0:2502bdbfc7ae 61 GSM.format(8,Serial::None,1);
tony63 0:2502bdbfc7ae 62
tony63 0:2502bdbfc7ae 63 while(1){
tony63 0:2502bdbfc7ae 64 if (GSM.readable()) {
tony63 0:2502bdbfc7ae 65 readBuffer(buffer,10);
tony63 0:2502bdbfc7ae 66 pc.printf("buffer= %s\n\r ",buffer); //imprime el bufer
tony63 0:2502bdbfc7ae 67 sscanf( buffer, "%d, %d, %d, %d", &C, &P, &I, &D);
tony63 0:2502bdbfc7ae 68 pc.printf("%d %d, %d,%d\n", C,P,I,D);
tony63 0:2502bdbfc7ae 69
tony63 0:2502bdbfc7ae 70 }
tony63 0:2502bdbfc7ae 71 cleanBuffer(buffer,10);
tony63 0:2502bdbfc7ae 72 }
tony63 0:2502bdbfc7ae 73 }