我正在尝试将信息从计算机发送到带蓝牙的Arduino。我正在使用Python进行蓝牙,使用pybluez库,在Windows上工作。
import bluetooth
target_name = "HC-05"
target_address = None
nearby_devices = bluetooth.discover_devices()
print nearby_devices
for bdaddr in nearby_devices:
print bluetooth.lookup_name( bdaddr )
if target_name == bluetooth.lookup_name( bdaddr ):
target_address = bdaddr
break
if target_address is not None:
print "found target bluetooth device with address ", target_address
sock=bluetooth.BluetoothSocket( bluetooth.RFCOMM )
print "Trying connection"
port = 3
sock.connect((target_address, port))
print "Trying sending"
sock.send("1 2 3")
print "Finished sending"
sock.close()
else:
print "could not find target bluetooth device nearby"
在arduino方面,我正在运行以下代码。
#include <stdlib.h>
//format [motor #][pwmA = 0, pwmB = 0]
const int MOTOR_PINS[5][2] = {{3,4}, {5,6}, {9, 10}, {23, 22}, {21, 20}};
const int SIGN = 1;
String msg = "";
void setup() {
Serial.begin(9600); //usb for testing
Serial1.begin(9600);
}
void loop() {
//check for inputs to bluetooth adapter
while (Serial1.available()) {
char reading = Serial1.read();
if ((int)reading == 13 ) {
Serial.println(msg);
parseString(msg);
msg = "";
}
else {
msg += reading;
}
}
}
(我知道Python端口是硬编码的,计划稍后进行清理。)
无论如何,当我运行Python代码时,我成功找到了蓝牙设备。但是,当我尝试连接时出现错误,说IOError: A socket operation failed because the destination host was down.
当我用PuTTY打开一个到COM3的会话时,我可以成功地将数据发送到Arduino(调用parseString)。我不明白有什么区别介于两者之间。
答案 0 :(得分:0)
也许COM3连接到蓝牙,所以你的Python程序可以连接?
或者,您可以使用pyserial连接到COM3。 http://pyserial.sourceforge.net/shortintro.html
答案 1 :(得分:0)
好的,感谢你的代码开始,通过修改它,我能够得到我想要的东西。
即使我得到了同样的IOError: A socket operation failed because the destination host was down.
所以我首先将我的HC-05与PC(Windows)配对,因为我的HC-05密码已锁定。接下来,我开始创建一个循环,可以检查给定范围之间的每个端口。
虽然影响不大但我的建议是提升HC的序列BAUD @ 115200,可能会更好。
所以我的Python代码看起来像这样。
import bluetooth
#--Pair HC-05 with PC first
target_name = "BLUESAND"
target_address = None
nearby_devices = bluetooth.discover_devices()
print nearby_devices
for bdaddr in nearby_devices:
print bluetooth.lookup_name( bdaddr )
if target_name == bluetooth.lookup_name( bdaddr ):
target_address = bdaddr
break
if target_address is not None:
print "found target bluetooth device with address ", target_address
sock=bluetooth.BluetoothSocket( bluetooth.RFCOMM )
print "Trying connection"
#######################################
i=0 # ---- your port range starts here
maxPort = 3 # ---- your port range ends here
err = True
while err == True and i <= maxPort:
print "Checking Port ",i
port = i
try:
sock.connect((target_address, port))
err = False
except Exception,e:
## print the exception if you like
i += 1
if i > maxPort:
print "Port detection Failed."
exit(0)
#######################################
print "Trying sending"
sock.send("Challange")
print "Finished sending"
print sock.recv()
print "Finished receiving"
sock.close()
else:
print "could not find target bluetooth device nearby"
我的Arduino代码看起来像这样。
String str;
void setup() {
Serial.begin(115200); // opens serial port, sets data rate
}
void loop() {
// send data only when you receive data:
if (Serial.available() > 0) {
str = Serial.readStringUntil('\n');
// say what you got:
Serial.print("I received: ");
Serial.println("\""+str+"\"");
//trim the string to skip the trailing CRLF
str.trim();
}
}