Configuración y lectura de puerto serial en sistemas operativos UNIX Like

Esta entrada es una de aquellas que se encontraba dormida hace años entre los borradores del blog.

Trata acerca de la configuración de puertos seriales de sistemas operativos Unix like y el comando que permita la lectura de la información que se recibe a través del mismo.

Si bien tengo entendido que la información corresponde a la configuración utilizada en un Unix, las directivas de los comandos podrían variar mínimamente entre sistemas operativos Unix y distribuciones Linux. Por otro lado, tampoco tengo forma de probarla ya que no dispongo de un entorno de pruebas adecuado para validar la información, de todas formas lo hago público por si le llega a ser de utilidad a alguien, mejor así que perdido entre los borradores.

Para leer información de un puerto serial lo que deberíamos hacer son dos cosas:
  1. Primero deberíamos configurar la velocidad del puerto, los bits de paridad, etc., y esto lo logramos mediante el comando stty de la siguiente manera:
    # stty 9600 cs7 istrip parenb -parodd min 1 time 0 ignpar -echo ignbrk -icanon < /dev/tty1a
    Es importante aclarar que el puerto serial al cual se hace referencia en el comando anterior (/dev/tty1a) corresponde a un sistema operativo basado Unix, en las distribuciones Linux los puertos seriales se encuentran en el mismo directorio /dev pero enumerados de forma diferente como por ejemplo ttyS0, ttyS1, ttyS2, etc.

    Para conocer más acerca del comando stty y de todos sus argumentos pueden consultar el man page aquí.
    # man stty

    stty(C)
    ____________________________________________________________

    stty -- set the options for a terminal

    Synopsis

    stty [-a] [-g] [options]

    Description

    stty sets certain terminal I/O options for the device that is the
    current standard input; without arguments, it reports the
    settings of certain options.

    In the input and output of stty, if a character is preceded by a
    caret (^), then the value of that option is the corresponding
    control character (for example, ``^h'' is H; in this case,
    recall that H is the same as the ``backspace'' key.) The
    sequence ``^''' means that an option has a null value.

    The -- argument is used to indicate the end of options. Any
    arguments following the -- are treated as operands, even if they
    begin with a -. The -- should not be used as an option nor as an
    operand.
    -a
    Report all option settings.
    -g
    Report current settings in a form that can be used as an
    argument to another stty command.

    For detailed information about the modes listed in sections
    ``Control modes'' through ``Local modes''. See termio(M). For
    detailed information about the modes listed in sections
    ``Hardware flow control modes'' and ``Clock modes''. See
    termiox(M). Options described in the ``Combination Modes''
    section are implemented using options in the earlier sections.
    Note that many combinations of options make no sense, but no
    sanity checking is performed. Hardware flow control and clock
    modes options may not be supported by all hardware interfaces.
  2. El siguiente paso consiste en leer la información recibida a través del puerto serial, para ello se puede utilizar el comando line:
    # line < /dev/tty1a > /tmp/captura_puerto_serial.txt
    Para conocer más acerca del comando line pueden consultar su man page:
    # man line

    line(C)
    ____________________________________________________________

    line -- read one line

    Syntax

    line

    Description

    line copies one line (up to a new line) from the standard input
    and writes it on the standard output.

    line is often used within shell scripts to read from the user's
    terminal.

    line always prints at least a new line.

    Exit values

    line returns 0 on successful completion; 1 on end-of-file
    detected on input.

Fuentes relacionadas:

Comentarios