问题描述
您如何通过蓝牙和Python处理和接收串行数据?
How do you process and receive serial data via Bluetooth and Python?
我正在尝试制作一个简单的Python服务器,以通过在这里解释.
I'm trying to make a simple Python server that access data via Bluetooth as explained here.
我的 server.py
文件是:
#!/usr/bin/env python
import os
import glob
import time
import random
from bluetooth import *
def read_temp():
return random.random()
server_sock=BluetoothSocket( RFCOMM )
server_sock.bind(("",PORT_ANY))
server_sock.listen(1)
port = server_sock.getsockname()[1]
uuid = "94f39d29-7d6d-437d-973b-fba39e49d4ee"
advertise_service( server_sock, "TestServer",
service_id = uuid,
service_classes = [ uuid, SERIAL_PORT_CLASS ],
profiles = [ SERIAL_PORT_PROFILE ],
# protocols = [ OBEX_UUID ]
)
while True:
print "Waiting for connection on RFCOMM channel %d" % port
client_sock, client_info = server_sock.accept()
print "Accepted connection from ", client_info
try:
data = client_sock.recv(1024)
if len(data) == 0: break
print "received [%s]" % data
if data == 'temp':
data = str(read_temp())+'!'
client_sock.send(data)
else:
data = 'WTF!'
client_sock.send(data)
print "sending [%s]" % data
except IOError:
pass
except KeyboardInterrupt:
print "disconnected"
client_sock.close()
server_sock.close()
print "all done"
break
当我第一次运行它时,我得到了错误:
When I first ran this, I was getting the error:
bluetooth.btcommon.BluetoothError: (2, 'No such file or directory')
Googled显示可以通过启用进行修复兼容模式并通过编辑文件/lib/systemd/system/bluetooth.service
并更改行来加载串行配置文件:
which Googled showed could be fixed by enabling compatibility mode and loading the serial profile by editing file /lib/systemd/system/bluetooth.service
and changing line:
ExecStart=/usr/lib/bluetooth/bluetoothd
收件人:
ExecStart=/usr/lib/bluetooth/bluetoothd -C
,然后运行 sudo sdptool add SP
.
现在 python server.py
可以正常运行了,我可以将运行它的机器与我的Android手机配对.但是,似乎无法接收任何数据.
Now python server.py
seems to run without error, and I can pair the machine running it to my Android phone. However, it seems it's unable to receive any data.
我尝试使用蓝牙终端仿真应用程序,例如BlueTerm,BT Simple Terminal和Arduino BT,但是当我连接到服务器时,输入文本并按Enter, server.py
没有任何响应.它将最初报告已接受的连接...",并且已接收到空字符串,但此后什么也没收到.
I've tried using bluetooth terminal emulation apps like BlueTerm, BT Simple Terminal, and Arduino BT, but when I connect to the server enter text and press enter, there's no response from server.py
. It will initially report a "Accepted connection..." and having received and empty string, but it receives nothing after that.
由于没有报告任何明显的错误,所以我不确定如何诊断问题.如何确定问题是否出在我的Python代码中?还是服务器上的蓝牙配置?还是我的Android手机?
Since there's no explicit error being reported, I'm not sure how to diagnose the problem. How would I determine if the problem lies in my Python code? Or the bluetooth configuration on the server? Or my Android phone?
推荐答案
正确的代码是:
#!/usr/bin/env python
"""
A simple test server that returns a random number when sent the text "temp" via Bluetooth serial.
"""
import os
import glob
import time
import random
from bluetooth import *
server_sock = BluetoothSocket( RFCOMM )
server_sock.bind(("",PORT_ANY))
server_sock.listen(1)
port = server_sock.getsockname()[1]
uuid = "94f39d29-7d6d-437d-973b-fba39e49d4ee"
advertise_service( server_sock, "TestServer",
service_id = uuid,
service_classes = [ uuid, SERIAL_PORT_CLASS ],
profiles = [ SERIAL_PORT_PROFILE ],
# protocols = [ OBEX_UUID ]
)
print "Waiting for connection on RFCOMM channel %d" % port
client_sock, client_info = server_sock.accept()
print "Accepted connection from ", client_info
while True:
try:
req = client_sock.recv(1024)
if len(req) == 0:
break
print "received [%s]" % req
data = None
if req in ('temp', '*temp'):
data = str(random.random())+'!'
else:
pass
if data:
print "sending [%s]" % data
client_sock.send(data)
except IOError:
pass
except KeyboardInterrupt:
print "disconnected"
client_sock.close()
server_sock.close()
print "all done"
break
这篇关于使用Python通过蓝牙串行进行通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!