How to keep a bluetooth object open in between multiple runs of the same program?
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
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 comentario
dafniz91
el 14 de Mayo de 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?
Respuestas (2)
Vinod
el 28 de Mayo de 2015
Editada: Vinod
el 28 de Mayo de 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.
0 comentarios
Naveen
el 28 de Mayo de 2015
3 comentarios
Vinod
el 28 de Mayo de 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
Ver también
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!