Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Read buffer from bluetooth
#1
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?
Reply
#2
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
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#3
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'
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Read data via bluetooth frohr 9 3,419 Jul-10-2022, 09:51 AM
Last Post: frohr
  Connect to HC-05 Bluetooth (NOT BLE) korenron 0 1,476 Jun-26-2022, 09:06 AM
Last Post: korenron
  Scan for Bluetooth device korenron 0 2,629 Jan-10-2022, 01:06 PM
Last Post: korenron
  Sending string commands from Python to a bluetooth device Rovelin 13 9,525 Aug-31-2021, 06:40 PM
Last Post: deanhystad
  ModuleNotFoundError: No module named 'bluetooth' Error Rovelin 4 12,203 Aug-31-2021, 04:04 PM
Last Post: Rovelin
  Bluetooth send message after connecting? korenron 2 2,679 Apr-26-2021, 05:50 AM
Last Post: korenron
  JS Buffer.from VS struct.pack DreamingInsanity 3 2,472 Apr-05-2021, 06:27 PM
Last Post: DreamingInsanity
  Seperate output buffer matt_the_hall 2 2,369 Mar-15-2021, 08:44 PM
Last Post: matt_the_hall
  Bluetooth problem Lad 0 1,904 Sep-24-2020, 07:26 PM
Last Post: Lad
  PyAudio Buffer Data Calculation MclarenF1 0 2,141 Aug-21-2020, 10:55 AM
Last Post: MclarenF1

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020