# Loop through serial ports

**URL:** https://discourse.processing.org/t/loop-through-serial-ports/28442
**Category:** Electronics (Arduino, etc.)
**Created:** [March 11, 2021, 4:32pm UTC](https://discourse.processing.org/t/loop-through-serial-ports/28442 "2021-03-11T16:32:45Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![Konstanitn](https://avatars.discourse-cdn.com/v4/letter/k/9d8465/32.png) [@Konstanitn](https://discourse.processing.org/u/Konstanitn)
#### Post date: [March 11, 2021, 4:32pm UTC](https://discourse.processing.org/t/loop-through-serial-ports/28442/1 "2021-03-11T16:32:45Z")

</div>

Loop through serial ports

Hello,  
I am trying to make a while loop for searching of the right serial port. I get the `“RuntimeException: Error opening serial port COM4: Port busy”` error probably because the port connection is still open. For my understanding there should be a command like “close.myPort”.

```auto
while (firstContact == false){  
for (int i = 0; i <= Serial.list().length-1; ++i)
  {
  myPort = new Serial(this, Serial.list()[i], 9600);
  val = myPort.readStringUntil('\n');
  val = trim(val);
    if (val != null) 
    {
      if (val.equals("K")) 
      {
      firstContact = true;
      portNumber=i;
      myPort.write("O");
      println("contact");
      }
      else {println("no contact");}
    }
  }
}

```

Sorry if the solution is too simple but I could not google the answer.

---

<div class="post-metadata">

### Author: ![micuat](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/micuat/32/19407_2.png) [@micuat](https://discourse.processing.org/u/micuat)
#### Post date: [March 11, 2021, 7:13pm UTC](https://discourse.processing.org/t/loop-through-serial-ports/28442/2 "2021-03-11T19:13:44Z")

</div>

I think you cannot simply “close” the port because other applications can be using it. Perhaps you can wrap inside the for loop with try/catch not to abort the program  
[https://processing.org/reference/catch.html](https://processing.org/reference/catch.html)

---

<div class="post-metadata">

### Author: ![Konstanitn](https://avatars.discourse-cdn.com/v4/letter/k/9d8465/32.png) [@Konstanitn](https://discourse.processing.org/u/Konstanitn)
#### Post date: [March 11, 2021, 11:04pm UTC](https://discourse.processing.org/t/loop-through-serial-ports/28442/3 "2021-03-11T23:04:39Z")

</div>

To clarify, the for loop (first run) works in the example above. The error occurs when it get called second time, because it can’t do `myPort = new Serial(this, Serial.list()[i], 9600);` again for same port.

---

<div class="post-metadata">

### Author: ![micuat](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/micuat/32/19407_2.png) [@micuat](https://discourse.processing.org/u/micuat)
#### Post date: [March 11, 2021, 11:21pm UTC](https://discourse.processing.org/t/loop-through-serial-ports/28442/4 "2021-03-11T23:21:00Z")

</div>

oh. In that case why wouldn’t you keep the already-opened port variable and use it later?

---

<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 15, 2021, 2:23am UTC](https://discourse.processing.org/t/loop-through-serial-ports/28442/5 "2021-03-15T02:23:07Z")

</div>

> [@Konstanitn](#):
>
> For my understanding there should be a command like “close.myPort”.

It is here:  
_[Serial / Libraries / Processing.org](https://processing.org/reference/libraries/serial/Serial.html)_

`:)`
