Serial Port opening problem

조회 수: 17 (최근 30일)
Abdel Rahman Bekawi
Abdel Rahman Bekawi 2019년 11월 4일
댓글: Abdel Rahman Bekawi 2019년 11월 5일
I'm trying to acquire values from a microcontroller using the serial port. However, I'm facing a strange bug!
when I try to run the program for the first time it works and when I try to run it again it gives me an error saying that cannot open the port! But if I close matlab and open it again and run the program it works for the first time only and then same problem arises!
Can anyone help me please ?
Thanks in advance
  댓글 수: 5
Abdel Rahman Bekawi
Abdel Rahman Bekawi 2019년 11월 4일
I will try then to remove try and catch and close after my code is done and see!
thanks a lot for your help
Abdel Rahman Bekawi
Abdel Rahman Bekawi 2019년 11월 5일
Here is the code from the tutorial I'm following in this link https://os.mbed.com/cookbook/Interfacing-with-Matlab
function accell()
TIMEOUT = 5; %time to wait for data before aborting
XPOINTS = 50; %number of points along x axis
try
%create serial object to represent connection to mbed
mbed = serial('COM4', ...
'BaudRate', 9600, ...
'Parity', 'none', ...
'DataBits', 8, ...
'StopBits', 1); %change depending on mbed configuration
set(mbed,'Timeout',TIMEOUT); %adjust timeout to ensure fast response when mbed disconnected
fopen(mbed); %open serial connection
position = 1; %initialise graph variables
time = 1;
x = [(1:XPOINTS)' (1:XPOINTS)' (1:XPOINTS)'];
xlabels = (1:XPOINTS);
y = zeros(XPOINTS,3);
% in my CASE I'm NOT having the CONDITION to be alwase TRUE as in here
while (1) %loop forever (or until an error occurs)
values = fscanf(mbed, '%f,%f,%f'); %get values into vector
%assumes data formatted as
%'1,2,3'
y(position,:) = values'; %put into y to be displayed
%update position on x-axis and x-axis labels
xlabels(position) = time;
time = time + 1;
if (position < XPOINTS)
position = position + 1;
else
position = 1;
end
%display
plot(x,y);
set(gca, 'XTick', 1:XPOINTS);
set(gca, 'XTickLabel', xlabels);
drawnow; %this is required to force the display to update before the function terminates
end
fclose(mbed); %close connection (this should never be reached when using while(1), but included for completeness)
catch
%in case of error or mbed being disconnected
disp('Failed!');
fclose(mbed); %close connection to prevent COM port being lokced open
end

댓글을 달려면 로그인하십시오.

채택된 답변

Murugan C
Murugan C 2019년 11월 5일
Hi
you should check, how many ports are open and closed using instrfind syntax in matlab.
>> instrfind
Instrument Object Array
Index: Type: Status: Name:
1 serial closed Serial-COM4
2 serial closed Serial-COM4
3 serial closed Serial-COM1
4 serial closed Serial-COM1
5 serial closed Serial-COM1
6 serial closed Serial-COM1
7 serial open Serial-COM1
now, easily we can close all ports using fclose. and should delete that object.
fclose(instrfind)
delete(mbed)
correct your as like below..
try
mbed = serial('COM1', ...
'BaudRate', 9600, ...
'Parity', 'none', ...
'DataBits', 8, ...
'StopBits', 1); %change depending on mbed configuration
.......
catch
%in case of error or mbed being disconnected
disp('Failed!');
fclose(mbed);
fclose(instrfind);
delete(mbed);
end
  댓글 수: 1
Abdel Rahman Bekawi
Abdel Rahman Bekawi 2019년 11월 5일
Thanks Murugan and everyone for the help it worked out!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Serial and USB Communication에 대해 자세히 알아보기

제품


릴리스

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by