How to open serial port in linux without changing any pin?

后端 未结 4 1090
南旧
南旧 2021-02-19 00:39

Posix requires changing RTS pin on port opening. I want a way to avoid it.

4条回答
  •  既然无缘
    2021-02-19 01:10

    A change in the DTR pin can be (eventually) avoided using the command line

    stty -F /dev/ttyUSB0 -hupcl
    

    This has the effect of making DTR turn on; and subsequently when the port is opened and closed, DTR is not affected.

    Source: https://raspberrypi.stackexchange.com/questions/9695/disable-dtr-on-ttyusb0/27706#27706

    And there is code there to do the same thing from python via termios, this can be done before opening the port via pyserial:

    import termios
    
    path = '/dev/ttyACM0'
    
    # Disable reset after hangup
    with open(path) as f:
        attrs = termios.tcgetattr(f)
        attrs[2] = attrs[2] & ~termios.HUPCL
        termios.tcsetattr(f, termios.TCSAFLUSH, attrs)
    

    The OP was running this on a Raspberry Pi, but I just tried it on Linux Mint on x86_64, it worked. I don't know how RTS is affected.

    The reason I find this useful, is for communication with an Arduino Nano - which has a USB-> serial chip on board - and normally the Arduino gets reset every time you open the serial port from linux (rising edge of DTR causes reset). For some applications, this is not a problem, but it's clearly useful to avoid this for other applications, and it's not so easy to remove that tiny capacitor from the Arduino which connects DTR to reset.

    You will still get a single reset when the stty command is executed (after plugging in the USB cable). But at least you can then keep opening and closing the serial port after that without further resets.

提交回复
热议问题