0

我正试图让我的 ardunio 通过蓝牙与我的树莓派通信。到目前为止,我已经让他们使用 Minicomm 进行通信,但我还没有成功使用 pyserial。我尝试了无数的事情,但我无法让它发挥作用;我肯定知道的几件事(来自 minicomm 和其他东西):

  1. 有联系
  2. 它的串口 /dev/rfcomm0
  3. arduino 上的代码正在运行
  4. BPS 为 9600

这是我在我的 pi 上的代码

    import serial
    import time
    port="/dev/rfcomm0"
    print('hello world')
    bluetooth= serial.Serial(port,9600)
    print ('hello world 2')
    bluetooth.flushInput()
    print ('hello world 3')
    for i in range(100):
        print("we are in the for loop",i)
        inputs=bluetooth.readline()
        print("we are in the inputs for loop",i)
        inputasinteger= int(inputs)
        if inputs:
                print('we have inputs')
                fileb= open("blue.txt",'wU')
                fileb.write(inputasInteger*10)
        time.sleep(.1)
        print('sleeping')
    fileb.close()
    print('file has been closse')
    exit()

你可以假设缩进是正确的......我不确定如何在这里修复它们但是我的代码一直运行到行 inputs=bluetooth.readline(); 然后它就挂了有没有人有这方面的经验?有什么解决办法吗?知道我可以使用的任何其他模块吗?

4

1 回答 1

2

您确定 Arduino 端正在发送换行符(“\n”)吗?除非收到换行符,否则代码将挂起。请参阅 readline() 的 pySerial 文档: pySerial API

读取以行尾 (eol) 字符(默认为 \n)或直到超时结束的行。

如果您确定 Arduino 正在发送换行符,请在此处发布来自 Arduino 的代码。

于 2013-07-09T14:33:51.013 回答