Utilizzo di Python con Bluetooth per comunicare


8

Sto cercando di scrivere un semplice programma per comunicare tra l'RPi e un cellulare bluetooth. Sono andato con questo tutorial: tutorial Bluetooth Python per RPi ed è stato in grado di connettersi al telefono e si sono collegati correttamente. Quindi ho scritto questo script 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()

Il codice dovrebbe leggere un comando dal dispositivo BT e inviarlo a un controller del motore. Ma ricevo questo errore:

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')

qualche idea?


ricevi lo stesso messaggio se lo esegui con "sudo"?
ruba il

Questo è il messaggio quando lo eseguo con sudo
Javi il

Risposte:


Utilizzando il nostro sito, riconosci di aver letto e compreso le nostre Informativa sui cookie e Informativa sulla privacy.
Licensed under cc by-sa 3.0 with attribution required.