![](/img/trans.png)
[英]Send one byte of data to the hc 05 arduino bluetooth module using python on a windows 10 computer with jupyter notebook
[英]Bluetooth with Python and Arduino (HC-06)
我正在嘗試從Arduino向Python發送信息。 我發送的信息是16個字符的字符串,問題是我沒有一次接收到所有數據。
Arduino代碼:
void setup(){
BTserial.begin(9600);
}
void loop() {
BTserial.print("0123456789012345");
delay(500);
}
Python代碼:
import bluetooth
sock = bluetooth.BluetoothSocket(bluetooth.RFCOMM)
sock.connect(("00:13:04:83:EC:45", 1))
while(True):
print(sock.recv(10))
輸出的結果是:
b'0'
b'123456789'
b'012345'
我想要的輸出是:
b'0123456789012345'
要么
0123456789012345
感謝您的回答。
我有臨時的解決方法,但是如果有其他方法,請告訴我
string = ""
final = ""
num = 0
while(True):
string = sock.recv(4096)
if str(string) == "b'$'":
num=1
elif num == 1:
final+=str(string)
num = 2
elif num == 2:
final += str(string)
final = final.replace(("b"), "")
final = final.replace(("'"), "")
print(final)
final = ""
num == 0
在arduino輸出序列上附加一個消息定界符; 您的選擇,但'\\ n'會很好。 創建一個python生成器函數,該函數從藍牙流中讀取輸入字符,並將它們組裝到緩沖區中。 收到消息定界符后,將產生整個緩沖區,因此調用生成器的函數將阻塞直到整個消息可用為止。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.