设备发现与连接
发现候选串口,配置自动发现或固定端口,并用逻辑名称选择多台 Exo。
当前版本 · 1.4.0
设备发现与连接
发现设备
from qnbot_sdk.exo import discover_exos
for candidate in discover_exos():
print(candidate.transport_type.value)
if candidate.error:
print(candidate.port, candidate.error.code, candidate.error.message)
else:
print(candidate.port, candidate.device_info.product)每条 DiscoveredExo 要么包含 device_info,要么包含 error。错误码为 PORT_OPEN、PROTOCOL_ERROR 或 INCOMPATIBLE。
这些错误表示单个候选端口的探测结果,其他候选仍会继续返回。如果串口枚举操作整体失败,discover_exos() 会直接抛出发现异常,而不是构造没有 port 的 DiscoveredExo。
DiscoveredExo.transport_type 是必需字段,类型为 ExoTransportType,当前值为 ExoTransportType.SERIAL(serial)。自行构造或反序列化发现记录时也要提供该字段。
自动发现与固定串口
from qnbot_sdk import Sdk, SerialConnection
from qnbot_sdk.exo import ExoConfig
# 唯一设备自动发现
automatic = ExoConfig()
# 固定串口
fixed = ExoConfig(
name="primary-exo",
connection=SerialConnection(port="/dev/ttyUSB0"),
)
sdk = Sdk(devices=(fixed,))
device = sdk.exo().device("primary-exo")Windows 端口形如 COM3。Exo 不支持用 SerialConnection.serial_number 定位;序列号只用于展示设备信息。多设备必须为每台设备提供不同的逻辑 name 和明确端口。
设备选择
exo.device()仅在配置中恰好有一台 Exo 时使用;零台会报告未找到,多台会报告选择不明确。exo.device("primary-exo")按ExoConfig.name选择;名称不存在会报告未找到。name是应用配置的逻辑名称,不是hardware_name或硬件序列号。
启动前读取设备信息
get_device_info() 可以在 start() 前调用。它读取 SDK 创建阶段已经确定的不可变设备快照,不会在每次调用时重新访问硬件。
info = device.get_device_info()
print(info.product, info.firmware)
print("encoders", info.components.encoders.available,
"/", info.components.encoders.total_slots)
print("telemetry", info.capabilities.telemetry)capabilities 表示功能是否可用;components 表示实际存在数量与设备总槽位。不要用组件数量代替能力判断,例如读取 IMU 前仍应检查 capabilities.imus。