How to connect to the bluetooth Serial Port?

4 次查看(过去 30 天)
I am trying to use the SSP as a COM port to receive data from MCU. I have to try many times to connect to the COM port, but it often failed to open.The version is R2013a and the codes are as below:
s=serial('COM4');
fopen(s);
Sometimes it would work,but usually failed and get errors:
Open failed: Port: COM4 is not available. Available ports: COM4, COM5. Use INSTRFIND to determine if other instrument objects are connected to the requested device.
Sometime I get this:
Open failed: Cannot connect to the COM4 port. Possible reasons are another application is connected to the port or the port does not exist.
I use instrhwinfo('serial') to check whether the COM4 is available, and I could find the COM4 is in AvailableSerialPorts list.Even when the COM4 is not in AvailableSerialPorts list, sometimes I can still connect. I was so confused.
Here describe a similar problem, but I can't get any data using data=fgets(b);
What's more I need to configure the COM ports like BaudRate,DataBits,StopBits.....But I can't find these properties in bluetooth!

回答(1 个)

Mark O'Sullivan
Mark O'Sullivan 2016-12-6
This may be because you're not closing the serial connection after using it. After you open a serial connection and read from it, you need to close it before you can open and read from it again.
So something like this should work every time:
s=serial('COM4');
fopen(s);
fgets(s);
fclose(s);
If you have an error and your script stops before reaching the fclose line, then you can use the following 2 commands to clear the open serial ports.
newobjs = instrfind;
fclose(newobjs)
That should fix that problem. :)

类别

Help CenterFile Exchange 中查找有关 Source Control 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by