# How to close serial port when window is closed

**URL:** <https://discourse.processing.org/t/how-to-close-serial-port-when-window-is-closed/37854>\
**Category:** Electronics (Arduino, etc.)\
**Created:** [July 6, 2022, 3:01am UTC](https://discourse.processing.org/t/how-to-close-serial-port-when-window-is-closed/37854 "2022-07-06T03:01:08Z")\
**Posts on this page:** 1\
**Showing post:** 5

<div class="post-metadata">

**Author:** ![Ghostsss](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/ghostsss/32/16483_2.png) [@Ghostsss](https://discourse.processing.org/u/Ghostsss)\
**Post date:** [July 14, 2022, 12:15pm UTC](https://discourse.processing.org/t/how-to-close-serial-port-when-window-is-closed/37854/5 "2022-07-14T12:15:22Z")

</div>

I found the solution.

Credit : [Found info here](https://discourse.processing.org/t/can-i-interrupt-a-closing-of-a-sketch-to-save-data/16231)

Here is the code:

```auto
void exit() {
    println("Exiting program");
    myPort.stop(); // stoping serial port.
   //your Code above. 
   super.exit();
}

```

---

_[View the full topic](https://discourse.processing.org/t/how-to-close-serial-port-when-window-is-closed/37854)._
