[Previo por Fecha] [Siguiente por Fecha] [Previo por Hilo] [Siguiente por Hilo]

[Hilos de Discusión] [Fecha] [Tema] [Autor]

Re: [Sop.Tec.LinuxPPP] Del Puerto Serial a Base de Datos.



Ya encontre el programa en C para leer el puerto Serial.
Se los envio y espero les sea de utilidad.

Seguimos en contacto.


El mar, 28-01-2003 a las 12:56, Sergio Vergara Ganado escribió:
> Bueno, quizas el los quiere meter en una DB para la facturacion y
> tenerlos en linea los CDR's, yo que se, pero siempre en estos casos es
> mejor dejarlos en una DB que en un archivo plano.
> 
> 
> 
> El mar, 28-01-2003 a las 12:01, Sergio de la Cruz Flores escribió:
> > con perl tienes todo para hacer esto, recuerda que una de las cosa para que fue
> > creado perl fue para manipular datos y formatear texto.
> > 
> > Es cosa sencilla, puedes empezar a leer los datos y asi irlos metiendo a un
> > repositorio de datos, no necesariamente un relacional, lo puedes pasar en crudo
> > osea directo, y ya despues formatearlos para generar reportes si no lo quieres
> > hacer de inmediato, o la otra es que en cuanto lea, arregle , formatee y
> > despues almacene.
> > 
> > saludos.
> > cHeKo=)
> > 
> >  --- Sergio Vergara Ganado <svergara en telereunion com mx> escribió: > Primero
> > checa con minicom la comunicacion entre el puerto serial de PBX
> > > con el puerto serial de la PC (o lo que tengas). Cuando veas que envia
> > > informacion puedes hacer un programa en C para que este cachando todo lo
> > > que pasa por el puerto serial del PC. Otra forma de cachar lo que se
> > > encuentra en el puerto serial del PC es ejecutar algo como esto:
> > > cat /dev/ttyS1 > /home/info/pbx.txt
> > > Este metodo no es de lo mas recomendable, yo diria que es un Bill Gates
> > > (al ahi se va) pero te funciona de entrada en lo que tienes el programa
> > > en C o en perl.
> > > Ya teniendo toda la informacion en el archivo la analizas para checar
> > > sus campos y de ahi unicamente formateas y suber la informacion a la DB.
> > > Realmente no necesitas PHP para manejar la informacion.
> > > 
> > > Espero esto te ayude. Recuerdo que tenia por ahi el programa en C para
> > > estar escuchando el puerto, pero no recuerdo donde lo deje. Si lo
> > > encuentro te lo envio a tu cuenta personal.
> > > 
> > > Estamos en contacto.
> > > 
> > > 
> > > El lun, 27-01-2003 a las 18:15, Edgar escribió:
> > > > 
> > > > Que tal a todos!!
> > > > 
> > > > Tengo la siguiente situacion:
> > > > 
> > > > Una equipo PBX envia informacion valiosa por el puerto serial a la
> > > computadora que tiene linux. Necesito tomar la informacion que llega al
> > > puerto serial y guardarla en la base de datos. Segun debo poner un proceso
> > > que lea el puerto serial y guarde a un archivo texto/ascii y con php leerlo y
> > > guardarlo en mysql.  Pero no tengo idea de como hacer ese proceso ? Que me
> > > aconsejan hacer ? Tienen otra forma de hacerlo ? 
> > > > 
> > > > Gracias.
> > > > 
> > > > Saludos !!
> > > > 
> > > > 
> > > > 
> > > > _______________________________________________
> > > > <font size=2 face=geneva><b>Join Excite! - <a href=http://www.excite.com
> > > target=_blank>http://www.excite.com</a></b>
> > > > The most personalized portal on the Web!</font>
> > > > -- 
> > > > Lista de soporte de LinuxPPP
> > > > Dirección email: Linux en linuxppp net
> > > > Dirección web: http://mail.linuxppp.net/mailman/listinfo/linux
> > > > Reglas de la lista: http://linuxppp.net/reglas.html
> > > -- 
> > > Sergio Vergara Ganado
> > > Coordinador Network Support System
> > > Telereunion Mexico, Moras #430 Col del Valle
> > > Tel: 91718737
> > > ICQ: 75874968
> > > --------------------------------------------
> > > "La felicidad no se produce por grandes golpes de fortuna, que ocurren
> > > raras veces, sino por pequeñas ventajas que ocurren todos los días."
> > > -Benjamin Franklin
> > > 
> > > -- 
> > > Lista de soporte de LinuxPPP
> > > Dirección email: Linux en linuxppp net
> > > Dirección web: http://mail.linuxppp.net/mailman/listinfo/linux
> > > Reglas de la lista: http://linuxppp.net/reglas.html 
> > 
> > _________________________________________________________
> > Do You Yahoo!?
> > Información de Estados Unidos y América Latina, en Yahoo! Noticias.
> > Visítanos en http://noticias.espanol.yahoo.com
> > -- 
> > Lista de soporte de LinuxPPP
> > Dirección email: Linux en linuxppp net
> > Dirección web: http://mail.linuxppp.net/mailman/listinfo/linux
> > Reglas de la lista: http://linuxppp.net/reglas.html
> -- 
> Sergio Vergara Ganado
> Coordinador Network Support System
> Telereunion Mexico, Moras #430 Col del Valle
> Tel: 91718737
> ICQ: 75874968
> --------------------------------------------
> "La felicidad no se produce por grandes golpes de fortuna, que ocurren
> raras veces, sino por pequeñas ventajas que ocurren todos los días."
> -Benjamin Franklin
-- 
Sergio Vergara Ganado
Coordinador Network Support System
Telereunion Mexico, Moras #430 Col del Valle
Tel: 91718737
ICQ: 75874968
--------------------------------------------
"La felicidad no se produce por grandes golpes de fortuna, que ocurren
raras veces, sino por pequeñas ventajas que ocurren todos los días."
-Benjamin Franklin
#include <stdio.h>   /* Standard input/output definitions */
#include <string.h>  /* String function definitions */
#include <unistd.h>  /* UNIX standard function definitions */
#include <fcntl.h>   /* File control definitions */
#include <errno.h>   /* Error number definitions */
#include <termios.h> /* POSIX terminal control definitions */


