Python SDK

SmartUSBHub Python SDK

基于 USB CDC 的开源 Python 库,Windows、macOS、Linux 免驱动,即插即用。Open-source Python library over USB CDC. Driverless on Windows, macOS and Linux.

通过 pip 安装,无需额外驱动或系统配置:Install via pip — no extra drivers or system configuration.

pip install smartusbhub
Python 3.7+WindowsmacOSLinux
依赖Dependencies
  • pyserial
  • 标准库:threading, timestdlib: threading, time

pyserial 为唯一第三方依赖,pip 安装时自动处理。pyserial is the only third-party dependency, installed automatically.


连接Connection
SmartUSBHub.scan_and_connect() → SmartUSBHub

自动扫描所有串口,连接第一个 SmartUSBHub 设备,返回实例。Scans all serial ports, connects to the first SmartUSBHub, returns an instance.


通道Channels
hub.get_channels() → list[int]

返回设备实际通道编号列表。请始终使用此方法,不要硬编码通道数。Returns the device's actual channel numbers. Always use this — don't hardcode counts.

channels = hub.get_channels() # SmartUSBHub Pro 7CH → [1, 2, 3, 4, 5, 6, 7] # SmartUSBHub Pro 4CH → [1, 2, 3, 4]

电源控制Power control
hub.set_channel_power(channel: int, enable: bool) → None

开关指定通道的电源输出。channel 从 1 开始。Enable or disable power on a channel. Channel is 1-indexed.

# 打开第 1 路电源 hub.set_channel_power(1, True) # 关闭所有通道 for ch in hub.get_channels(): hub.set_channel_power(ch, False)

数据线控制Data-line control
hub.set_channel_usb2_dataline(channel: int, enable: bool) → None

控制指定通道的 USB 2.0 D+/D− 数据线,可在保持供电的同时断开数据线。Control the USB 2.0 D+/D− lines on a channel — cut data while keeping power on.

# 断开第 2 路数据线(保持供电) hub.set_channel_usb2_dataline(2, False)

测量Measurement
hub.get_channel_measurements(channel: int) → Measurements

读取指定通道的电压(mV)和电流(mA)。返回含 voltage_mv 与 current_ma 的对象。Read voltage (mV) and current (mA). Returns an object with voltage_mv and current_ma.

m = hub.get_channel_measurements(1) print(f"电压 {m.voltage_mv} mV, 电流 {m.current_ma} mA") # 100Hz 循环采集 import time while True: m = hub.get_channel_measurements(1) print(m.voltage_mv, m.current_ma) time.sleep(0.01) # 10ms = 100Hz

quickstart.py
from smartusbhub import SmartUSBHub

hub = SmartUSBHub.scan_and_connect()

# 逐路上电并读取电流
for ch in hub.get_channels():
    hub.set_channel_power(ch, True)
    m = hub.get_channel_measurements(ch)
    print(f"ch{ch}: {m.current_ma} mA")