How to keep a bluetooth object open in between multiple runs of the same program?
1 次查看(过去 30 天)
显示 更早的评论
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 个评论
dafniz91
2016-5-14
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?
回答(2 个)
Naveen
2015-5-28
3 个评论
Vinod
2015-5-28
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
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!