# P5 serial port not closing

**URL:** <https://discourse.processing.org/t/p5-serial-port-not-closing/32366>\
**Category:** Libraries\
**Created:** [September 22, 2021, 3:49am UTC](https://discourse.processing.org/t/p5-serial-port-not-closing/32366 "2021-09-22T03:49:12Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![processing\_user](https://avatars.discourse-cdn.com/v4/letter/p/7c8e57/32.png) [@processing\_user](https://discourse.processing.org/u/processing_user)\
**Post date:** [September 22, 2021, 3:49am UTC](https://discourse.processing.org/t/p5-serial-port-not-closing/32366/1 "2021-09-22T03:49:12Z")

</div>

hi,  
I am using p5 serial library, everything works fine but the only thing is I am unable to close the port once done using it. The browser once opened will not close the port so I wanted to use keypressed() function to close the port when ESCAPE key is pressed. But it is not working. I get the message in console that it is close but it is not closed(another terminal says port is busy). In order to close I had to use the p5 serial port monitor.

The following is the code:

```auto
let serial;
var pot;

function setup() {
  createCanvas(400, 400);
  serial = new p5.SerialPort();
   serial.list();
   serial.open("COM3");
   serial.on('data', portData);
   serial.on('close', portClose); 
}

function draw(){
  background("#2C2323");
  fill("#4293D8");
  ellipse(width/2, height/2, pot, pot);
}

function portData() {
  pot = Number(serial.readLine());
  //console.log(pot);
}

function portClose(){
    print("Serial Port is Closed");
}

function keyPressed() {
  if (keyCode === ESCAPE) {
      portClose();
  }
}

```

how can I close the port with key pressed or button would be helpful.

thanks
