Can not close Serial connection

Hello everyone. I have a problem with Serial ports. I can not close them. Even if I do not call Serial.begin(9600), it continues to send output to Serial port. Here is the basic code I am trying. My hardware is MKR WiFi 1010(ESP 32)

void setup() {
// put your setup code here, to run once:
Serial.end();
}
void loop() {
// put your main code here, to run repeatedly:
Serial.println("a");
delay(1000);
}

It is like a secret hand unlocks Serial communication behind my back, Can someone try this code piece and check Serial monitor?

I'm guessing that serial is being opened when you try to write to it. You should not write to serial if you do not want serial to be used. You could use macros/conditional defines to get about it:

//Uncomment the following line to enable serial
//#define USE_SERIAL

#ifdef USE_SERIAL
  #define SERIALPRINT(x) Serial.print(x)
  #define SERIALPRINTLN(x) Serial.println(x)
#else
  #define SERIALPRINT(x)
  #define SERIALPRINTLN(x)
#endif

void loop()
{
  SERIALPRINT("Hello ");
  SERIALPRINTLN("World!");

  #ifdef USE_SERIAL
    //Another approach
    Serial.println("Hello World!");
  #endif
}

What does the output look like?

aarg:
What does the output look like?

It looks like as expected:

Capture.PNG

Capture.PNG

I have just tried with an ESP32 (DOIT DEVKIT V1), and there is no serial output unless Serial.begin(..) is called.

Actually and could be the Serial port your are using is the ESP32 serial port (0) and is used for programing the ESP32 and to send to the monitor. You are not using Serial port (1) and Serial port (2).

I would not recommend using Serial (0) for connecting devices to your project.

What do you get when you print to Serial1.println( "blue daba di dad doo dabba dee doo doo dabe dee waw wa" ); or Serial2.println( "blue daba di dad doo dabba dee doo doo dabe dee waw wa" );.

What is the problem? Just don't send anything to serial.

aarg:
What is the problem? Just don't send anything to serial.

Normally I wouldn' t bother but I am trying to understand this strange behavior. How can we not close the regular Serial port?

Idahowalker:
Actually and could be the Serial port your are using is the ESP32 serial port (0) and is used for programing the ESP32 and to send to the monitor. You are not using Serial port (1) and Serial port (2).

I would not recommend using Serial (0) for connecting devices to your project.

What do you get when you print to Serial1.println( "blue daba di dad doo dabba dee doo doo dabe dee waw wa" ); or Serial2.println( "blue daba di dad doo dabba dee doo doo dabe dee waw wa" );.

I don't want to reuse Serial(0) for other purposes. Iam just trying to prevent it from working as functions promise me.

babaraza:
Normally I wouldn' t bother but I am trying to understand this strange behavior. How can we not close the regular Serial port?I don't want to reuse Serial(0) for other purposes. Iam just trying to prevent it from working as functions promise me.

Good luck with that.

babaraza:
It looks like as expected:

can you explain - what's the connexion between the PC and the Arduino? just USB ?

with no begin(), Serial.print() won't do anything normally
just tried this on a UNO

void setup() {}

void loop() {
  Serial.println("a");
  delay(1000);
}

but other posters are right, if you don't want to print, don't call print in the first place... your code will be more optimized

My connection is simply USB cable.

J-M-L:
can you explain - what's the connexion between the PC and the Arduino? just USB ?

with no begin(), Serial.print() won't do anything normally
just tried this on a UNO

void setup() {}

void loop() {
 Serial.println("a");
 delay(1000);
}




but other posters are right, if you don't want to print, don't call print in the first place... your code will be more optimized

And for the last time for all helpful people and people may be interested in this weird behavior: if I do not want anything to be printed, I won't call println/print(duh). But at least on MKR 1010 WiFi, Serial can not be closed nor it needs to be initialized. Also, the libraries I use may call Serial prints. I do not control every part of what goes in final binary.

babaraza:
Also, the libraries I use may call Serial prints. I do not control every part of what goes in final binary.

Have you consider that you can open up those libraries that use serial print and edit those libraries so they do not do the serial print thing?

babaraza:
But at least on MKR 1010 WiFi, Serial can not be closed nor it needs to be initialized.

OK - I learnt something today.

Can you explain why this is a problem though ? If you don’t want to see anything why would you have a serial monitor opened and connected to the port on the other end…

J-M-L:
OK - I learnt something today.

Can you explain why this is a problem though ? If you don’t want to see anything why would you have a serial monitor opened and connected to the port on the other end...

It's like this. Every time I drink tea I get a pain in my eye. I've been told to take the spoon out before drinking tea but I don't want to take the spoon out. What do I do to get rid of the pain in my eye caused by me drinking tea and leaving my spoon in the cup?

:slight_smile:

Idahowalker:
What do I do to get rid of the pain in my eye caused by me drinking tea and leaving my spoon in the cup?

Keep doing what you do, at some point in time the spoon will bend or the eye will pop! :stuck_out_tongue:

Boink, I sure do feel silly. I am sorry for being snarky, see above.

I got curious, something nagging me in the addled brain. So I went hunting to find that you can turn 'off' the serial ports on the ESP32 by using the ESP32 UART API UART - ESP32 - — ESP-IDF Programming Guide latest documentation

The macro uart_driver_delete(uart_port_tuart_num)
will unload the UART driver.

How embarrassing to be caught with my fly open like this.

Oooh, I'm smelling an X/Y problem. https://xyproblem.info/

The clue is in one's immediate reaction to the post title. "Close" the serial connection? "Close" the USB port sticking out of the side of the board? What's that supposed to mean? Why on earth would anyone want to do such a thing?

Idahowalker:
Have you consider that you can open up those libraries that use serial print and edit those libraries so they do not do the serial print thing?

Of course I do not like to manually do that kind of things. It is a nuisance. Problem is not preventing Serial prints with a hacky ways. It is the hardware that you can not close when documentation states otherwise.

babaraza:
Of course I do not like to manually do that kind of things. It is a nuisance. Problem is not preventing Serial prints with a hacky ways. It is the hardware that you can not close when documentation states otherwise.

Which documentation?

Have you taken this grave and most serious fatal flaw up with Espressif esp32.com?

Oh, don’t forget. You can unload the UART drivers, as per the ESP32 API.