MATLAB data recording from bluetooth device malfunctions after about 10 minutes. How do I fix this?

12 visualizaciones (últimos 30 días)
I have a bluetooth Arduino device that sends experimental data to a MATLAB program I have written. I have one script that establishes the connection to the bluetooth by creating a MATLAB bluetooth object, and then opens the object (Script 1 below). I only have to run this script once, and then I run my data recording script (Script 2 below) multiple times (to record multiple different trials of my experiment). The data recording script does not create, open, close, or delete the bluetooth object. I have other things in Script 2 to clean up the data and write it to a txt file, but below is essentially all the bluetooth is doing.
Script 1
b = Bluetooth('HC-05',1);
fopen(b);
Script 2
angle = zeros(1,100)
for i = 1:100
angle(i) = str2double(fscanf(b));
end
So everything runs smoothly and I am able to easily record data from multiple trials of my experiment without any downtime in between each run. However, randomly after about 10-15 minutes, the bluetooth will stop working. I'll run the data recording script and it will start up fine, but when it starts to record data, it will record random numbers that don't actually represent the data for a few seconds, and then it will say: "Warning: Unsuccessful read: A timeout occurred before the Terminator was reached." And then for the rest of that trial, the data will be "NaN"
This is what I've discovered fixes the problem: Clear all MATLAB variables and run "delete(instrfindall)". Close MATLAB. Go into control panel and "Remove devices" to manually disconnect the HC-05 bluetooth device. Disconnect and reconnect the bluetooth dongle on my desktop computer. Press the reset button on my Arduino. Go to control panel and "Add devices" to reconnect the HC-05 bluetooth device. Reopen MATLAB. Run Script 1 to connect to the bluetooth and create bluetooth object. Run Script 2. Everything works fine now (for another 10-15 minutes again). I don't know if all of these steps are required to fix the problem, but a few times that I've forgotten one of the steps above, the problem persisted so now to be safe I do all of the above.
So to clarify, after about 10 minutes, MATLAB can no longer reliably retrieve data from the Bluetooth. HOWEVER, the Bluetooth IS still connected to my computer. So although my current fix around of reconnected the Bluetooth device to my computer includes steps outside of MATLAB, I feel like the problem might still be MATLAB related where the Bluetooth device is getting confused by the way MATLAB is handling the bluetooth object (maybe there's a problem that my Script 2 never does fclose(b)?)
I'd appreciate any feedback on this because it is getting annoying and adding unnecessary time to my experiments.
  1 comentario
Naveen
Naveen el 5 de Ag. de 2015
Also, my Bluetooth device can be connected to my computer indefinitely before I start recording data in MATLAB with no issues. It's once MATLAB begins the data recording that this issue/time limit comes up.

Iniciar sesión para comentar.

Respuestas (1)

Vinod
Vinod el 8 de Ag. de 2015
I suspect this is because your Bluetooth object's buffer is overflowing. What is the InputBufferSize of your Bluetooth object? Can you try modifying it to a larger number and post back if that resolves the issue?

Community Treasure Hunt

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

Start Hunting!

Translated by