Page 1 of 1

ESP32 automatically resets after using fclose in Matlab

Posted: Mon Aug 19, 2019 11:31 am
by robpo1111
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!

Re: ESP32 automatically resets after using fclose in Matlab

Posted: Tue Aug 20, 2019 4:51 am
by Angus
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.

Re: ESP32 automatically resets after using fclose in Matlab

Posted: Wed Aug 21, 2019 4:58 am
by robpo1111
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

Re: ESP32 automatically resets after using fclose in Matlab

Posted: Tue Apr 15, 2025 6:14 am
by Cholesterol
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.

Re: ESP32 automatically resets after using fclose in Matlab

Posted: Tue Apr 15, 2025 4:19 pm
by lbernstone
Have you tried turning off flow control?