Serial port with ESP-32 not working.

22 views (last 30 days)
Gerson
Gerson on 30 Oct 2023
Answered: Raj on 7 Nov 2023
We're having some difficulties with a project. Apparently an "error using serialport", matlab says it can connect to port 'COM8', but that is indeed the right port. We don't know if it makes any difference, but the following is what we have as com8: USB-SERIAL CH340(COM8)
The code is as follows:
%% nettoyage de l'environnement
clear
close
close all;
clear all
%% liste de toutes les matrices
EDF_mes=[];
EDF_cmd=[];
temperature_mat=[];
P_atm=[];
P_mes=[];
P_mes_new=[];
P_est=[];
Q_mes=[];
Q_est=[];
reg_mes=[];
reg_cmd=[];
U_DC=[];
I_DC=[];
Pow_DC=[];
I_15V=[];
cmd=[];
RPM_mes=[];
D_I=[];
D_O=[];
sample=[];
temps=[];
Fontsize = 12
hypothese=3
p_estime=0
q_estime=0
%% adresses modbus
adress_tension = 2;
adress_speed = 3;
adress_U_DC = 4;
adress_I_DC = 5;
adress_Power = 6;
%% initialisation de l'UI
fig = uifigure('Name','test_manuel',"Position",[100 100 800 800]);
graphical_window = uigridlayout(fig);
%graphical_window.RowHeight = {'1x','1x'};
%graphical_window.ColumnWidth = {'1x','fit','1x'};
fig.UserData=1; % utilisé pour mettre fin a la boucle principale
fig.CloseRequestFcn = @(src,event)my_closereq(src);
%% initialisation sliders cmd
cmd_edf = uislider(graphical_window); % Slider tension
cmd_edf.Limits = [80 260]; % I3
cmd_edf.Value=123;
cmd_edf.Layout.Row = 1;
cmd_edf.Layout.Column = [4];
cmd_edf.UserData="fdsf";
cmd_moteur =uislider(graphical_window); % Puissance moteur
cmd_moteur.Limits = [0 6500];
cmd_moteur.Value=3000;
cmd_moteur.Layout.Row = 5;
cmd_moteur.Layout.Column = [4];
cmd_moteur.UserData="fdsf";
cmd_registre=uislider(graphical_window); % Papillon
cmd_registre.Limits = [0 10];
cmd_registre.Value=7;
cmd_registre.Layout.Row = 3;
cmd_registre.Layout.Column = [4];
cmd_registre.UserData="fdsf";
%% initialisation des graphes
grapheQ = uiaxes(graphical_window);
grapheQ.Layout.Row = [1 3];
grapheQ.Layout.Column = [1 3];
%plot(grapheQ,sample,D_I)
grapheQ.Title.String=("débit en fonction du temps");
grapheQ.YLabel.String = 'Débit en m3/h';
grapheQ.XLabel.String = 'nb échantillons';
%legend('Q_est','Q_mes');
%grapheQ.Legend('Q_est','Q_mes')
grapheP = uiaxes(graphical_window);
grapheP.Layout.Row = [4 6];
grapheP.Layout.Column = [1 3];
%plot(grapheP,sample,D_I)
grapheP.Title.String=("Pression en fonction du temps");
grapheP.YLabel.String = 'Pression en Pa';
grapheP.XLabel.String = 'nb échantillons';
graphe_EDF = uiaxes(graphical_window);
graphe_EDF.Layout.Row = 2;
graphe_EDF.Layout.Column = 4;
%plot(graphe_EDF,sample,D_I)
graphe_EDF.Title.String=("Tension EDF");
graphe_EDF.YLabel.String = 'Tension en Vac';
graphe_EDF.XLabel.String = 'nb échantillons';
graphe_cmd = uiaxes(graphical_window);
graphe_cmd.Layout.Row = 6;
graphe_cmd.Layout.Column = 4;
%plot(graphe_cmd,sample,D_I)
graphe_cmd.Title.String=("commande moteur mV");
graphe_cmd.YLabel.String = 'cmd mV';
graphe_cmd.XLabel.String = 'nb échantillons';
graphe_rpm = uiaxes(graphical_window);
graphe_rpm.Layout.Row = 7;
graphe_rpm.Layout.Column = 4;
%plot(graphe_rpm,sample,D_I)
graphe_rpm.Title.String=("RPM moteur");
graphe_rpm.YLabel.String = 'RPM';
graphe_rpm.XLabel.String = 'nb échantillons';
graphe_registre = uiaxes(graphical_window);
graphe_registre.Layout.Row = 4;
graphe_registre.Layout.Column = 4;
%plot(graphe_registre,sample,D_I)
graphe_registre.Title.String=("position registre");
graphe_registre.YLabel.String = 'position';
graphe_registre.XLabel.String = 'nb échantillons';
graphe_registre2 = uiaxes(graphical_window);
graphe_registre2.Layout.Row = 7;
graphe_registre2.Layout.Column = [1 3];
%plot(graphe_registre2,sample,D_I)
graphe_registre2.Title.String=("position registre");
graphe_registre2.YLabel.String = 'position';
graphe_registre2.XLabel.String = 'nb échantillons';
disp(123)
%% DAQ :
% DAQ general parameters
disp("initialistion device_daq")
device_daq = daq("ni");
time_offset = 0; % s
rate = 10; % Hz - communication rate with NI
device_daq.Rate = 1/(1/rate-time_offset);
disp('DAQ input')
%% Initialisation new pressure
newPressureDevice = serialport("COM8", 9600);
% DAQ input:
read_pressure = addinput(device_daq,"Dev1","ai8","Voltage");
read_pressure.TerminalConfig = "SingleEnded";
%
read_flow_rate = addinput(device_daq,"Dev1","ai1","Voltage");
read_flow_rate.TerminalConfig = "SingleEnded";
%
read_valva_position = addinput(device_daq,"Dev1","ai3","Voltage");
read_valva_position.TerminalConfig = "SingleEnded";
% daq output:
%controle du registre
write_motor_signal = addoutput(device_daq,"Dev1","ao1","Voltage");
write_motor_signal.Range = [-10 10];
disp(146)
%% initialistion modbus
disp("initialistion modbus")
moteur_modbus = modbus("serialrtu","COM5");
disp("modbus: ok")
%% controle de la tension d'alimentation
% commande servo moteur de l'autotransformateur (arduino usb + servo
% moteur)
cmd_autotransfo=serialport("COM7",9600); % Vérifier la BAUD
position_autotransfo = 260;
consigne_position=90;
position_autotransfo_str=int2str(position_autotransfo);
write(cmd_autotransfo, position_autotransfo_str, "uint8");
configureTerminator(cmd_autotransfo,"CR/LF")
disp("autotransfo arduino connecté")
% voltmetre USB (visa)
visausb = visadev("USB0::0x0957::0x0607::MY47011181::0::INSTR");
visausb.Timeout=3;
visausb.write("CONF:VOLT:AC")
visausb.write("CONF:VOLT:AC BAND 20")
tension_entree=visausb.writeread("READ?");
%tension_voulue = (tension_voulue_init);
disp(165)
%% mesure de la température ambiante
comserie_temp=serialport("COM3",9600);
configureTerminator(comserie_temp,"CR/LF");
%% boucle principale
% Integration UI
toto=0;
i=0;
visausb.Timeout=2;
while fig.UserData<2
%% condition de sortie de la boucle et compteur de boucle
%fig.UserData=fig.UserData+1
%pause(0.1)
toto=toto+1;
%
%% get data
% Vedf
var_temp=visausb.writeread("READ?");
var_temp=str2double (var_temp);
EDF_mes=[EDF_mes;var_temp];
EDF_cmd=[EDF_cmd ;cmd_edf.Value];
% temperature
%var_temp=readline(comserie_temp);
%var_temp=str2double (var_temp);
temperature_mat=[temperature_mat ; var_temp];
% données du DAQ
inScanData = read(device_daq);
pressure = table2array(inScanData(:,1));
% pressure_convert=(pressure-2)*62.5;
pressure_convert=(pressure*20);
P_mes=[P_mes;pressure_convert];
flow_rate = table2array(inScanData(:,2));
flow_rate_convert=(flow_rate*63.375)-126.75;
Q_mes=[Q_mes;flow_rate_convert];
var_temp=table2array(inScanData(:,3));
reg_mes=[reg_mes;var_temp];
%% New pressor sensor
new_pressure = readline(newPressureDevice);
new_num = str2double(new_pressure);
disp(new_pressure);
P_mes_new=[P_mes_new;new_pressure];
disp('DONE')
pause(0.5)
%% pression obtenue sur internet
try
webb = webread("https://www.allosurf.net/meteo/live/toulouse-blagnac-station-meteo-lfbo-5386.html");
k = strfind(webb, 'hPa');
releve = webb(k(1)-5: k(1)-2);
catch
releve = '0000';
end
releve=str2num(releve);
P_atm=[P_atm;releve];
% données modbus
modbus_data = read(moteur_modbus,"holdingregs",17,7);
U_DC=[U_DC; modbus_data(4)* 0.1];
I_DC=[I_DC; modbus_data(5)* 0.1];
Pow_DC=[Pow_DC; modbus_data(6)* 0.1];
I_15V=[I_15V; modbus_data(7)* 0.1];
cmd=[cmd; modbus_data(2)];
RPM_mes=[RPM_mes; modbus_data(3)];
if toto==1
D_I=[D_I; 0];
D_O=[D_O; 0];
else
D_I=[D_I;I_DC(end)-I_DC(end-1)];% 0];
D_O=[D_O;RPM_mes(end)-RPM_mes(end-1)];% 0];
end
% données estimées
%p_estime=pressure_06_14_2023_15_14(RPM_mes(end),U_DC(end),I_DC(end),D_O(end),D_I(end))*0.4+0.6*p_estime;%(rpm,udc,idc,delta_rpm, delta_idc)
%q_estime=flow_06_14_2023_15_14(RPM_mes(end),U_DC(end),I_DC(end),D_O(end),D_I(end))*0.4+0.6*q_estime;
if hypothese==1
q_estime=flow_hyp_1deg8(RPM_mes(end),U_DC(end),I_DC(end))
p_estime=pressure_hyp_1deg8(RPM_mes(end),U_DC(end),I_DC(end))
end
if hypothese==2
q_estime=flow_hyp_2deg7(RPM_mes(end),U_DC(end),I_DC(end),D_I(end))
p_estime=pressure_hyp_2deg7(RPM_mes(end),U_DC(end),I_DC(end),D_I(end))
end
if hypothese==3
q_estime=flow_hyp_3deg6(RPM_mes(end),U_DC(end),I_DC(end),D_O(end))
p_estime=pressure_hyp_3deg6(RPM_mes(end),U_DC(end),I_DC(end),D_O(end))
end
if hypothese==4
q_estime=flow_hyp_4deg7(RPM_mes(end),U_DC(end),I_DC(end),D_I(end),D_O(end))
p_estime=pressure_hyp_4deg7(RPM_mes(end),U_DC(end),I_DC(end),D_I(end),D_O(end))
end
if p_estime<0
p_estime=0
end
if p_estime>600
p_estime=600
end
if q_estime<0
q_estime=0
end
if q_estime>600
q_estime=600
end
P_est=[P_est;p_estime];
Q_est=[Q_est;q_estime];
% données sliders
reg_cmd=[reg_cmd;cmd_registre.Value];
% echantillonage
sample=[sample ;toto];
temps=[temps;now];
%% asservissements
%registre
write(device_daq,cmd_registre.Value);
%commande VMC
var_temp=double(round(cmd_moteur.Value));
disp(var_temp)
disp(278);
i=var_temp%i+100
i=mod(i,6000)
if i<3000
i=3000
end
disp('cmd')
disp(i)
disp(cmd_moteur.Value)
disp(cmd(end))
write(moteur_modbus,'holdingregs',2,i);
disp(280);
%% plot des graphes
plot(graphe_cmd,sample,cmd,'--b')%,sample,RPM_mes,'.r')
lgd_cmd=legend(graphe_cmd, 'cmd moteur (mV)', 'Location','northwest') ;
lgd_cmd.FontSize = Fontsize;
plot(graphe_EDF,sample,EDF_cmd,'--b',sample,EDF_mes,'.r');
lgd_EDF=legend(graphe_EDF, 'EDF_cmd','EDF_mes', 'Location','northwest') ;
lgd_EDF.FontSize= Fontsize;
plot(graphe_registre,sample,reg_cmd,'--b',sample,reg_mes,'.r');
lgd_registre=legend(graphe_registre, 'reg_cmd','reg_mes', 'Location','northwest');
lgd_registre.FontSize= Fontsize;
plot(graphe_registre2,sample,reg_cmd,'--b',sample,reg_mes,'.r');
lgd_registre2=legend(graphe_registre, 'reg_cmd','reg_mes', 'Location','northwest');
lgd_registre2.FontSize=Fontsize;
plot(graphe_rpm,sample,RPM_mes,'--b');%,sample,RPM_mes,'.r')
lgd_rpm=legend(graphe_rpm, 'RPM_mes', 'Location','northwest');
lgd_rpm.FontSize=Fontsize;
plot(grapheP,sample,P_est,'--b',sample,P_mes,'.r', sample, P_mes_new);
lgd_P=legend(grapheP, 'P_est','P_mes','P_mes_new', 'Location','northwest');
lgd_P.FontSize=Fontsize;
plot(grapheQ,sample,Q_est,'--b',sample,Q_mes,'.r');
lgd_Q=legend(grapheQ, 'Q_est','Q_mes', 'Location','northwest');
lgd_Q.FontSize=Fontsize;
%%sauvegarde des données en matrice
save_matrix=[EDF_mes,EDF_cmd,temperature_mat,P_atm,P_mes,P_est,Q_mes,Q_est,reg_mes,reg_cmd,U_DC,I_DC,Pow_DC,I_15V,cmd,RPM_mes,D_I,D_O,sample,temps];
pause(0.2)
if P_mes>500
fig.UserData=3
end
% -------------------------------------------------Integration panne_UI
%
%% on convertit les volts en degre
% récupère la consigne du slider
consigne_edf=cmd_edf.Value;
% on mesure la sortie
sortie=EDF_mes(end);
% on calcul l'erreur
erreur = sortie-consigne_edf;
% on met une consigne de position qui prends en compte l'erreur et la
% consigne du slider
Kp=-0.6;% correcteur proportionnel;
consigne_position=consigne_position+Kp*erreur;
consigne_position=round(consigne_position);
if consigne_position>180
consigne_position=180
end
if consigne_position<0
consigne_position=0
end
position_autotransfo_str=int2str(consigne_position);
disp(position_autotransfo_str)
write(cmd_autotransfo, position_autotransfo_str, "uint8");
%
%pause(0.5)
% -------------------------------------------------Integration panne_UI
end
%% procedure de fermeture
currentDateTime = datestr(now, 'mm-dd-yyyy_HH-MM'); % Format YYYY-MM-DD_HH-MM-SS
fileName = strcat('Repetabilite_var_position_', currentDateTime, '.csv');
%matrice= [RPM_mes,cmd];
writematrix (save_matrix,fileName,"Delimiter",';')
% exportapp(fig,['filename.jpg'])
disp(fig.UserData)
delete(fig)
write(moteur_modbus,'holdingregs',2,0);
write(device_daq,0);
disp("Fin du test");
function my_closereq(fig)
selection = uiconfirm(fig,'Close the figurewindow?',...
'Confirmation');
switch selection
case 'OK'
%exportapp(fig,['filename'])
fig.UserData=201 %delete(fig)
case 'Cancel'
return
end
end
The specific parts of the code that have been causing this problem are:
%% Initialisation new pressure
newPressureDevice = serialport("COM8", 9600);
And...
%% New pressor sensor
new_pressure = readline(newPressureDevice);
new_num = str2double(new_pressure);
disp(new_pressure);
P_mes_new=[P_mes_new;new_pressure];
disp('DONE')
pause(0.5)
If anyone has any idea please share.
Thanks in advance

Answers (1)

Raj
Raj on 7 Nov 2023
Hello Gerson,
I understand you are facing an error while using serial port when you connecting your ESP-32 with your laptop.
There may be several reasons responsible for this-
  • Incorrect port might be selected. You can use the "serialportlist” function to list all available serial ports and verify that the ESP-32 is connected to the expected port.
  • Check if there are any driver issues and the support packages for ESP-32 are installed and up-to date.
  • Check if all serial ports are not already in use in ESP-32. Since ESP-32 has 3 serial ports, atleast one of them should be available while you connect ESP-32 to MATLAB.
You can also refer to the documentation from MathWorks regarding how to troubleshoot serial port connection issues-
I hope this resolves your query and you are able to proceed further!

Categories

Find more on Signal Integrity Kits for Industry Standards in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!