Skip to main content

Simple serial connectivity to CDC (serial) USB devices exposed by a USBIPD service.

Project description

USBIP Serial client workflow Python PyPI - Implementation coverage


Overview


This package supports connecting to a CDC (serial) USB device exposed by a USBIPD server. USBIP is a protocol that allows sharing USB devices over a TCP/IP connection. The protocol specification can be found here. This package should be capable of running on any system that supports Python.

There are some issues sharing USB devices to docker containers, a major one being if the USB connection is lost it is difficult to recover the connection between the docker container and the hosting server.

Here's a link that discusses this issue and another solution.

The USBIP client implementation will only address USB devices that implemented the CDC protocol, basically simple serial devices. This allows for a direct connection to the USBIP server without the need of mapping USB devices into the container.

Installation


pip install serial-usbipclient should work for most users.

Usage


from serial_usbipclient.usbip_client import USBIPClient, HardwareID, USBIP_Connection

host: str = 'localhost'
port: int = 3240  # commonly used port for USBIPD servers
target: HardwareID = HardwareID(vid=1234, pid=5678)  # USB devices are identified by VID/PID
client: USBIPClient = USBIPClient(remote=(host, port))
client.connect_server()
client.attach(devices=[target])
connections: list[USBIP_Connection] = client.get_connection(device=target)

# using the established connection, data can be written to the USB device
# using the sendall() method
connections[0].sendall(data=b'\01\02\03\04')

# response data can be read either explicitly by specifying the size of the expected
# response, or if 0, the size is specified, up to a delimiter. The delimiter is a property of
# the connection and can be set, default=b'\r\n'
connections[0].delimiter = b'\n'
response: bytes = connections[0].response_data(size=0)  # reads until delimiter

SOUP


Module Version comments
Python 3.11, 3.12, 3.13, 3.14 Python interpreter(s)
py-datastruct 1.1.0 Serialization of binary to/from dataclasses
type-extensions 4.15.0 extensions for typing (SocketWrapper typing)

Useful Resources


For a Windows version of the usbipd server, look here. You can run this to share USB devices across a network, there are usbipd-clients for Linux & Windows.

Here's a USBIP server for testing written in RUST, https://github.com/jiegec/usbip.

Testing


A MockUSBIP service reads configuration information from the output of lsusb (e.g lsusb -d 1f46:1b01 -v). MockUSBIP will then play back this configuration. Just capture the output of the lsusb command and save with the .lsusb suffix in the test folder. The file name should be the busnum/devnum number.

1-1.lsusb
1-2.lsusb
1-3.lsusb
99-99.lsusb

Would result in 4 devices with busid values of 1-1, 1-2, 1-3 and 99-99. Please note the file 99-99.lsusb is reserved to provide a device to generate failing USBIPD attachments, and 1-2 & 1-3 have the same VID/PID (for testing multiple identical device connections).

During testing, the MockUSBIP service acts as a stand-in for an actual USBIP server. Since the tests are run in parallel, the port on which the service listens must be unique for each unit test. This is accomplished by the conftest.py::pytest_sessionstart which is run when the pytest session is started and collects all tests being run into a file called lists_of_tests.json. The unit test's index into this array is used to determine the offset to be added to the port base (typically 3240). The SocketWrapper class is overridden in unit tests to inject low-level socket failures.

Tooling


This package was created using JetBrains PyCharm Professional IDE, the repository does not contain any IDE specific files. Development work was done using Windows 11 & Python 3.12.5, with final verification on an Ubuntu 20.04 system.

Static code analysis performed using radon and xenon.

Packages required to run tests


Module Version comments
pytest 9.0.0 unit testing framework
pytest-xdist 3.8.0 distributes testing across multiple cpu/cores
coverage 7.11.3 coverage of unit tests
pylint 4.0.2 linter, ensures adherence to PEP-8 standards
pytest-cov 7.0.0 integrates coverage with pytest
pytest-timeout 2.4.0 provides ability to timeout pytest unit tests
mypy 1.18.2 type checking
radon 6.0.1 static code analysis
xenon 0.9.3 static code analysis with thresholds

Packages required to publish to PyPi


Module Version comments
poetry-core 2.2.1 build system

All tooling is defined in the pyproject.toml and managed using poetry as follows:

poetry install --with tests

Tooling is not needed to run the package but is required for testing and packaging.

Build Process


Store the PyPi API token

keyring set https://upload.pypi.org/legacy/ __token__

when prompted, enter the API token you created using PyPi.

To build the distribution and upload to PyPi

poetry build
poetry publish

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

serial_usbipclient-1.1.2.tar.gz (28.0 kB view details)

Uploaded Source

Built Distribution

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

serial_usbipclient-1.1.2-py3-none-any.whl (29.8 kB view details)

Uploaded Python 3

File details

Details for the file serial_usbipclient-1.1.2.tar.gz.

File metadata

  • Download URL: serial_usbipclient-1.1.2.tar.gz
  • Upload date:
  • Size: 28.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.2.1 CPython/3.12.10 Windows/11

File hashes

Hashes for serial_usbipclient-1.1.2.tar.gz
Algorithm Hash digest
SHA256 4252b14ce2060c9bc6dcb371cd25586016d5749750f7603caa4bdc151b1a3b82
MD5 5eefce30a9480eb1cabde987c5ded04f
BLAKE2b-256 2bde72c5844287b22a12f83e2972882993cdbc3b038e5997b5384a5edd7911b3

See more details on using hashes here.

File details

Details for the file serial_usbipclient-1.1.2-py3-none-any.whl.

File metadata

File hashes

Hashes for serial_usbipclient-1.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 17eff3fd869c7bf78f9e8e1aac5939ba5160157427ee011554d49d513f93c852
MD5 a7eaf12891b451010f384e8a32695001
BLAKE2b-256 392d6917c802c05707d7b3ec06325f322dde91d537ab374618812dc1a15d42fe

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