Saya mencoba menulis program sederhana untuk berkomunikasi antara RPi dan ponsel bluetooth. Saya pergi dengan tutorial ini: Bluetooth Python tutorial untuk RPi dan dapat terhubung ke telepon dan mereka terikat dengan benar. Lalu saya menulis skrip python ini:
#! /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()
Kode tersebut seharusnya membaca perintah dari perangkat BT dan mengirimkannya ke pengontrol Motor. Tapi saya menerima kesalahan ini:
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')
Adakah pikiran?
apakah Anda mendapatkan pesan yang sama jika Anda menjalankannya dengan "sudo"?
—
merampok
Itulah pesannya ketika saya menjalankannya dengan sudo
—
Javi