# 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:** 5\
**Page:** 1

<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 6, 2022, 3:01am UTC](https://discourse.processing.org/t/how-to-close-serial-port-when-window-is-closed/37854/1 "2022-07-06T03:01:08Z")

</div>

I am using serial port for building GUI with ControlP5.  
I open serial port in setup() function. i.e. tha start of program.

I want to close the serial port when user closes the window.  
I know that port.close() can be used to close the serial port.

But I don’t know where to use this command as I don’t know what is executed when the user closes the window.  
Please help.

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [July 6, 2022, 3:31am UTC](https://discourse.processing.org/t/how-to-close-serial-port-when-window-is-closed/37854/2 "2022-07-06T03:31:42Z")

</div>

> [@Ghostsss](#):
>
> I want to close the serial port when user closes the window.  
> I know that port.close() can be used to close the serial port.

You can override _exit()_:

> <https://github.com/benfry/processing4/blob/main/core/src/processing/core/PApplet.java#L3512>

I did some experimenting with this a while ago to close the serial port when closing the sketch.

This only works when you close sketch window with X

```auto
@Override  
 public void exit() {
    if (surface.isStopped()) {
      // exit immediately, dispose() has already been called,
      // meaning that the main thread has long since exited
      exitActual();

    } else if (looping) {
      // dispose() will be called as the thread exits
      finished = true;
      // tell the code to call exitActual() to do a System.exit()
      // once the next draw() has completed
      exitCalled = true;

    } else { // !looping
      // if not looping, shut down things explicitly,
      // because the main thread will be sleeping
      dispose();

      // now get out
      exitActual();
    }

  println("Exiting..."); // This is all I added!
  } 

```

Give it a try adding your code.

References:

- [https://processing.org/reference/libraries/serial/index.html](https://processing.org/reference/libraries/serial/index.html)
- [https://processing.github.io/processing-javadocs/libraries/processing/serial/Serial.html](https://processing.github.io/processing-javadocs/libraries/processing/serial/Serial.html)
- [https://github.com/processing/processing4/tree/master/java/libraries/serial](https://github.com/processing/processing4/tree/master/java/libraries/serial)
- [https://processing.org/reference/libraries/serial/Serial\_stop\_.html](https://processing.org/reference/libraries/serial/Serial_stop_.html)  
\*[https://github.com/processing/processing4/blob/master/core/src/processing/core/PApplet.java#L1258](https://github.com/processing/processing4/blob/master/core/src/processing/core/PApplet.java#L1258)

`:)`

---

<div class="post-metadata">

**Author:** ![jafal](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jafal/32/19112_2.png) [@jafal](https://discourse.processing.org/u/jafal)\
**Post date:** [July 6, 2022, 12:12pm UTC](https://discourse.processing.org/t/how-to-close-serial-port-when-window-is-closed/37854/3 "2022-07-06T12:12:27Z")

</div>

Hi

> <https://github.com/adafruit/Adafruit_9DOF/blob/master/examples/ahrs/processing/cuberotate/cuberotate.pde>

```auto
void setSerialPort(String portName) {
  // Close the port if it's currently open.
  if (port != null) {
    port.stop();
  }
  try {
    // Open port.
    port = new Serial(this, portName, 115200);
    port.bufferUntil('\n');
    // Persist port in configuration.
    saveStrings(serialConfigFile, new String[] { portName });
  }
  catch (RuntimeException ex) {
    // Swallow error if port can't be opened, keep port closed.
    port = null; 
  }
}

```

---

<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, 10:56am UTC](https://discourse.processing.org/t/how-to-close-serial-port-when-window-is-closed/37854/4 "2022-07-14T10:56:22Z")

</div>

Thank you,

I am new to java.

I don’t understand where to add myPort.stop(); in this code?  
Please help.

---

<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();
}

```