#define BAUDRATE    B9600
#define MODEMDEVICE "/dev/ttyS"
#define MAXLENNAME  1024   /* max number of bytes we can get at once */
#define MAXLENCAMPO 10     /* max number of bytes we can get at once */

int  open_port(char NoPto[]);
void NuevoNom (char acNomFileAct[], int imes, int idia, int ianio, char acExt[], char pathfile[]);
void MesDia   (int *imes, int *idia, int *ianio);
int  IntMes   (char acmes[]);
void CreaFile (FILE **pFile, char acNomFileAct[]);




int main(int argc, char *argv[])
   {
   int mainfd=0;                               /* File descriptor */
   char chout;
   struct termios options;

   FILE     *ArchOut;
   char     acNomFileAct[MAXLENNAME];
   int      idia, imes, idiaant, imesant, ianio;

   if (argc !=4 )
      {
      fprintf(stderr,"\n\nusage: ReadCOM Extension Path Serial\n\n");
      fprintf(stderr,"Donde: Extension = extension que tendra el archivo Generado.\n");
      fprintf(stderr,"       Path      = Lugar donde se grabara el archivo de salida.\n");
      fprintf(stderr,"       Serial    = Puerto serial a leer.\n\n");
      fprintf(stderr,"Ejemplo: ReadCOM  .pbx /home/Reportes/ 1\n\n");
      fprintf(stderr,"Leera el Serial COM1, es decir, /dev/ttyS1 y \nGenerara archivos en el directorio: /home/Reportes/ , con el nombre PBX_ANNIMESDIA.pbx\n\n");
      fprintf(stderr,"Donde: ANNIO = 4 digitos para el Anio\n");
      fprintf(stderr,"       MES   = 2 digitos para el Mes\n");
      fprintf(stderr,"       ANNIO = 2 digitos para el Dia\n\n");

      exit(1);
      }

   
   mainfd = open_port(argv[3]);

   fcntl(mainfd, F_SETFL, FNDELAY);            /* Configure port reading */

   tcgetattr(mainfd, &options);                /* Get the current options for the port */
   cfsetispeed(&options, BAUDRATE);            /* Set the input speed baud rates to 9600 */
   cfsetospeed(&options, BAUDRATE);            /* Set the out   speed baud rates to 9600 */
    
   options.c_cflag |= (CLOCAL | CREAD);        /* Enable the receiver and set local mode */

  /* Puerto configurado como 7E1
  /* Size Word = 7, paridad = par, 1 bit de paro
  /*
   options.c_cflag |= PARENB;
   options.c_cflag &= ~PARODD;
   options.c_cflag &= ~CSTOPB;
   options.c_cflag &= ~CSIZE;
   options.c_cflag |= CS7;

   options.c_cflag &= ~CRTSCTS;                /* Disable hardware flow control */  
   options.c_lflag &= ~(ICANON | ECHO | ISIG); /* Enable data to be processed as raw input */

   tcsetattr(mainfd, TCSANOW, &options);       /* Set the new options for the port */


  /*
   * Obtencion del Mes y Dia actual
   */
   MesDia(&imes, &idia, &ianio);

  /*
   * Generacion del nombre del archivo actual
   */
   NuevoNom(acNomFileAct, imes, idia, ianio, argv[1], argv[2]);

  /*
   * Creacion del archivo actual
   */
   CreaFile(&ArchOut, acNomFileAct);

   idiaant = idia;
   imesant = imes;

                         
   while (1)
      {
      read(mainfd, &chout, sizeof(chout));      /* Read character from ABU */
   
      if (chout != 0)
         {
         /*printf("Got %c.\n", chout);*/
         /*printf("%c", chout);*/

         fprintf(ArchOut, "%c", chout);
         fflush(ArchOut);
         }

      if( (imes!=imesant) || (idia!=idiaant) )
          {
          MesDia(&imes, &idia, &ianio);
          NuevoNom(acNomFileAct, imes, idia, ianio, argv[1], argv[2]);
          fclose(ArchOut);

          CreaFile(&ArchOut, acNomFileAct);

          idiaant = idia;
          imesant = imes;
          }


      MesDia(&imes, &idia, &ianio);

      chout=0;
      usleep(20000);
      }
                                                    /* Close the serial port */
   close(mainfd);
   }





 /*
  * 'open_port()' - Open serial port 1.
  *
  * Returns the file descriptor on success or -1 on error.
  */

