• Domingo 12 de Mayo de 2024, 15:44

Autor Tema:  Lectura 1 Byte Puerto Serie  (Leído 1480 veces)

bratis

  • Nuevo Miembro
  • *
  • Mensajes: 6
    • Ver Perfil
Lectura 1 Byte Puerto Serie
« en: Viernes 6 de Febrero de 2004, 13:56 »
0
Hola, estoy desarrolando una aplicacion en la que necesito leer byte a byte lo que me llega por el puerto serie. La idea es la siguiente:

Bucle infinito hasta que recibo la signal SIGIO, esta signal dara paso a un manejador que leera el byte del puerto y lo tratara.Mi cuestion es, la interrupcion software SIGIO se produce cuando el dispositivo asociado al puerto serie recibe 1 solo byte?y con la funcion read se puede leer tambien un solo byte?
read(fd, buffer, 1);

Si alguien me pudiera mandar algun ejemplo parecido le estaria muy agradecido.

Muchas gracias

Ruben3d

  • Miembro HIPER activo
  • ****
  • Mensajes: 710
  • Nacionalidad: es
    • Ver Perfil
    • Web personal
Re: Lectura 1 Byte Puerto Serie
« Respuesta #1 en: Sábado 7 de Febrero de 2004, 00:51 »
0
Hola.

Existe un programa, llamado miniterm, que lo que hace es enviar por el puerto serie lo que escribas, carácter a carácter, y escribir por pantalla lo que reciba, también carácter a carácter. Su código es

Código: Text
  1.  
  2. /*
  3. * AUTHOR: Sven Goldt (goldt@math.tu-berlin.de)
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License
  7. * as published by the Free Software Foundation; either version 2
  8. * of the License, or (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. */
  16. /*
  17. This is like all programs in the Linux Programmer's Guide meant
  18. as a simple practical demonstration.
  19. It can be used as a base for a real terminal program.
  20. */
  21.  
  22. #include
  23. #include
  24. #include
  25. #include
  26. #include
  27.  
  28. #define BAUDRATE B38400
  29. #define MODEMDEVICE "/dev/ttyS1"
  30. #define ENDMINITERM 2 /* ctrl-b to quit miniterm */
  31.  
  32. #define _POSIX_SOURCE 1 /* POSIX compliant source */
  33.  
  34. #define FALSE 0
  35. #define TRUE 1
  36.  
  37. volatile int STOP=FALSE;
  38.  
  39. void child_handler(int s)
  40. {
  41. STOP=TRUE;
  42. }
  43.  
  44. main()
  45. {
  46. int fd,c;
  47. struct termios oldtio,newtio,oldstdtio,newstdtio;
  48. struct sigaction sa;
  49.  
  50. /*
  51. Open modem device for reading and writing and not as controlling tty
  52. because we don't want to get killed if linenoise sends CTRL-C.
  53. */
  54. fd = open(MODEMDEVICE, O_RDWR | O_NOCTTY);
  55. if (fd <0) {perror(MODEMDEVICE); exit(-1); }
  56.  
  57. tcgetattr(fd,&oldtio); /* save current modem settings */
  58.  
  59. /*
  60. Set bps rate and hardware flow control and 8n1 (8bit,no parity,1 stopbit).
  61. Also don't hangup automatically and ignore modem status.
  62. Finally enable receiving characters.
  63. */
  64. newtio.c_cflag = BAUDRATE | CRTSCTS | CS8 | CLOCAL | CREAD;
  65.  
  66. /*
  67. Ignore bytes with parity errors and make terminal raw and dumb.
  68. */
  69. newtio.c_iflag = IGNPAR;
  70.  
  71. /*
  72. Raw output.
  73. */
  74. newtio.c_oflag = 0;
  75.  
  76. /*
  77. Don't echo characters because if you connect to a host it or your
  78. modem will echo characters for you. Don't generate signals.
  79. */
  80. newtio.c_lflag = 0;
  81.  
  82. /* blocking read until 1 char arrives */
  83. newtio.c_cc[VMIN]=1;
  84. newtio.c_cc[VTIME]=0;
  85.  
  86. /* now clean the modem line and activate the settings for modem */
  87. tcflush(fd, TCIFLUSH);
  88. tcsetattr(fd,TCSANOW,&newtio);
  89.  
  90. /*
  91. Strange, but if you uncomment this command miniterm will not work
  92. even if you stop canonical mode for stdout. This is a linux bug.
  93. */
  94. tcsetattr(1,TCSANOW,&newtio); /* stdout settings like modem settings */
  95.  
  96. /* next stop echo and buffering for stdin */
  97. tcgetattr(0,&oldstdtio);
  98. tcgetattr(0,&newstdtio); /* get working stdtio */
  99. newstdtio.c_lflag &= ~(ICANON | ECHO);
  100. tcsetattr(0,TCSANOW,&newstdtio);
  101.  
  102. /* terminal settings done, now handle in/ouput */
  103. switch (fork())
  104. {
  105. case 0: /* child */
  106. /* user input */
  107. close(1); /* stdout not needed */
  108. for (c=getchar(); c!= ENDMINITERM&#59; c=getchar()) write(fd,&c,1);
  109. tcsetattr(fd,TCSANOW,&oldtio); /* restore old modem setings */
  110. tcsetattr(0,TCSANOW,&oldstdtio); /* restore old tty setings */
  111. close(fd);
  112. exit(0); /* will send a SIGCHLD to the parent */
  113. break;
  114. case -1:
  115. perror("fork");
  116. tcsetattr(fd,TCSANOW,&oldtio);
  117. close(fd);
  118. exit(-1);
  119. default: /* parent */
  120. close(0); /* stdin not needed */
  121. sa.sa_handler = child_handler;
  122. sa.sa_flags = 0;
  123. sigaction(SIGCHLD,&sa,NULL); /* handle dying child */
  124. while (STOP==FALSE) /* modem input handler */
  125. {
  126. read(fd,&c,1); /* modem */
  127. write(1,&c,1); /* stdout */
  128. }
  129. wait(NULL); /* wait for child to die or it will become a zombie */
  130. break;
  131. }
  132. }
  133.  
  134.  

Espero que te sirva.

Un saludo.

Ruben3d