Post

Create Virtual Serial Port in Linux

Create Virtual Serial Port with socat & Python in Linux.

Create Virtual Serial Port in Linux

Virtual serial ports are a logical representation of physical serial ports. These ports do not connect with the serial port. A virtual serial port allows us to emulate a physical serial port, thereby removing the requirements of cable and physical communication interface.

Additionally, a virtual port allows software packages to communicate with internal memory using the null modem emulator. The null modem emulator is a virtual driver for Linux that enables virtual serial port communication.

There are several ways to create a virtual serial port in Linux to test and debug serial communication protocols.

socat

Install

socat is usually available by default, but we can still install it manually with apt-get or yum.

1
2
3
4
#Debian-based
sudo apt-get install -y socat
# RHEL-based
sudo yum install -y socat

Create Virtual Serial Port

For instance, to create two pseudo terminals with the debug mode on:

1
$ socat -d -d pty,rawer,echo=0 pty,rawer,echo=0

The -d -d option prints errors, fatals, warnings and notices. The two pty,rawer,echo=0 create two pseudo terminals under /dev/pts.

Python

A python script can create virtual serial ports.

Here’s a simple example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import pty
import os
import select
 
def mkpty():
    master1, slave = pty.openpty()
    slaveName1 = os.ttyname(slave)
    master2, slave = pty.openpty()
    slaveName2 = os.ttyname(slave)
    print("\ndevice names: ", slaveName1, slaveName2)
    return master1, master2
 
if __name__ == "__main__":
    master1, master2 = mkpty()
    while True:
        rl, wl, el = select.select([master1, master2], [], [], 1)
        for master in rl:
            data = os.read(master, 128)
            print("read %d data." %len(data))
            if master == master1:
                os.write(master2, data)
            else:
                os.write(master1, data)
This post is licensed under CC BY 4.0 by the author.