Serial Port opening problem
14 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Abdel Rahman Bekawi
il 4 Nov 2019
Commentato: Abdel Rahman Bekawi
il 5 Nov 2019
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 Commenti
Risposta accettata
Murugan C
il 5 Nov 2019
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
Più risposte (0)
Vedere anche
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!