How to keep a bluetooth object open in between multiple runs of the same program?

1 view (last 30 days)
I am using MATLAB to record data from a bluetooth/arduino/accelerometer device. The way my experiment is set up, it is easiest to just run my MATLAB program multiple times for consecutive datasets. For example, I press run, the data immediately begins recording, it ends when specified. Then I change some physical parameters of my physical experimental set-up, press run on MATLAB when I'm ready, and ideally it will begin recording again.
I pretty much have my code worked out to do exactly what I want it to EXCEPT the fact that each time I run the program, it clears the bluetooth object at the beginning. As a result, it takes about 10 seconds or so to reconnect to the bluetooth module and recreate the MATLAB bluetooth object. Because of this, data recording does not begin when I want it to.
Here's a simplified outline of my code (again, this works perfectly OTHER than the lag in between multiples runs):
clear all
delete(instrfindall);
b = Bluetooth('HC-06',1);
fopen(b);
array = zeros (1,100);
for i = 1:100
array(i) = str2double (fscanf(b));
end
Again, this outline is very simplified. There is a lot of other stuff that I actually do need to clear at the beginning. I have tried modifying the code to explicitly clear everything OTHER than the bluetooth object b. It still doesn't work. I think in that case, the problem is that I'm not clearing the bluetooth object on the second run, but still telling it to create/open the object again. But when I try to exclude lines 3 and 4 after the first run, I get errors. Something about an invalid object.
Does anyone have any advise for how I can keep my bluetooth device connected to MATLAB as a bluetooth object so that I don't have to wait for it to connect each time I run the program?
  1 Comment
dafniz91
dafniz91 on 14 May 2016
hello, how did you connect it? I´m trying to connect my HC06 module with Arduino for a while and I haven´t succeed..what version of MatLab are you using?

Sign in to comment.

Answers (2)

Vinod
Vinod on 28 May 2015
Edited: Vinod on 28 May 2015
The first two lines of your script:
clear all
delete(instrfindall);
are what is clearing all variables, objects and resetting all bluetooth connections. Remove these lines if you don't want to start from a clean slate every time you run your script.

Naveen
Naveen on 28 May 2015
I don't know why, but delete(instrfindall) is necessary for the code to work. I based my code off of something similar that someone else had written and that's where I got the idea to use delete(instrfindall).
When I remove the delete (instrfindall) command, I get the following error:
Error using icinterface/fopen (line 83)
Unsuccessful open: Cannot connect to the device. Possible reasons are another application is connected
or the device is not available.
Error in btcallibration (line 4)
fopen(b);
  3 Comments
Vinod
Vinod on 28 May 2015
Your script doesn't seem to be cleaning up, which is why you needed the delete(instrfindall).
In general it is a good practice to have Close, Delete and Clear the object after you are done with it. Consider incorporating this line do you no longer need to be deleting all open connections with delete(instrfindall).
fclose(b); delete(b); clear b

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!