int open_port(char NoPto[])
   {
   int  fd;                                           /* File descriptor for the port */
   char acSerial[15];


   acSerial[0]='\0';
   strcpy(acSerial,MODEMDEVICE);
   strcat(acSerial,NoPto);
   acSerial[10]='\0';
 
   printf("\nPuerto a Monitor: %s\n", acSerial);

   fd = open(acSerial, O_RDWR | O_NOCTTY | O_NDELAY);

   if (fd == -1)
      {                                              /* Could not open the port */
      fprintf(stderr, "open_port: Unable to open /dev/ttyS1 - %s\n", strerror(errno));
      exit(1);
      }

   return (fd);
 }


void MesDia (int *imes, int *idia, int  *ianio)
   {
   time_t t;
   char   buffer[1020];
   char   acmes[10];
   char   acdia[10];
   char   acanio[10];
   int auxmes = 0;

   memset(buffer, 0x00,1020);
   memset(acmes,  0x00, 10);
   memset(acdia,  0x00, 10);
   memset(acanio,  0x00, 10);

   time(&t);
   sprintf(buffer,"%s", ctime(&t));
   memcpy(acmes, &buffer[4], 3);
   memcpy(acdia, &buffer[8], 2);
   memcpy(acanio, &buffer[20], 4);

   acmes[3] = '\0';
   acdia[2] = '\0';
   acanio[4] = '\0';

   *idia = atoi(acdia);
   *imes = IntMes(acmes);
   *ianio = atoi(acanio);
   }





int IntMes(char acmes[])
   {
   int   icont, icomp;
   char  acMesCal[12][4]={"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"  };

   for(icont=0; icont<12; ++icont)
      {
      icomp = strcmp(acmes, acMesCal[icont]);

      if(icomp == 0)
         break;
      }
   return (++icont);
   }




void NuevoNom(char acNomFileAct[], int imes, int idia, int ianio, char acExt[], char pathfile[])
   {
   char   acMesbuf[MAXLENCAMPO];
   char   acDiabuf[MAXLENCAMPO];
   char   acAniobuf[MAXLENCAMPO];

   memset(acNomFileAct, 0x00, MAXLENNAME);
   memset(acMesbuf,     0x00, MAXLENCAMPO);
   memset(acDiabuf,     0x00, MAXLENCAMPO);
   memset(acAniobuf,    0x00, MAXLENCAMPO);

   sprintf(acAniobuf, "%d", ianio);
   acAniobuf[4] = '\0';
   sprintf(acMesbuf, "%02d", imes);
   acMesbuf[3] = '\0';
   sprintf(acDiabuf, "%02d", idia);
   acDiabuf[2] = '\0';

   /*printf ("\n Anio=%s, Mes=%s, Dia=%s", acAniobuf,acMesbuf,acDiabuf); */

   strcat(acNomFileAct, pathfile);
   strcat(acNomFileAct, "PBX_");
   strcat(acNomFileAct, acAniobuf);
   strcat(acNomFileAct, acMesbuf);
   strcat(acNomFileAct, acDiabuf);
   strcat(acNomFileAct, acExt);
  /* printf("\n acNomFileAct = %s\n", acNomFileAct);*/

   }



void CreaFile(FILE **pFile, char acNomFileAct[])
   {
   if ((*pFile = fopen(acNomFileAct, "a+t")) == NULL)
      {
      fprintf(stderr, "Cannot open output file.\n");
      exit(1);
      }

   }




[Hilos de Discusión] [Fecha] [Tema] [Autor]