Skip to main content

Device discovery over Serial, USB, and TCP/IP

Project description

Dafydd

CI PyPI License Python Rust

Fast device discovery over Serial, USB, and TCP/IP.

Dafydd is a Rust library with Python bindings (via PyO3) for probing and enumerating physical hardware. It sweeps serial ports, USB buses, and TCP subnets concurrently — returning structured results with transport-specific metadata.

Features

  • Three transports, one API: Serial, USB, and TCP discovery share the same DeviceMatch result type and discovery interface
  • Preferred-address fast path: Try a known port/host first; fall back to a full sweep only if it fails
  • Streaming and watch modes: Get results as they arrive via callbacks, or poll for hot-plug events
  • Probe & response matching: Send custom bytes and filter devices by their response
  • ARP cache and mDNS acceleration: On TCP, prioritise hosts already in the ARP table or announcing via mDNS
  • No extra system dependencies: Pure Rust (no libpcap, no libusb), PyO3 for Python interop
  • Tokio-powered: Shared async runtime — no per-call startup cost

Installation

pip install dafydd

To build from source (requires Rust 1.95+):

uv sync
uv run maturin develop --release

Quick Start

TCP

import dafydd

# Scan local subnets for hosts accepting connections on port 502 (Modbus)
devices = dafydd.TcpDiscovery(port=502).discover()

# With a probe command: only return hosts that respond
devices = dafydd.TcpDiscovery(
    port=8080,
    subnets=["192.168.1.0/24"],
    probe_command=b"PING",
    connect_timeout_ms=200,
    io_timeout_ms=500,
).discover()

for d in devices:
    print(f"{d.host}:{d.port}  {d.response}")

Serial

import dafydd

# Sweep all ports at common baud rates
devices = dafydd.SerialDiscovery(
    probe_command=b"*IDN?\r\n",
    baud_rates=[9600, 115200],
    timeout_ms=500,
    response_terminator=b"\r\n",   # stop reading early once terminator arrives
).discover()

for d in devices:
    print(f"{d.address} @ {d.baud_rate} baud: {d.response}")

USB

import dafydd

# Filter by vendor ID, product ID, or device class
devices = dafydd.UsbDiscovery(vid=0x04D8).discover()

for d in devices:
    print(f"{d.address}  {d.info.get('product', '')}")

Streaming and Watch

All three discovery classes expose discover_streaming (callback per result) and watch (hot-plug polling):

import dafydd

token = dafydd.CancellationToken()

# Streaming — callback fires as each device is found
dafydd.SerialDiscovery(
    probe_command=b"*IDN?\r\n",
    baud_rates=[9600],
    timeout_ms=500,
    cancellation_token=token,
).discover_streaming(lambda d: print("found", d.address))

# Watch — polls for plug/unplug events until cancelled
import threading
threading.Thread(
    target=dafydd.UsbDiscovery(vid=0x04D8).watch,
    kwargs={
        "on_added":   lambda d: print("connected",    d.address),
        "on_removed": lambda d: print("disconnected", d.address),
        "interval_ms": 1000,
    },
    daemon=True,
).start()

# Stop from any thread
token.cancel()

Correlating USB and Serial

When the same physical device appears on both a USB bus and a serial port, correlate_usb_serial pairs them by USB serial number:

import dafydd

usb     = dafydd.UsbDiscovery().discover()
serial  = dafydd.SerialDiscovery(probe_command=b"", baud_rates=[9600], timeout_ms=100).discover()

for pair in dafydd.correlate_usb_serial(usb, serial):
    print(f"USB {pair.usb.address}  ↔  Serial {pair.serial.address}")

Utilities

# Auto-detect local subnets (what TcpDiscovery uses when subnets is not set)
print(dafydd.local_subnets())   # e.g. ['192.168.1.0/24', '10.0.0.0/24']

# Split a mixed list by transport
serial_ms, usb_ms, tcp_ms = dafydd.partition_by_transport(all_matches)

How It Works

  1. Preferred-address path: If preferred_host / preferred_port is set, try it first with a short timeout. On success, return immediately without sweeping.
  2. Full sweep: Enumerate available endpoints (ports, USB devices, or subnet hosts) and probe them concurrently via a Tokio JoinSet.
  3. ARP / mDNS acceleration (TCP): Hosts already in the system ARP cache or announcing via mDNS are probed before the raw subnet sweep, reducing latency for recently-seen devices.
  4. macOS tty dedup (Serial): Each physical port appears as both /dev/tty.* and /dev/cu.*; the blocking tty.* variant is filtered out automatically.

Building from Source

# Development build (editable install)
uv sync
uv run maturin develop

# Release build
uv run maturin develop --release

# Rust-only tests
cargo test
cargo bench

Contributing

See CONTRIBUTING.md for build instructions, test workflows, and PR guidelines.

License

Licensed under MIT. See LICENSE for details.

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

dafydd-0.1.0.tar.gz (64.0 kB view details)

Uploaded Source

Built Distributions

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

