How to connect matlab with Bluetooth HC 05

60 Ansichten (letzte 30 Tage)
wildan
wildan am 16 Mär. 2012
Bearbeitet: Walter Roberson am 24 Jun. 2022
Hi, iam wildan. im a mechatronics student. wanti to know about how to connect Matlab (simulink) with bluetooth device in microcontroller. So, i can control microcontroller use matlab wireless. the bluetooth model is HC 05. thanks for your answer.
  2 Kommentare
Alexander Slovak
Alexander Slovak am 6 Apr. 2012
I'm trying to do this for two months!!!
But get only errors:
Error evaluating registered method 'Start' of MATLAB S-Function 'sserialcb' in 'bttest/Serial Configuration'.
Open failed: Port: COM4 is not available. Available ports: COM4.
Use INSTRFIND to determine if other instrument objects are connected to the requested device.
Model with one block 'Serial Configuration' connect correctly, but when I add serial send or serial recieve block - I get an error again.
Adel Djellal
Adel Djellal am 20 Apr. 2017
hello
I think you used inadequate 0 you must configure as follows:
>> B = Bluetooth('HC-05', 1);
>> fopen(B);

Melden Sie sich an, um zu kommentieren.

Antworten (4)

Lu
Lu am 15 Nov. 2012
Hi guys,
Here's the solution:
HC-05 is actually a bluetooth SPP device, so we cannot use traditional serial function in matlab, because its only support "real" COM port.
However, matlab give us a similar lib named "bluetooth" :
You can apply ccde as below to access the HC-05:
1. first make sure you havn't change the device name of you HC-05, default is "HC-05", you can check it at your bluetooth Properities menu at Control Panel.
2. Code
%creat a bluetooth object
%HC-05 channel default is 1
b = Bluetooth('HC-05',1);
fopen(b);
%write and read function
fwrite(b,Bluetooth_Write,'uchar');
Bluetooth_Read=fgets(b);
%close and clear
fclose(b);
clear(b);
3. try and enjoy
Best,
  5 Kommentare
George Hoo
George Hoo am 16 Mär. 2018
I meet the same question,have this problem been solved?
puneet yadav
puneet yadav am 24 Apr. 2018
Is the problem solved?

Melden Sie sich an, um zu kommentieren.


H W
H W am 23 Jun. 2022
Bearbeitet: Walter Roberson am 24 Jun. 2022
Communicate with HC-06 over Bluetooth
This example shows how to communicate with a HC-06 Bluetooth® module using Serial Port Profile.
Hardware Requirements
Windows® 10 or macOS machine with built-in or external Bluetooth 2.0 compatible adapter
HC-06 Bluetooth module
5 V source, such as an FTDI converter or Arduino® board
A few wires
Hardware Setup
This example uses a HC-06 Bluetooth transceiver module configured as a loopback device. On the HC-06 module:
Connect the VCC pin to the 5 V source.
Connect the GND pin to the ground.
Connect the TXD pin to the RXD pin.
A blinking LED on the HC-06 module indicates that the device is powered. Next, pair the device on the host computer to allow connection in MATLAB®. The default PIN code is 1234 unless configured otherwise.
Get Device Information
Identify the Bluetooth device address or name and SPP channel number by calling the bluetoothlist function. Make sure the device status is "Ready to connect", indicating that it is powered on and paired. If you already know the device information, you can skip this step.
bluetoothlist
Connect to Device
Use the device address or name and SPP channel number to connect to the device by calling bluetooth. Specify the device name if it is unique or specify the device address.
hc06 = bluetooth("HC-08", 1)
Write and Read Data
After you connect the HC-06 to MATLAB, write byte data or string data to the device by calling write.
write(hc06, 1:10);
write(hc06, "helloworld");
Since the device is configured as a loopback, the data received on the device is immediately returned to MATLAB. Read the data by calling read.
read(hc06, 10)
read(hc06, 10, "string")
For information on sending and receiving terminated string data, see readline and writeline.
Collect Data Asynchronously
You can use a callback function to read data as it is written to the Bluetooth device. Call configureCallback to read and collect data each time five bytes are sent by the device and received in MATLAB.
function collectData(src, evt)
% Read received data and store it in the UserData property on the bluetooth object
src.UserData = [src.UserData; read(src,src.BytesAvailableFcnCount)];
end
configureCallback(hc06, "byte", 5, @collectData);
Write data to the device. The data is immediately sent back to MATLAB, triggering the callback function to read and display the data.
write(hc06, 1:20);
% Wait for all callbacks to be processed
pause(1);
disp(hc06.UserData);
Disconnect from Device
Clear the device object when you are finished working with it.
clear hc06
Copyright 2020 The MathWorks, Inc.

Hung BV
Hung BV am 28 Okt. 2013
Hi guys,I'm trying to do this use lib "bluetooth" but it not work I used command instrhwinfo('bluetooth') to check the bluetooth device in my laptop, and it return the resuls the name is 'HC-05' so i sure that the device name of my Blutooth device is 'HC-05'. Then i use the command: b = Bluetooth('HC-05',1); fopen(b);
There is an error, it say that the device already in use, but i sure that no device is connected to my HC05
Anyone can help me!
Thanks for your answer!
  2 Kommentare
Jose andres Ardilla
Jose andres Ardilla am 21 Okt. 2016
cierre el programa de arduino y mire que este bien escrito el nombre del bluetooh como aparece en (instrhwinfo('Bluetooth','HC-05'))
espero te sirva
Borja Roca Romero
Borja Roca Romero am 5 Apr. 2018
Hi, I tried this but I still have the same issue as Hung BV, fopen(b) does not work for me. I'm sure nobody else nor my applications r using the device.

Melden Sie sich an, um zu kommentieren.


liang kai-min
liang kai-min am 15 Mai 2017
My Bluetooth device type is HC-05. If I use bluetooth function, it is necessary to set the baudrate?

Community Treasure Hunt

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

Start Hunting!

Translated by