Este programa se usa en recepcion de cadenas de caracteres, util para probar programas de conexion bluetooth en APPINVENTOR

Dependencies:   mbed

Committer:
tony63
Date:
Mon Apr 13 19:56:04 2015 +0000
Revision:
0:9894ad6153eb
Child:
1:bf58c37db6af
Programa para recibir cadenas de caracteres y activar salidas

Who changed what in which revision?

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