フィルターのクリア

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

1 回表示 (過去 30 日間)
Naveen
Naveen 2015 年 5 月 27 日
コメント済み: dafniz91 2016 年 5 月 14 日
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
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 件)

Vinod
Vinod 2015 年 5 月 28 日
編集済み: Vinod 2015 年 5 月 28 日
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 2015 年 5 月 28 日
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 件のコメント
Naveen
Naveen 2015 年 5 月 28 日
Thank you, this helped!
Vinod
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

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeFile Operations についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by