0

I'm just trying to know how to established serial communication between Arduino and MATLAB. From this tutorial I copied the code, but as for my PC use "Windows" I had to change my serial port name. My arduino is connected to 'COM4' port . enter image description here

But When I run the code , error poped saying that : " Open failed: Port: COM4 is not available. No ports are available." I do not understand what this mean and how am I suppose to solve the problem. I have found nothing helpful in internet . Can you please help me out ?

enter image description here

my code is given below :

function [obj,flag]=setupSerial(comPort)

flag =1;
obj=serial(comPort);
set(obj,'DataBits',8);
set(obj,'StopBits',1);
set(obj,'BaudRate',9600);
set(obj,'Parity','none');
fopen(obj);
a='b';
while (a~='a')
   a=fread(obj,1,'uchar');
end
if(a=='a')
   disp('Serial Setup Complete');
end
  fprintf(obj,'%c','a');
  mbox = msgbox('Serial Communication setup'); uiwait(mbox);
  fscanf(obj,'%u');
  fclose(obj);
end

Thanks in advance .

3
  • You might not have closed it before. I would suggest making the serial object global so you can close it even if you lose the local handle.
    – b degnan
    Apr 18, 2016 at 19:37
  • Probably a better fit for SuperUser since this is a PC hardware/software question. Apr 18, 2016 at 20:18
  • I closed it , even I add this new line at the end to close it , still same problem .
    – Anklon
    Apr 19, 2016 at 11:58

1 Answer 1

0

Your port may be used or open for another channel . Insert this line before creating the obj:

delete(instrfind({'port'},{comPort}));

This line will delete any previous setup of the comPort .

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .