rs 232 pin configuration in linux pc

后端 未结 2 830
日久生厌
日久生厌 2021-01-27 05:54

There are a lot of examples which shows on how to communicate through the serial port of the pc. But is there a way to configure the pins of rs 232? I just need to set the tx pi

相关标签:
2条回答
  • 2021-01-27 06:26

    If you are not restricted to RS232 only. You have other options

    First, if you PC still have parallel port, it would be a better choice over RS232.

    Or, you can use some some USB-GPIO modules. Some suggestion:

    • FTDI bitbang mode
    • 8 CHANNEL USB GPIO MODULE
    0 讨论(0)
  • 2021-01-27 06:29

    Control Pins

    For the other pins DTR CTS etc, you will need to use ioctl() to toggle the pin.

    Here is a simple example (no error checking) to do that for the DTR line:

    #include <termios.h>
    #include <unistd.h>
    #include <sys/ioctl.h>
    
    int f = open( "/dev/ttyS0", O_RDWR | O_NOCTTY);
    int pins;    
    ioctl( f, TIOCMGET, &pins);
    pins = pins | TIOCM_DTR;
    ioctl( f, TIOCMSET, &pins) // the order you do this depends 
    sleep(1);
    ioctl( f, TIOCMGET, &pins);
    pins = pins & ~TIOCM_DTR;
    ioctl( f, TIOCMSET, &pins)
    

    The various flags are described in the man page for open and tty_ioctl

    Transmit Pin

    Using the TX pin is probably a bit tricker; in theory the output is normally 1, but then you can set a 'break' for a period of time which set it to 0. You could probably use the following, but I havent tried it:

    ioctl( f, TIOCSBRK)
    

    Caution

    Note that in traditional rs232 the levels are notionally +/- 12v ( between +/-3,15V) where negative is 1 and positive is zero, which might be the opposite of what you are expecting. But these days a lot of serials port use TTL or 3v3 levels instead.

    I used the above in an application where we used DTR as an output GPIO; remember to use appropriate resistors or other buffering as needed, so you don't blow up your PC serial port.

    YMMV with USB serial dongles.

    0 讨论(0)
提交回复
热议问题