dafydd-0.1.0-cp313-cp313-win_amd64.whl (582.2 kB view details)

Uploaded CPython 3.13Windows x86-64

dafydd-0.1.0-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.0 MB view details)

Uploaded CPython 3.13manylinux: glibc 2.17+ x86-64

dafydd-0.1.0-cp313-cp313-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (1.0 MB view details)

Uploaded CPython 3.13manylinux: glibc 2.17+ ARM64

dafydd-0.1.0-cp313-cp313-macosx_11_0_arm64.whl (539.6 kB view details)

Uploaded CPython 3.13macOS 11.0+ ARM64

dafydd-0.1.0-cp312-cp312-win_amd64.whl (582.4 kB view details)

Uploaded CPython 3.12Windows x86-64

dafydd-0.1.0-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.0 MB view details)

Uploaded CPython 3.12manylinux: glibc 2.17+ x86-64

dafydd-0.1.0-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (1.0 MB view details)

Uploaded CPython 3.12manylinux: glibc 2.17+ ARM64

dafydd-0.1.0-cp312-cp312-macosx_11_0_arm64.whl (539.8 kB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

dafydd-0.1.0-cp311-cp311-win_amd64.whl (583.8 kB view details)

Uploaded CPython 3.11Windows x86-64

dafydd-0.1.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.0 MB view details)

Uploaded CPython 3.11manylinux: glibc 2.17+ x86-64

dafydd-0.1.0-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (1.0 MB view details)

Uploaded CPython 3.11manylinux: glibc 2.17+ ARM64

dafydd-0.1.0-cp311-cp311-macosx_11_0_arm64.whl (540.6 kB view details)

Uploaded CPython 3.11macOS 11.0+ ARM64

File details

Details for the file dafydd-0.1.0.tar.gz.

File metadata

  • Download URL: dafydd-0.1.0.tar.gz
  • Upload date:
  • Size: 64.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for dafydd-0.1.0.tar.gz
Algorithm Hash digest
SHA256 b834758abbc120c537e1430530f96e77f225429e176bbb11ad112788e5c70f2e
MD5 8fddc99675163f8498aafad56dccc061
BLAKE2b-256 931fd40795cca5eb31625b54313641b6ac45eef2f3f9a700336508bf9c330c70

See more details on using hashes here.

Provenance

The following attestation bundles were made for dafydd-0.1.0.tar.gz:

Publisher: release.yml on tstenvold/dafydd

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file dafydd-0.1.0-cp313-cp313-win_amd64.whl.

File metadata

  • Download URL: dafydd-0.1.0-cp313-cp313-win_amd64.whl
  • Upload date:
  • Size: 582.2 kB
  • Tags: CPython 3.13, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for dafydd-0.1.0-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 dff95f8c603e866a9ea0c0ecd1821383d18ddc1d6f18fc2413df9eaff27d7433
MD5 5a1eda54d7e82146cd46bbf01719a15b
BLAKE2b-256 36e8e95c04d7dd4d3a064c5d8b5f2e0ddc77021e1e19d5c5679122964ece6fed

See more details on using hashes here.

Provenance

The following attestation bundles were made for dafydd-0.1.0-cp313-cp313-win_amd64.whl:

Publisher: release.yml on tstenvold/dafydd

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file dafydd-0.1.0-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for dafydd-0.1.0-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 6bf8f39320979da4a82f07f7fb1f789e15d65cacf28e252d18e50f7a2eb3f009
MD5 2b7d2b4002a534e58edd7dc365e67da0
BLAKE2b-256 e7650447791d1759fddd887cb6a41195f90955180618db9dea45eceabb683fad

See more details on using hashes here.

Provenance

The following attestation bundles were made for dafydd-0.1.0-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl:

Publisher: release.yml on tstenvold/dafydd

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file dafydd-0.1.0-cp313-cp313-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for dafydd-0.1.0-cp313-cp313-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 ed230ced93bd882795967e152c71df499436f71ab08ef447ce8d652e0d58bcda
MD5 3d5f145723794279380c6795d946703b
BLAKE2b-256 3f913b8136663b27276471b53ca2c3b9a98e072df5b7b26edfc1a5c791d7e04d

See more details on using hashes here.

Provenance

The following attestation bundles were made for dafydd-0.1.0-cp313-cp313-manylinux_2_17_aarch64.manylinux2014_aarch64.whl:

Publisher: release.yml on tstenvold/dafydd

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file dafydd-0.1.0-cp313-cp313-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for dafydd-0.1.0-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 730c3f2813a6500fe606c436c840da50ada60e1758cffc391ea5edbfc1be1d41
MD5 bd9d55cd5f51ed23ff3771cff923a908
BLAKE2b-256 f49aa3d82de640bd7c0d94b5a3fd2deab4c53c88bb2fd13558f9226abc3adb11

See more details on using hashes here.

Provenance

The following attestation bundles were made for dafydd-0.1.0-cp313-cp313-macosx_11_0_arm64.whl:

Publisher: release.yml on tstenvold/dafydd

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file dafydd-0.1.0-cp312-cp312-win_amd64.whl.

