8

I'm trying to write a simple program to communicate between the RPi and a bluetooth cellphone. I went with this tutorial: Bluetooth Python tutorial for RPi and was able to connect to the phone and they bounded correctly. Then I wrote this python script:

#! /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()

The code is supposed to read a command from the BT device and send it to a Motor controller. But I receive this error:

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

Any thoughts?

  • do you get the same message if you run it with "sudo" ? – rob Oct 21 '14 at 7:48
  • That's the message when I run it with sudo – Javi Oct 21 '14 at 7:56
2

Possibly you don't have /dev/rfcomm1. I suggest to use python-bluetooth library. Check this: example usage

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.