[Python] How can I use /dev/ptmx for creating a virtual serial port?

Discussão em 'Python' iniciado por Stack, Setembro 8, 2025.

  1. Stack

    Stack Membro Participativo

    I have a program, using PySerial, and I want to test it without using a real serial port device.

    In Windows, I use com0com, and in Linux, I know there is a method to create a virtual serial port pair without using additional programs.

    So I look up the manual, and found pts, /dev/ptmx, but I don't know how to create a pair by following the manual. What is some example?

    I tried (in Python):

    f = open("/dev/ptmx", "r")


    And it works; /dev/pts/4 is created.

    And I tried:

    f = open("/dev/4", "w")


    And the result is:

    Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
    IOError: [Errno 5] Input/output error: '/dev/pts/4'


    edit: I found a solution (workround), using socat.

    socat PTY,link=COM8 PTY,link=COM9


    Then COM8 and COM9 are created as a virtual serial port pair.

    Continue reading...

Compartilhe esta Página