ESP32 automatically resets after using fclose in Matlab

robpo1111
Posts: 2
Joined: Mon Aug 19, 2019 11:25 am

ESP32 automatically resets after using fclose in Matlab

Postby robpo1111 » Mon Aug 19, 2019 11:31 am

Hello everybody,

I am currently facing a problem using an ESP32 and Matlab.
Everything works just fine except after Matlab closes the serial connection using fclose, the ESP32 resets. There are two ways to get out of this state, so the ESP32 works normally again:
1. Open the serial monitor in the Arduino IDE. The monitor shows the information of the bootloader. The ESP32 works after it until fclose happens again.
2. Use fopen in Matlab, wait a little bit and the ESP32 works again fine.

Both ways won't work for my project. The ESP32 needs to be always online since it's used as an access point.

Here is the a simple code example to simulate the problem:
ESP32:

Code: Select all

void setup()
{
  Serial.begin(230400);
  pinMode(32, OUTPUT);
  digitalWrite(32, LOW);
}

void loop()
{
 
  }
 

Matlab:

Code: Select all

s = serial('COM20');
set(s,'BaudRate',230400, 'timeout', 0.5);
fopen(s);
fclose(s);



Does anyone has any Idea?
I already used google and looked in a bunch of diffent forums but I couldn't find the correct solution to avoid this problem. I don't really understand the problem since it's just opening and closing a serial port.

Thanks in advance!

Angus
Espressif staff
Espressif staff
Posts: 2344
Joined: Sun May 08, 2016 4:11 am

Re: ESP32 automatically resets after using fclose in Matlab

Postby Angus » Tue Aug 20, 2019 4:51 am

What ESP32 hardware do you have (development board model, etc)?

Most development boards use the RTS & DTR pins to reset into bootloader mode, specifically RTS pin will toggle the EN pin to reset the board. If Matlab fclose() is toggling RTS then it may be resetting the board, or keeping it in reset indefinitely.

robpo1111
Posts: 2
Joined: Mon Aug 19, 2019 11:25 am

Re: ESP32 automatically resets after using fclose in Matlab

Postby robpo1111 » Wed Aug 21, 2019 4:58 am

Thanks for your answer!
Yes I am currently using the development board ESP32 NodeMCU.
Do you have a suggestion to avoid the problem without changing the development board?

rob

Cholesterol
Posts: 1
Joined: Tue Apr 15, 2025 6:05 am

Re: ESP32 automatically resets after using fclose in Matlab

Postby Cholesterol » Tue Apr 15, 2025 6:14 am

Hi,
I created an account, because I ran into a similar issue.
For me it was worse, because the ESP stayed in reset and I had to manually powercycle it to get it running again.
My workaround was to add these lines after opening the serial port:

Code: Select all

fopen(s1);
pause(1);
s1.RequestToSend = 'on';
pause(1);
s1.RequestToSend = 'off';
pause(1);
This got the ESP out of reset but does not prevent it from going into reset in the first place when doing a fclose.

lbernstone
Posts: 1132
Joined: Mon Jul 22, 2019 3:20 pm

Re: ESP32 automatically resets after using fclose in Matlab

Postby lbernstone » Tue Apr 15, 2025 4:19 pm

Have you tried turning off flow control?

Who is online

Users browsing this forum: Baidu [Spider], Qwantbot and 3 guests