【问题标题】:Python Communication to Arduino via BluetoothPython 通过蓝牙与 Arduino 通信
【发布时间】:2016-07-29 22:16:15
【问题描述】:

我正在尝试使用 python 来控制(发送值)通过蓝牙连接到 HC-06 的 arduino。

我在 Windows 10 机器上使用 python 3.4.2。

问题在于如何做到这一点。我尝试了几种方法都没有成功。

我尝试过的:

我尝试使用 Pybluez 库来查找 HC-06,连接到它并发送数据。这样做的问题是没有办法找出我应该通过哪个端口发送。

driver_socket = bluetooth.BluetoothSocket(bluetooth.RFCOMM)
    driver_socket.connect((addr,port))
    print("Connected!")
    time.sleep(1)
    driver_socket.send("a")
    print("Sent Message!")
    driver_socket.close()

我试图通过 bluetooth.service 找到端口,但返回一个空列表,可能是由于 HC-06 的功能有限。

接下来我尝试将设备与我的计算机预先配对,并设置一个 COM 端口以通过 Pyserial 发送数据。我进行测试时,HC-06 绑定到 COM8。

s = serial.Serial('COM8', 9600,timeout = 1)
print("connected!")
time.sleep(10)
s.write(b"Hello")
print("Sent Message!")

这似乎可行,但在尝试建立连接几秒钟后,我收到以下错误:

self.open()
  File "C:\Python34\lib\site-packages\serial\serialwin32.py", line 62, in open
    raise SerialException("could not open port %r: %r" % (self.portstr, ctypes.WinError()))
serial.serialutil.SerialException: could not open port 'COM8': OSError(22, 'The network location cannot be reached. For information about network troubleshooting, see Windows Help.', None, 1231)

Windows 10 还会产生一个问题,当连接到设备时,会出现一个弹出窗口,要求您“设置”设备。如果单击它,则会出现一个新窗口,指出存在问题,您应该重新连接设备。不管你是否点击这个弹窗,错误依旧。

任何帮助将不胜感激。

【问题讨论】:

    标签: python bluetooth arduino


    【解决方案1】:

    所以我猜时间会治愈所有的伤口。

    解决方案是使用 pyserial 并首先将笔记本电脑与 HC-06 配对。

    确保配对成功很重要,所有HC-06芯片的默认密码都是“1234”

    配对后,您必须进入 Windows 中的蓝牙设置,然后会有一个高级菜单,您可以在其中找到传出 COM 端口号,这是您将在 pyserial 中用于打开与 HC- 的串行连接的数字06

    s = serial.Serial("COM3",9600,timeout = 2)
    s.write(bytes("hello!",'utf-8'))
    

    多田!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-26
      相关资源
      最近更新 更多