使用python和蓝牙进行通讯


8

我正在尝试编写一个简单的程序来在RPi和蓝牙手机之间进行通信。我使用了该教程:用于RPi的Bluetooth Python教程, 能够连接到手机,并且绑定正确。然后我写了这个python脚本:

#! /usr/bin/python
import serial
from protocol import *
from MotorControllerP import *

def startBluetoothServer():
        bluetoothSerial = serial.Serial("/dev/rfcomm1",baudrate=9600)
        print("Bluetooth connected")
        try:
                while 1:
                        data = bluetoothSerial.readLine()
                        if not data: break
                        data = data.decode()
                        print("Data received: "+data)
                        if data[:3] == Client.INIT_HEY:
                                print("Initiallizing connection")
                                bluetoothSerial.write((Server.INIT_OK+"\n").enc$
                                print("Connection initiallized")
                        elif data[:3] == Client.KTHXBYE:
                               bluetoothSerial.write(Server.CLOSE.encode())
                                exitAndClean()
                        elif data[:3] == Client.CUSTOM_MOVE:
                                data = str(data)
                                formattedData = data.split(",")
                                direction = formattedData[1]
                                left = formattedData[2]
                                right = formattedData[3]
                                response = customSpeed(direction,left,right)
                                print(direction+","+left+","+right)
                                bluetoothSerial.write((str(response)+"\n").enco$
                        else:
                                print("Command not understood: "+data)
                bluetoothSerial.write(Server.CLOSE.encode())
        except KeyboardInterrupt:
                print("Rage Quit")
        except:
                print("Error happened:",sys.exc_info())
        finally:
                exitAndClean()

该代码应该从BT设备读取命令并将其发送到电机控制器。但我收到此错误:

Traceback (most recent call last):
  File "/usr/lib/python2.7/multiprocessing/process.py", line 258, in _bootstrap
    self.run()
  File "/usr/lib/python2.7/multiprocessing/process.py", line 114, in run
    self._target(*self._args, **self._kwargs)
  File "/home/pi/Desktop/Car/RPiCar/BluetoothServer.py", line 7, in startBluetoothServer
    bluetoothSerial = serial.Serial("/dev/rfcomm1",baudrate=9600)
  File "/usr/lib/python2.7/dist-packages/serial/serialutil.py", line 260, in __init__
    self.open()
  File "/usr/lib/python2.7/dist-packages/serial/serialposix.py", line 280, in open
    self._reconfigurePort()
  File "/usr/lib/python2.7/dist-packages/serial/serialposix.py", line 409, in _reconfigurePort
    termios.tcsetattr(self.fd, TERMIOS.TCSANOW, [iflag, oflag, cflag, lflag, ispeed, ospeed, cc])
error: (5, 'Input/output error')

有什么想法吗?


如果使用“ sudo”运行它会得到相同的消息吗?
2014年

这是我使用sudo运行时的消息
Javi 2014年

Answers:


By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.