如何在Windows10上使用python从BT模块(HC-05)接收数据?

How to recieve data from BT module (HC-05) by using python on Windows 10?

我正在尝试从我的 HC-05 蓝牙模块接收 GPS 数据。 我可以在任何串行绘图仪程序中看到完整的数据,但是我需要为我的 Raspberry PI.

使用 Python 可执行文件

我尝试了以下从互联网上找到的代码;

"""
A simple Python script to receive messages from a client over 
Bluetooth using PyBluez (with Python 2). 
"""

import bluetooth

hostMACAddress = 'C8:09:A8:56:11:EC'  # The MAC address of a Bluetooth adapter on the server. The server might have
# multiple Bluetooth adapters.
port = 9
backlog = 1
size = 1024
s = bluetooth.BluetoothSocket(bluetooth.RFCOMM)
s.bind((hostMACAddress, port))
s.listen(backlog)
try:
    client, clientInfo = s.accept()
    while 1:
        data = client.recv(size)
        if data:
            print(data)
            client.send(data)  # Echo back to client
except:
    print("Closing socket")
    client.close()
    s.close()

但是它给我下面的错误,对于行“s.bind((hostMACAddress, port))”。我在 cmd window 中有 运行 "ipconfig /all" 来查看蓝牙适配器 MAC 地址,并在我电脑的 "蓝牙设备" 中检查高级设置以找到相应的端口。

我怀疑的其他问题是我正在使用 Python 3.8,而在评论区它写着 Python 2。我不确定 3.xx 是否向后向后兼容2.xx.

C:\Users\aliul\PycharmProjects\pythonProject\venv\Scripts\python.exe C:/Users/aliul/PycharmProjects/pythonProject/main.py
Traceback (most recent call last):
  File "C:/Users/aliul/PycharmProjects/pythonProject/main.py", line 14, in <module>
    s.bind((hostMACAddress, port))
  File "C:\Users\aliul\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\bluetooth\msbt.py", line 84, in bind
    bt.bind (self._sockfd, addr, port)
OSError: G

我是 python 的新手,非常感谢任何帮助! 谢谢

我想出了接收数据导入“串行”包而不是 pybluez 的“蓝牙”。源码如下,你只需要找到你的socket的串口地址,根据你的蓝牙模块设置baudrate,timeout,parity,stopbits参数即可!

import serial

serialPort = serial.Serial(port='COM8', baudrate=9600, timeout=0, parity=serial.PARITY_EVEN, stopbits=1)
size = 1024

while 1:
    data = serialPort.readline(size)

    if data:
        print(data)