我想在带有集成蓝牙模块的Raspberry Pi 3和Windows 10机器之间设置串行蓝牙通信。raspi是服务器,运行python脚本,处理连接和数据传输。我已经全部设置好了,在互联网上学习了几个教程,在服务器端的效果非常好。我已经在我的android手机上用串行终端测试了服务器,它按预期工作:所有消息都正确传输,并且只要我想,它就保持连接。另一方面,用我的windows机器进行测试造成了很多问题。我已经解决了大部分问题,现在我可以通过串行终端将我的windows机器连接到raspi。我现在的问题是,过了一会儿,连接就断了,通常在10到40秒后,我找不到解决方法。windows上的错误消息为:cannot open COM4
,raspi上的错误消息为:bluetooth.btcommon.BluetoothError: [Errno 110] Connection timed out
。
这是我的python脚本,id,这实际上是问题所在:
import serial, bluetooth, subprocess, select
serialPort0 = serial.Serial(
port = '/dev/serial0',
baudrate = 115200,
parity = serial.PARITY_NONE,
stopbits = serial.STOPBITS_ONE,
bytesize = serial.EIGHTBITS,
timeout = 1
)
server_sock = bluetooth.BluetoothSocket(bluetooth.RFCOMM)
server_sock.bind(("", bluetooth.PORT_ANY))
server_sock.listen(1)
port = server_sock.getsockname()
uuid = "94f39d29-7d6d-437d-973b-fba39e49d4ee"
bluetooth.advertise_service(server_sock, "bluetoth_server", service_id=uuid,
service_classes=[uuid, bluetooth.SERIAL_PORT_CLASS],
profiles=[bluetooth.SERIAL_PORT_PROFILE],
# protocols=[bluetooth.OBEX_UUID]
)
print("Waiting for connection on RFCOMM channel", port[1], ", addr", port[0])
client_sock, client_info = server_sock.accept()
print("Accepted connection from", client_info)
client_sock.setblocking(0)
while True:
if not serialPort0.isOpen:
raise Exception("Serial port closed unexpected")
break
ready = select.select([client_sock], [], [], 0.5)
if ready[0]:
blu_data = client_sock.recv(4096)
if not blu_data:
break
serialPort0.write(blu_data)
ser_data = serialPort0.readline().decode("utf-8")
client_sock.send(ser_data)
client_sock.close()
server_sock.close()
它从串行端口和蓝牙串行端口读取数据,并分别发送到另一个端口。 我正处在这样一个时刻,我不知道还有什么可以尝试,我真的很感谢你的帮助。先谢谢你
将指向与超时相关的描述
/etc/bluetooth/main.conf
蓝牙配置文件
您需要取消对pairabletimeout=0的注释,这将防止超时问题
您可能想查看Raspberry蓝牙配置(最新答案)以了解详细信息
Bluetooth Serial Configuration
相关问题 更多 >
编程相关推荐