Python Forum

Full Version: Read buffer from bluetooth
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi,
I try to use pybluez to read data from Arduino/bluetooth to Python on Win10. I have now working code for reading via USB and it works well. But I have no idea how to do it with BT. Now I can connect BT device and read some data but not exactly what I need.

OLD working code for serial:
import serial
import struct
SAMPLES= 30000
v_data = []
s = struct.Struct('<' + str(SAMPLES) + 'f')
ser = serial.Serial(port=serio, baudrate=2000000)
ser.reset_input_buffer()
ser.write("r".encode())
serial_data = ser.read(SAMPLES*4)
unpacked_data = s.unpack(serial_data)
v_data[0:SAMPLES] = unpacked_data[0:SAMPLES]
NOW I try this:

import bluetooth
import serial
import struct
v_data = []
ser = ""
s = struct.Struct('<' + str(10) + 'f')
SAMPLES = 30000
bd_addr = "FC:A8:9A:00:22:33" #itade address
port = 1
sock=bluetooth.BluetoothSocket( bluetooth.RFCOMM )
sock.connect((bd_addr, port))
print('Connected')
#sock.settimeout(1.0)

sock.send("r")
print('Sent data')

while True:
    v_data = sock.recv(100)
    if len(v_data) == 0: break
    print("received [%s]" % v_data)
print(v_data)
I have this code in Teensy4.0:

#include <SoftwareSerial.h>
SoftwareSerial mySerial(0, 1); // RX, TX
char inChar;
void setup()  
{
  Serial.begin(38400);
  Serial.println("I am ready to send some stuff!");
  mySerial.begin(38400);
}
void loop() // run over and over
{
    if (Serial.available() > 0) {
    inChar = Serial.read();
    for(int i=0; i<100; i++) {
      byte *b = (byte *)&i;
      mySerial.write(b[0]);
      mySerial.write(b[1]);
      mySerial.write(b[2]);
      mySerial.write(b[3]);
    }
  }
}
But data seems like nonsense and it is very slow. With Serial I can read 30k values in 0.2s.
Is there any way how to proceed?
Thanks!


EDIT:
Main question is: Is possible send array of 30000 floats from Teensy/Arduino to Python via bluetooth in very short time? And how to receive it in Python?
Quote:Bluetooth Classic is designed for continuous two-way data transfer with high Application throughput (up to 2.1 Mbps); highly effective, but only for short distances. So, it's a perfect solution in the case of streaming audio and video, or mice and other devices that need a continuous, broadband link. 07.04.2020

A float has 4 Bytes.
If you send 30_000 floats, then it's in size ~ 118 KiB big.
struct.calcsize("<f") * 30_000 / 1024
This will take minimum 54.5 ms.
(struct.calcsize("<f") * 30_000) / (2.1 * 1024 ** 2) * 1000
Thank you.

But how to read the dada correctly?
Now I have "while True:" and it is very slow for reading.
And values I see are:
b'\xc7\xb9\x8c\x10'
b'\xf4'
b'\x8c\xa80\x18'
b'\xe4\xf6\x9a\x84'
b'\x8a\x14\xef\x06'