Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

To resolve issues starting MATLAB on Mac OS X 10.10 (Yosemite) visit: http://www.mathworks.com/matlabcentral/answers/159016

Serial port is not available in GUI but it actually available in my pc

Asked by Yeoh on 15 Jun 2011

Hi,

I wish to have simple GUI communicate serially with microcontroller (PIC) through UART. When press button1 in GUI, LED1 will light up. Press button2, LED2 will light up. I use USB to RS232 converter. In my computer management, under the port category, it shows USB-to-RS232 converter port (COM14). So I use COM14 in my GUI. Unfortunately, it shows an error when I run them. "??? Error using ==> serial.fopen at 72 Port: COM14 is not available. Available ports: COM5, COM12." Why COM14 is not available? But it shows in my computer management. when I use instrfind command, the following information I get:

>> instrfind

   Instrument Object Array
   Index:    Type:     Status:   Name:  
   1         serial    open      Serial-COM14
   2         serial    closed    Serial-COM14
   3         serial    closed    Serial-COM14
   4         serial    closed    Serial-COM14

What does it mean? why COM14 is open then closed? Thank you.

0 Comments

Yeoh

Products

No products are associated with this question.

3 Answers

Answer by Walter Roberson on 15 Jun 2011

Did you turn on the device after you started MATLAB? USB devices have to be fully connected before you start MATLAB or else you will not be able to use them.

If the problem continues, try

fclose(instrfind)

and then try to find the port.

2 Comments

Yeoh on 15 Jun 2011

erm... ya, I did on everything then only run my GUI. I also try to off all my matlab and start again after I on my system. But the thing is still the same. I try fclose(instrfind) but no use because the problem is at fopen. my programming is as shown:

function pushbutton3_Callback(hObject, eventdata, handles)
clear all;
s = serial('COM1');
set(s,'BaudRate',9600);
fopen(s);
fprintf(s,'%s','g');
fclose(instrfind)
delete(s);
clear s;

I just change from com port 14 to com port 1 in my computer management, so I change for matlab as well. But the problem still there, the error is as shown:

??? Error using ==> serial.fopen at 72
Port: COM1 is not available. Available ports: COM5, COM12.
Use INSTRFIND to determine if other instrument objects are
connected to the requested device.

Error in ==> test1>pushbutton3_Callback at 105
fopen(s);

Error in ==> gui_mainfcn at 96
feval(varargin{:});

Error in ==> test1 at 42
gui_mainfcn(gui_State, varargin{:});

Error in ==>
@(hObject,eventdata)test1('pushbutton3_Callback',hObject,eventdata,guidata(hObject))


??? Error while evaluating uicontrol Callback

now is COM1 not available.

Walter Roberson on 15 Jun 2011

Just before the fopen(s), try adding

instrfind

You should expect to see one occurrence of COM1 (corresponding to its use in s) and anything else is Odd.

Walter Roberson
Answer by Ankit Desai on 15 Jun 2011

There are a few things that you might want to check:

  1. Make sure the port is not used in any other application (such as hyperterminal, another instance of MATLAB etc.). If the port is used by any such application, it will not show up in MATLAB.
  2. If an object is created in MATLAB but not deleted and cleared correctly - it will not show up in subsequent attempt of using it.

From the output of instrfind, it appears that a serial object for COM14 was created but not cleaned up correctly (it is still available in the memory, which is why instrfind is finding it). Which means as per point 2 above, it will not be available when you try to create another object using the same port.

You might want to check the code to find all the places where you create a serial object and make sure that you are not trying to create multiple objects to the same port. You should use the same object for all your serial communication to that port.

0 Comments

Ankit Desai
Answer by Yeoh on 16 Jun 2011

[EDIT: 20110616 08:57 CDT - reformat - WDR]

After collected all the opinions, I successfully change my coding and the program can run. The code is as shown:

function pushbutton3_Callback(hObject, eventdata, handles)
delete(instrfind);
pause(0.1);
s = serial('COM1');
set(s,'BaudRate',9600,'DataBits', 8, 'Parity', 'none','StopBits', 1, 'FlowControl', 'none','Terminator','CR/LF');
fopen(s);
fprintf(s,'g');
fclose(s);
delete(s);
clear s;

Thank you. But it is quite slow, around 3 minutes to react. Does any idea to make it faster? I already change the baud rate but it is still the same. Thank you.

2 Comments

amey patil on 22 Nov 2011

How to make use of COM port in serial communication ??
Do we have to declare it in matlab by saying that 'COM1' should be used for matlab??
I want to do this serial communication on COM1 and end device for communication is FPGA kit VIRTEX 5 ; can I do this???
IF yes then HOW??????

Kaustubha Govind on 23 Jan 2012

See http://www.mathworks.com/help/releases/R2011b/techdoc/matlab_external/f38496.html

Yeoh

Contact us