PyVirtualSerialPorts
lastmod 2026-11-10created 2022-08-173 edits
PyVirtualSerialPorts is a Python package that creates virtual serial ports on Unix-like systems. It creates ports with pty instead of real devices, so it is used to test serial communication code without hardware. It passes data between the created ports, and with the loopback option enabled it also echoes the same data back to the sender. It can be used both as a command-line tool and as a Python module.
nix virtual serial ports
Usage
usage: virtualserialports [-h] [-l] [-d] num_ports
positional arguments:
num_ports number of ports to create
optional arguments:
-h, --help show this help message and exit
-l, --loopback echo data back to the sending device too
-d, --debug log received data to stderr
Test example
from multiprocessing import Process
from unittest import TestCase, main
from virtualserialports import run as virtual_serial_ports_run
class TestAppTestCase(TestCase):
def setUp(self):
num_ports = 1
loopback = True
debug = False
args = (num_ports, loopback, debug)
self.process = Process(target=virtual_serial_ports_run, args=args)
self.process.start()
def tearDown(self):
self.process.kill()
self.process.join(timeout=8.0)
def test_serials(self):
# Test your code
pass