Read BLE and handle data in microsec level with MATLAB appdesigner

13 visualizaciones (últimos 30 días)
Jin Kee Kim
Jin Kee Kim el 14 de Mzo. de 2023
Respondida: Rasmita el 12 de Abr. de 2023
I want to read data from BLE(function in MATLAB) and handle the data.
BLE data is always 240 bytes(uint8), and the period is 1 ~ 6 msec level.
Every functions are defined and used in appdesigner GUI.
My problem is,
BLE data will store in memory ring buffer,(1 x 1,000,000)
and My data handler function will read data from buffer, and split by packets with head/tail identifier.
Data handler function is the callback function of timer which period is 1 msec.
I'm understanding the MATLAB's callback queue line is single.
When data handler execution time is longer than BLE data period,
the speed at which data is accumulated is faster than the speed at which data handled.
Is there any possible way to control two callback function in MATLAB?
or split the different size of packets with head/tail in (1 x 1,000,000) in very short time? (like ~ 100 microsec.)
BLE data example below :
BLE_DATA = [ head, data(124), tail, head, data(1), tail, head, data(,,,), tail ... ] %% size will (1 x 240),
%% but the head or tail will not always on the first / last location of array.
%% packet = [ head, data(124), tail] / [head, data(1), tail] ....
Every arrays in my codes are preallocated,
and the time I measured with "timeit", "tic / toc" during handle the 240 bytes at handler function is 400 ~ 600 us, including refreshing plot.
I tried to store BLE data in external file(csv, txt, xls), but the time for save / load the file is much longer than memory buffer.
Maybe my code was inefficient.
The data in packets should be save in external file, but this is also time-hunger process in MATLAB. (msec level)
I'm using MATLAB 2020b.
Any ideas or advice are welcome.
Thank you.

Respuestas (1)

Rasmita
Rasmita el 12 de Abr. de 2023
Hi,
It is my understanding that, you are trying to read data from ‘BLE’ function in MATLAB and handle the data. Also, you want to split the different size of 'BLE' packets with head/tail and store the data packets in an external file in very short time.
To handle the BLE data in MATLAB, you can use the ble object available in MATLAB. The ble object provides the capability to scan and discover nearby BLE devices, connect and disconnect from devices, and read and write data to devices. You can use the read method of the 'ble' object to read data from the device.
To handle the data received from the BLE device, you can use a timer object in MATLAB. You can set the timer object to fire every 1 millisecond and call the data handler function. In the data handler function, you can read the data from the ring buffer and split it into packets using the head/tail identifier.
To handle the case where the data handler function takes longer than the 'BLE' data period, you can use a separate thread to handle the 'BLE' data. MATLAB supports parallel computing using the Parallel Computing Toolbox. You can use the parfeval function to execute the data handler function in a separate thread. The parfeval function returns a Future object, which you can use to check the status of the execution and get the results.
To split the packets in the ring buffer quickly, you can use the strfind function to search for the head/tail identifier in the ring buffer. The strfind function returns the indices of the head/tail identifier in the buffer, which you can use to split the buffer into packets.
For more information, please refer to the below documentation links:
Hope this helps!
Regards,
Rasmita

Categorías

Más información sobre Startup and Shutdown en Help Center y File Exchange.

Productos


Versión

R2020b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by