File metadata

  • Download URL: dafydd-0.1.0-cp312-cp312-win_amd64.whl
  • Upload date:
  • Size: 582.4 kB
  • Tags: CPython 3.12, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for dafydd-0.1.0-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 34981f68299f831acc7dcdf503c10bcd5488752811b013526306c1305ff2e5a6
MD5 667b306ffa35d25aa0e1d47624ee2184
BLAKE2b-256 c70dfab52b6c958476342fa4aa8a6caadecc6907c82f2a21c78b3ac0bfba6874

See more details on using hashes here.

Provenance

The following attestation bundles were made for dafydd-0.1.0-cp312-cp312-win_amd64.whl:

Publisher: release.yml on tstenvold/dafydd

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file dafydd-0.1.0-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for dafydd-0.1.0-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 9270e62b86cdb14f13f07c666d01c72c393dc8111f73e12dfcb1ffe8c15e2285
MD5 6ade26ab7c68daad4be10487797e6011
BLAKE2b-256 87ca43a30ebbe14162365e2ace9eba4c7442bb0ed6352edb26130203c9fb54d3

See more details on using hashes here.

Provenance

The following attestation bundles were made for dafydd-0.1.0-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl:

Publisher: release.yml on tstenvold/dafydd

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file dafydd-0.1.0-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for dafydd-0.1.0-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 30d24a456d1a08fe8f4b0c96f6f040e9f6354611f6d0320a0296ac2ae6211cc3
MD5 75c6d4026b03f093d00037a02acc72bb
BLAKE2b-256 3a75904db1df7b92306212a332b908953a9de58eca1727925f134443786dbe54

See more details on using hashes here.

Provenance

The following attestation bundles were made for dafydd-0.1.0-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl:

Publisher: release.yml on tstenvold/dafydd

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file dafydd-0.1.0-cp312-cp312-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for dafydd-0.1.0-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 8e14caa89bf4298ffef2de6a622fa9c540acae1df6a8624e08ee4fa841b64a7a
MD5 612a987a790dd56d2f4d44bf049d259a
BLAKE2b-256 e827aef5f5db0ee5ff60022150fe927e67e836e57c2941da6c65f860b60b96bf

See more details on using hashes here.

Provenance

The following attestation bundles were made for dafydd-0.1.0-cp312-cp312-macosx_11_0_arm64.whl:

Publisher: release.yml on tstenvold/dafydd

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file dafydd-0.1.0-cp311-cp311-win_amd64.whl.

File metadata

  • Download URL: dafydd-0.1.0-cp311-cp311-win_amd64.whl
  • Upload date:
  • Size: 583.8 kB
  • Tags: CPython 3.11, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for dafydd-0.1.0-cp311-cp311-win_amd64.whl
Algorithm Hash digest
SHA256 d7234b36b521df6455a136d0d047f684155ca65596efa088ab7cff8ba38358a1
MD5 95666859a482803b7919445b49b8f03e
BLAKE2b-256 f36b06cfe4ee28653c8d43dd7bb75ac597c7ff93b245454c3e4c0945fda2cdba

See more details on using hashes here.

Provenance

The following attestation bundles were made for dafydd-0.1.0-cp311-cp311-win_amd64.whl:

Publisher: release.yml on tstenvold/dafydd

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file dafydd-0.1.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for dafydd-0.1.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 a07b039cff30fd0fef57ad32e458e3817430afcc8ac133d8cdf8636726a3fbf5
MD5 75bce467e9ade8ca59dec3d495a89b76
BLAKE2b-256 83d7ea1c989d2f24734184438f33bc8042c5855896f65237ed283a849e7822bb

See more details on using hashes here.

Provenance

The following attestation bundles were made for dafydd-0.1.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl:

Publisher: release.yml on tstenvold/dafydd

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file dafydd-0.1.0-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for dafydd-0.1.0-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 13e4e44c503b5b9ed2fd3e4e9eb978049ee38c180886ebdd1eb2d1ebb6173529
MD5 40f9eb9c62df4c44d7e6d647ae718e4e
BLAKE2b-256 63bb37d48f302719cacbb07f27965ad1b1e630a23ca890ffd2f9cfcb1befa90b

See more details on using hashes here.

Provenance

The following attestation bundles were made for dafydd-0.1.0-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl:

Publisher: release.yml on tstenvold/dafydd

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file dafydd-0.1.0-cp311-cp311-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for dafydd-0.1.0-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 0ebdbd7611dc24242be5289a1ba4b656c71cabc3bb4a75a196dce4f4a1de4834
MD5 61eb7379623c0bd1716427c6aabf99d7
BLAKE2b-256 8f9c50a4eddef3f3e60d97d05b36b596a5753689dea0f236800d26d5c9ce07de

See more details on using hashes here.

Provenance

The following attestation bundles were made for dafydd-0.1.0-cp311-cp311-macosx_11_0_arm64.whl:

Publisher: release.yml on tstenvold/dafydd

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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