Skip to main content

Python library and script to create temporary virtual serial ports

Project description

PyVirtualSerialPorts

A Python implementation of virtual serial ports. Useful for developing and testing programs which need to talk to a serial port.

You can:

  • Create a virtual port which echoes back any data sent to it: Demo showing characters being entered into a terminal emulator being echoed back

  • Create a two or more ports; sending data to one sends data to the others: Demo showing characters being sent alternately into two terminal emulators, with the characters appearing on the opposite terminal.

  • Monitor what is being sent between the ports: Demo showing characters being sent alternately in two terminal emulators, with the characters appearing on both terminals, and debug data showing in the pyvirtualserialports output.

It should work on Python 3.5+, however is only tested on supported Python versions.

Has no dependencies other than the Python standard library.

Currently works on *nix type systems. Tested on Ubuntu and MacOS, but should work on others e.g. BSD. Windows support is being worked on.

Installation

Install for current user

$ pip3 install PyVirtualSerialPorts

Install system-wide

$ sudo pip3 install PyVirtualSerialPorts

Running

If the script install folder is in your PATH (almost certainly the case if installed as root), you can simply run:

$ virtualserialports ...

Otherwise, you can use it as a module:

$ python3 -m virtualserialports ...

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

Examples

Create a single port, and echo back anything sent to it

$ virtualserialports -l 1

The created port will be printed on the command line e.g. /dev/pts/0, and can be used with any serial program, e.g. minicom:

$ minicom -D /dev/pts/0

Create a pair of ports, sending the data between them

$ virtualserialports 2

The two created ports will be printed, and again can be used with any serial program. E.g. in one terminal window:

$ minicom -D /dev/pts/0

and in a second:

$ minicom -D /dev/pts/1

Now typing data on one terminal will appear in the other.

Create three ports, sending data received from any of them to all three

$ virtualserialports -l 3

Use as a library

As of version 2.0.0, much improved support has been added for use as a library, with processing done in the background.

It is recommended to use as a context manager, which will handle setup and clean up nicely:

with VirtualSerialPorts(2) as ports:
    print(f'Port 1: {ports[0]}')
    print(f'Port 2: {ports[1]}')

    # Open and use ports as required.
    # When the context manager ends, ports will be removed.

ports is a list of strings, which can be used to open the ports, e.g with PySerial. A complete example is in example.py.

It can also be used without a context manager as follows:

vsp = VirtualSerialPorts(2)
vsp.open()
# `vsp.ports` is a list of strings of the created ports.

vsp.start()
# Use ports as you wish here.
vsp.stop()
vsp.close()

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pyvirtualserialports-2.0.0.tar.gz (6.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pyvirtualserialports-2.0.0-py3-none-any.whl (6.8 kB view details)

Uploaded Python 3

File details

Details for the file pyvirtualserialports-2.0.0.tar.gz.

File metadata

  • Download URL: pyvirtualserialports-2.0.0.tar.gz
  • Upload date:
  • Size: 6.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.7

File hashes

Hashes for pyvirtualserialports-2.0.0.tar.gz
Algorithm Hash digest
SHA256 0caa0f3b45911b0d818095cd0f0253ef2be0e1b9bd181b01f05d5ea7b6659a9a
MD5 3de391ef0b604552c7dc731f0ca3808f
BLAKE2b-256 7c55e75f1c5d060f6e296966e74f4c8352bbafae9dd7248990924d3b1037f128

See more details on using hashes here.

File details

Details for the file pyvirtualserialports-2.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for pyvirtualserialports-2.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 d86177e8f2d0ddd1e04bd399a329f220a9c7b668dc14129417d9b556874f1f67
MD5 385df588a0433e8e1bca96b53a829b37
BLAKE2b-256 997074328f514f27a233be781c628c40872e1408973e02538a536360e6a81f87

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page