我必须构造汽车,可以移动的结果为基础的代码,所以必须通过蓝牙发送字节的信息(使用蓝牙模块Hc -05)运行python (目前使用jupyter笔记本,python 3)。我已经试了好几天了都没找到。这是我一直在使用的代码。使用套接字库
import socket
serverMACAddress = '00:00:00:00:00:00'
port = 4
s = socket.socket(socket.AF_BLUETOOTH, socket.SOCK_STREAM, socket.BTPROTO_RFCOMM)
s.connect((serverMACAddress,port))
s.send(bytes("A",'UT
AttributeError: module 'socket' has no attribute 'AF_BLUETOOTH'F-8'))
s.close()和序列库和时间库
import serial
import time
port="COM4" #This will be different for various devices,COM port.
bluetooth=serial.Serial(port, 9600)#Start communications with the bluetooth unit
bluetooth.flushInput() #This gives the bluetooth a little kick
bluetooth.write(b"A")#These need to be bytes not unicode
bluetooth.close() #Otherwise the connection will remain open until a timeout 这是arduino的代码
#include <SoftwareSerial.h>
SoftwareSerial miBT(10,11);
void setup() {
Serial.begin(9600);
miBT.begin(38400);
pinMode(6, OUTPUT);
pinMode(5, OUTPUT);
pinMode(4, OUTPUT);
pinMode(3, OUTPUT);
}
void loop(){
if (miBT.available()>0) {
byte input=miBT.read();
if(input == 'B'){ //FORWARD ----
digitalWrite(6, HIGH);
digitalWrite(5, LOW);
digitalWrite(4, LOW);
digitalWrite(3, HIGH);
}
if(input == 'A'){ //BACKWARD ---
digitalWrite(6, LOW);
digitalWrite(5, HIGH);
digitalWrite(4, HIGH);
digitalWrite(3, LOW);
}
if(input == 'C'){ //LEFT ---
digitalWrite(6, LOW);
digitalWrite(5, HIGH);
digitalWrite(4, LOW);
digitalWrite(3, LOW);
}
if(input == 'D'){ //RIGHT ---
digitalWrite(5, LOW);
digitalWrite(4, HIGH);
digitalWrite(3, LOW);
}
if(input == 'E'){ //STOP ---
digitalWrite(6, LOW);
digitalWrite(5, LOW);
digitalWrite(4, LOW);
digitalWrite(3, LOW);
}
}
}尝试了几天,我没有太多的编程技巧。
因为hc05正在使用"COM4“或"COM5”来接收
HC-05 Arduino UNO
----- -----------
RX --> Pin 11
TX --> Pin 10
+5v --> +5v
GND --> GND 发布于 2019-12-02 16:56:01
我不确定同时使用socket和pyserial想要达到什么目的。据我所知,蓝牙套接字甚至在Windows下都不可用,而且从外观上看,您的Python是在没有蓝牙支持的情况下构建的。
将您的Windows10 PC与HC05模块配对。然后在Windows蓝牙设置中设置传出COM-Port。(蓝牙和其他设备->更多蓝牙选项)
然后使用pyserial发送数据。
我建议你一步一步地按照Arduino蓝牙教程在Windows10上操作。
https://stackoverflow.com/questions/59130775
复制相似问题