Can not connect to serial port my MATLAB-arduino comunication

1 visualización (últimos 30 días)
Vladyslav Lenko
Vladyslav Lenko el 22 de Mayo de 2020
Hi, i'm doing a project:
------------------------ ''Only the serial communication zone needs to be solved ''
% --- Executes on button press in iniciar.
function iniciar_Callback(hObject, eventdata, handles)
% hObject handle to iniciar (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% NOOOOOOOO close all;
clc;
y=zeros(1,1000); %Vector on es guardaran les dades
pressio = 0;
pressio_ini = pressio_inicial(); %pressio inicial a la superficie
%%%% provaaaaaa pressio_ini = 200;
%Mostrem la pressió inicial ACTUAL a la superficie terrestre
fprintf('Pressió inicial actual a la superfície terrestre: %.2fkPa\n', pressio_ini);
%Inicialització port sèrie
delete(instrfind({'Port'},{'COM7'}));
serialPort = serial('COM7');
serialPort.BaudRate=9600;
warning('off','MATLAB:serial:fscanf:unsuccessfulRead');
%Creació i obertura arxiu resultats.txt
fid=fopen('resultats.txt','w');
%Obrim el port
fopen(serialPort);
%Enviem S a Arduino
fprintf(serialPort,'%s\n','S');
%pause(1);
%fprintf(serialPort,'%s\n','o');
%Declaram un comptador del nombre de mostres ja preses
i=1;
%Crem finestra per a la gràfica
%%%% NOOOOOOOOOOOOO figure('Name','Serial communication: Matlab + Arduino.')
title('SERIAL COMMUNICATION MATLAB+ARDUINO')
xlabel('Pressió cada 200ms')
ylabel('Metres sota aigua [m]');
grid on;
hold on;
%Descartem el primer valor (és text)
pressio=fscanf(serialPort,'%d');
global lletra teclatoboto mostres
lletra='p';
a='p';
mostres=9999; %Posem moltes mostres i, si premem el botó Aturar, posarem la mostres=1 perquè s'aturi.
%Bucle que llegeix i dibuixa les mostres
while(i<=mostres)
ylim([-1 5]);
xlim([i-20 i+5]);
pressio=fscanf(serialPort,'%d');
%y(i)=((pressio[bits]*(5[V_AO]/1023[bits])*(1[V_SENS]/161[V_AO])*(1[KPa]/0,2e-3[V_AO]))[pressio mesurada sensor KPa]-pressio_inicial[KPa]))*(10[m Aigua]/100[KPa]); %PASSEM ELS BITS A BAR I DE BAR A COL AIGUA NORMALITZADES
y(i)=((pressio*(5/1023)*(1/161)*(1/0.2e-3))-pressio_ini)*(10/100);
%y(i)=(pressio(1)*(1/pressio_inicial)*10)-10;
plot(i,y(i),'X-r');
drawnow
%Escrivim les dades a resultats.txt
z=[i;y(i)];
fprintf(fid,'%d %.3f \n', z);
hold on;
%Escrivim les dades a resultats.txt
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%Si indiquem que volem controlar per teclat, llegirem per teclat. Si
%indiquem que per botons, per botons.
if (strcmp(teclatoboto,'teclat'))
a=llegir_tecles3;
elseif (strcmp(teclatoboto,'boto'))
a=lletra;
pause(1);
end;
disp(a);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
fprintf(serialPort,'%s\n',a);
i = i + 1;
end
%Enviem o per tancar rele
%fprintf(serialPort,'%s\n','o');
%pause(20);
%Tancam la connexió amb el serial i l'arxiu
fclose(serialPort);
fclose(fid);
delete(serialPort);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% --- Executes on button press in teclat.
function teclat_Callback(hObject, eventdata, handles)
% hObject handle to teclat (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hint: get(hObject,'Value') returns toggle state of teclat
global teclatoboto
teclatoboto='teclat';
% --- Executes on button press in botons.
function botons_Callback(hObject, eventdata, handles)
% hObject handle to botons (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hint: get(hObject,'Value') returns toggle state of botons
global teclatoboto
teclatoboto='boto';
% --- Executes on button press in on.
function on_Callback(hObject, eventdata, handles)
% hObject handle to on (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hint: get(hObject,'Value') returns toggle state of on
global lletra
lletra = 'm';
% --- Executes on button press in off.
function off_Callback(hObject, eventdata, handles)
% hObject handle to off (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hint: get(hObject,'Value') returns toggle state of off
global lletra
lletra = 'n';
% --- Executes on button press in endavant.
function endavant_Callback(hObject, eventdata, handles)
% hObject handle to endavant (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hint: get(hObject,'Value') returns toggle state of endavant
global lletra
lletra = 'w';
% --- Executes on button press in endarrere.
function endarrere_Callback(hObject, eventdata, handles)
% hObject handle to endarrere (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hint: get(hObject,'Value') returns toggle state of endarrere
global lletra
lletra = 's';
% --- Executes on button press in dreta.
function dreta_Callback(hObject, eventdata, handles)
% hObject handle to dreta (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hint: get(hObject,'Value') returns toggle state of dreta
global lletra
lletra = 'd';
% --- Executes on button press in esquerra.
function esquerra_Callback(hObject, eventdata, handles)
% hObject handle to esquerra (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hint: get(hObject,'Value') returns toggle state of esquerra
global lletra
lletra = 'a';
% --- Executes on button press in pujar.
function pujar_Callback(hObject, eventdata, handles)
% hObject handle to pujar (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hint: get(hObject,'Value') returns toggle state of pujar
global lletra
lletra = 'r';
% --- Executes on button press in baixar.
function baixar_Callback(hObject, eventdata, handles)
% hObject handle to baixar (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hint: get(hObject,'Value') returns toggle state of baixar
global lletra
lletra = 'f';
% --- Executes on button press in radiobutton11.
function radiobutton11_Callback(hObject, eventdata, handles)
% hObject handle to radiobutton11 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hint: get(hObject,'Value') returns toggle state of radiobutton11
global lletra
lletra = 'p';
% --- Executes on button press in aturar.
function aturar_Callback(hObject, eventdata, handles)
% hObject handle to aturar (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
global mostres
mostres=1;
--------------------
and can run it( the guide) because when i press the initializate pushbotton it says me this error:
--------------------
Error using serial/fopen (line 72)
Open failed: Port: COM7 is not available. Available ports: COM1, COM3.
Use INSTRFIND to determine if other instrument objects are connected to the requested device.
-------------------
I'm using Matlab 2018b and don't know how to change the port used in this scretch. I tried to tip COM3 or 4 on scretch wich are avaliable in my Matlab but it still talks about COM 7!!.
Thanks :)

Respuestas (0)

Categorías

Más información sobre MATLAB Support Package for Arduino Hardware en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by