簡體   English   中英

從 arduino 收到的藍牙數據在 python 中被分成塊

[英]bluetooth data recieved from arduino gets divided into chunks in python

我試圖使用 HC-05 將來自 arduino pro micro 的 MPU6050 的 IMU 數據發送到我的電腦,我在 python 中編寫了一個小程序來接收它,但是數據被打印在不同的行中或被分成塊。

這是 Arduino 代碼,我使用 Serial1 線通過 HC-05 發送數據。

Serial1.print(ypr[0]*180/M_PI);
Serial1.print(",");
Serial1.print(ypr[1]*180/M_PI);
Serial1.print(",");
Serial1.println(ypr[2]*180/M_PI);

這是接收代碼,在 python 中:

import bluetooth 
import serial
target_name = "HC-05"
target_address = None
        
nearby_devices = bluetooth.discover_devices()
        
for bdaddr in nearby_devices:
    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)
    else:
        print ("could not find target bluetooth device nearby")

port = 1
sock=bluetooth.BluetoothSocket(bluetooth.RFCOMM)
size = 512
sock.connect((target_address, port))
print("CONNECTED")

    while True:
        data = sock.recv(size)
        data=data.decode('ascii')
        # data=data.strip()
        # imu=data.split(',')
        print(data)

數據在 python 中打印如下: python 以塊和無序打印數據

但我希望它是這樣的,如 Serial Monitor of arduino 偏航俯仰和滾動值順序

我建議將值作為字節發送。 看起來您需要為 imu 的每個 x、y 和 z 值分配兩個字節。 您可以通過乘以一個值以刪除小數位,將 imu 中的值轉換為 integer。 例如 12.34 * 100 是 1234。

在藍牙鏈接的另一端,您可以解壓縮字節並除以相同的數字以取回原始值。

示例 Arduino 代碼:

#include <SoftwareSerial.h>

SoftwareSerial hc06(3,2);

struct imu
{
  int16_t x;
  int16_t y;
  int16_t z;
};

imu state;

void setup(){
  //Initialize Serial Monitor
  Serial.begin(9600);
  //Initialize Bluetooth Serial Port
  hc06.begin(9600);
  Serial.println("setup done...");
  state.x = 12.34 * 100;
  state.y = 37.00 * 100;
  state.z = -10.99 * 100;
}

void loop(){
  // Write data from HC06 to Serial Monitor
    byte buf[6];
    buf[0] = state.x & 255;
    buf[1] = (state.x >> 8) & 255;
    buf[2] = state.y & 255;
    buf[3] = (state.y >> 8) & 255;
    buf[4] = state.z & 255;
    buf[5] = (state.z >> 8) & 255;
    hc06.write(buf, sizeof(buf));
    delay(2000);
}

和示例 Python 代碼:

"""
A simple Python script to receive bytes over Bluetooth using
Python sockets (with Python 3.3 or above).
"""

import socket
import struct

serverMACAddress = '00:00:12:06:53:92'
port = 1
with socket.socket(socket.AF_BLUETOOTH, socket.SOCK_STREAM, socket.BTPROTO_RFCOMM) as s:
    s.connect((serverMACAddress, port))
    data = b''
    while True:
        while len(data) < 6:
            data += s.recv(1)
        x, y, z = struct.unpack('<hhh', data[:6])
        print(f"raw data={data} : x={x/100}, y={y/100}, z={z/100}")
        data = data[6:]

其中給出了 output:

raw data=b'\xd2\x04t\x0e\xb5\xfb' : x=12.34, y=37.0, z=-10.99
raw data=b'\xd2\x04t\x0e\xb5\xfb' : x=12.34, y=37.0, z=-10.99
Serial1.print(ypr[0]*180/M_PI,",",ypr[1]*180/M_PI,",",ypr[2]*180/M_PI)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM