如何使用pyserial获取蓝牙端口方向?

How to obtain bluetooth port direction with pyserial?

我正在尝试通过 python 连接到 RN42 模块。当 RN42 与 W10 配对时,它会创建两个虚拟 COM 端口(传出和传入)。我需要连接到传出端口。

我正在尝试自动执行此操作。我试过:

import serial
import serial.tools.list_ports as port_lst

ports = list(port_lst.comports())
bluetooth_ports = []
for p in ports:
    if 'Bluetooth' in p.description:
        bluetooth_ports += [p.device]
bluetooth_com = serial.Serial(bluetooth_ports[0],115200)

我以为第一个端口通常是传出端口,但我已经将模块与另一台计算机配对,这不适用(第二个端口是传出端口)。有没有办法找出COM端口的方向?

谢谢!!!

虽然这是一个古老的问题,但我自己也一直在寻找这个问题的答案有一段时间了,既然我终于弄明白了,我希望其他人也能找到答案。在 in the hand and its accompanying gist:

的博客条目的帮助下

诀窍是使用pySerial获取hwid,然后解析地址。一对中的传入端口的地址为零,传出端口的地址为非零。这是一些丑陋的 Python 解码它的代码:

import serial.tools.list_ports
cp=serial.tools.list_ports.comports()
for p in cp:
    if "BTHENUM" in p.hwid:
        start_of_address=p.hwid.rfind("&")
        end_of_address=p.hwid.rfind("_")
        address=p.hwid[start_of_address+1:end_of_address]
        if int(address,16)==0:
            port_type="incoming"
        else:
            port_type="outgoing"
        print(p.hwid)
        print(p.name, address, port_type)

并且输出:

BTHENUM\{00001101-0000-1000-8000-00805F9B34FB}_LOCALMFG&0000&CC47540&0&000000000000_000000A8
COM4 000000000000 incoming
BTHENUM\{00001101-0000-1000-8000-00805F9B34FB}_LOCALMFG&0002&CC47540&0&209BA5420081_C00000000
COM5 209BA5420081 outgoing