# Serial port left open when sketch program closed

**URL:** <https://discourse.processing.org/t/serial-port-left-open-when-sketch-program-closed/28592>\
**Category:** Electronics (Arduino, etc.)\
**Created:** [March 16, 2021, 8:06pm UTC](https://discourse.processing.org/t/serial-port-left-open-when-sketch-program-closed/28592 "2021-03-16T20:06:13Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jeffmarc](https://avatars.discourse-cdn.com/v4/letter/j/82dd89/32.png) [@jeffmarc](https://discourse.processing.org/u/jeffmarc)\
**Post date:** [March 16, 2021, 8:06pm UTC](https://discourse.processing.org/t/serial-port-left-open-when-sketch-program-closed/28592/1 "2021-03-16T20:06:13Z")

</div>

Serial port opened by sketch is left open . When I try to run sketch again it fails with port busy  
try - catch exception doesn’t work on re running program to close open port then reopen.  
Is there a way , other than restarting PC to clear this?  
Restarting or removing power from serial I/O card is only way that works!

---

<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:** [March 16, 2021, 8:24pm UTC](https://discourse.processing.org/t/serial-port-left-open-when-sketch-program-closed/28592/2 "2021-03-16T20:24:31Z")

</div>

Hello,

Close the sketch with a _keypressed()_ function that will _stop()_ serial port and _exit()_

- [keyPressed() \ Language (API) \ Processing 3+](https://www.processing.org/reference/keyPressed_.html)
- [Serial::stop() \ Language (API) \ Processing 3+](https://processing.org/reference/libraries/serial/Serial_stop_.html)
- [exit() \ Language (API) \ Processing 3+](https://processing.org/reference/exit_.html)

Let me know if this works.

`:)`

---

<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:** [March 17, 2021, 11:35am UTC](https://discourse.processing.org/t/serial-port-left-open-when-sketch-program-closed/28592/3 "2021-03-17T11:35:45Z")

</div>

Hello,

I also came across this topic:

> [@Can I interrupt a closing of a sketch to save data?](https://discourse.processing.org/t/can-i-interrupt-a-closing-of-a-sketch-to-save-data/16231):
>
> Hello all, Can I interrupt a closing of a sketch to save data? When Alt-F4 is pressed or the cross in the window is clicked (OS = Windows), the program closes. I would like to interrupt this, to save a file that the user changed. I this possible? Thanks! Regards, Chrisir P.S. This belongs to the project: [Can I make a small windows app for taking time and log production times?](https://discourse.processing.org/t/can-i-make-a-small-windows-app-for-taking-time-and-logg-production-times/15484/13)

I have NOT tested it but it may have potential for adding a clean exit (saving data, closing serial port etc.) to sketches.

`:)`
