使用蓝牙在两个树莓派之间发送数据

时间:2017-09-06 17:30:56

标签: python bluetooth raspberry-pi

到目前为止,我已经能够使用bluetoothctl通过蓝牙连接两个覆盆子pi。 但是,当我们连接时,我仍然坚持如何实时地在它们之间发送和接收数据。

编辑: 我尝试过的事情:

 sudo rfcomm bind 0 (insert address)

如果我是正确的,这应该将蓝牙地址绑定到/ dev / rfcomm0 我为这两个RPI做了这个,然后从一端写了一个写,从另一端读了一个。

import serial

btSerial = serial.Serial("/dev/rfcomm0", baudrate=9600, timeout=100)

while True:
        rcv = btSerial.read(512)
        print(rcv)

发送结束:

import serial
import time

print('x')
btSerial = serial.Serial("/dev/rfcomm0", baudrate=9600, timeout=0.5)
i = 0
print('started')


while True:
    btSerial.write("OK!")
    i = i + 1
    print(i)
    time.sleep(1)

发送结束时出现错误:

x
started
1
Traceback (most recent call last):
  File "testBT.py", line 11, in <module>
    btSerial.write("OK!")
  File "/usr/lib/python2.7/dist-packages/serial/serialposix.py", line 489, in write
    raise SerialException('write failed: %s' % (v,))
serial.serialutil.SerialException: write failed: [Errno 5] Input/output error

阅读结束时出错:

Traceback (most recent call last):
  File "test.py", line 6, in <module>
    rcv = btSerial.read(512)
  File "/usr/lib/python2.7/dist-packages/serial/serialposix.py", line 460, in read
    raise SerialException('device reports readiness to read but returned no data (device disconnected?)')
serial.serialutil.SerialException: device reports readiness to read but returned no data (device disconnected?)

0 个答案:

没有答案