# Serial Events and the which parameter

**URL:** <https://discourse.processing.org/t/serial-events-and-the-which-parameter/39638>\
**Category:** Electronics (Arduino, etc.)\
**Created:** [November 9, 2022, 1:47am UTC](https://discourse.processing.org/t/serial-events-and-the-which-parameter/39638 "2022-11-09T01:47:23Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![GhostGlitch](https://avatars.discourse-cdn.com/v4/letter/g/b4bc9f/32.png) [@GhostGlitch](https://discourse.processing.org/u/GhostGlitch)\
**Post date:** [November 9, 2022, 1:47am UTC](https://discourse.processing.org/t/serial-events-and-the-which-parameter/39638/1 "2022-11-09T01:47:23Z")

</div>

In the Libraries Reference for serialEvents it mentions a “which” parameter but does  
not go into any details about it. I’ve search for any references on how to use this, but  
have come up short. How does this parameter work and where do you use it in code?

 ![which](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/3X/9/a/9a920c175e238d337837d50853ee2a0b666bdd4e.png)

I know I’ll probably be ask. I’m tinkering with an idea for automatically detecting com ports and this seems like it might be useful when dealing with an array of Serials. I’m not ready to share any code just yet, as I said, I’m just tinkering right now.

---

<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:** [November 12, 2022, 8:04pm UTC](https://discourse.processing.org/t/serial-events-and-the-which-parameter/39638/2 "2022-11-12T20:04:03Z")

</div>

Hello @GhostGlitch,

A _ **working** _ example receiving data from 2 Arduinos:

```auto

import processing.serial.*; 

String com0, com1;
 
Serial myPort0, myPort1; // The serial port

void setup() 
  { 
  size(400,200); 
 
  // List all the available serial ports: 
  printArray(Serial.list()); 

  com0 = Serial.list()[4];
  myPort0 = new Serial(this, com0, 9600); 
  myPort0.bufferUntil('\n'); 
  
  com1 = Serial.list()[5];
  myPort1 = new Serial(this, com1, 9600); 
  myPort1.bufferUntil('\n');
  } 
 
void draw() 
  { 
  } 
 
void serialEvent(Serial whichPort) 
  { 
  if (whichPort.equals(myPort0))
    {
    String inString0 = myPort0.readString();
    println(com0, inString0);
    }
    
  if (whichPort.equals(myPort1))  
    {
    String inString1 = myPort1.readString();
    println(com1, inString1);
    }
  }  

```

Older reference with different wording that may help:  
[https://www.andrew.cmu.edu/course/60-257/reference/libraries/serial/index.html](https://www.andrew.cmu.edu/course/60-257/reference/libraries/serial/index.html)

Current reference:  
[https://processing.org/reference/libraries/serial/index.html](https://processing.org/reference/libraries/serial/index.html)

Processing Serial JavaDocs:  
[https://processing.github.io/processing-javadocs/libraries/processing/serial/Serial.html](https://processing.github.io/processing-javadocs/libraries/processing/serial/Serial.html)

Serial library on GitHub:  
[https://github.com/processing/processing4/tree/main/java/libraries/serial](https://github.com/processing/processing4/tree/main/java/libraries/serial)  
The source code is there for the adventurous.

`:)`

---

<div class="post-metadata">

**Author:** ![GhostGlitch](https://avatars.discourse-cdn.com/v4/letter/g/b4bc9f/32.png) [@GhostGlitch](https://discourse.processing.org/u/GhostGlitch)\
**Post date:** [November 17, 2022, 1:20am UTC](https://discourse.processing.org/t/serial-events-and-the-which-parameter/39638/3 "2022-11-17T01:20:34Z")

</div>

Thanks. I already know how to use more than one serial. I was asking about a feature that is mentioned.  
I have dug into the library and apparently this feature does not exist. The way it’s stated, there should be a built in function within serialEvent that would return the port name that called it. The library reference should probably remove this statement to avoid confusion, or at least clarify that calling Serial.list()[number] returns a unique id that you need to use to compare the ports.

---

<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:** [November 20, 2022, 12:59am UTC](https://discourse.processing.org/t/serial-events-and-the-which-parameter/39638/4 "2022-11-20T00:59:25Z")

</div>

Hello @GhostGlitch,

> [@GhostGlitch](#):
>
> I already know how to use more than one serial. I was asking about a feature that is mentioned.

The example I provided was to demonstrate the use of the _ **which** _ parameter that you asked about.

The serial resources could use a review and update.

The older reference for _serialEvent()_ states:

 ![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/3X/1/b/1bbe5898df17984e6d5952a83dce29982522d720.png)

The _ **which** _ parameter is _ **whichPort** _.

The current reference for _serialEvent()_ comments on the _ **which** _ parameter but does not use it here in the syntax or parameters:

 ![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/3X/f/f/ff700b436fadd73ab5716c1a874a3c47bd3dd664.png)

`:)`

---

<div class="post-metadata">

**Author:** ![GhostGlitch](https://avatars.discourse-cdn.com/v4/letter/g/b4bc9f/32.png) [@GhostGlitch](https://discourse.processing.org/u/GhostGlitch)\
**Post date:** [November 20, 2022, 1:58am UTC](https://discourse.processing.org/t/serial-events-and-the-which-parameter/39638/5 "2022-11-20T01:58:44Z")

</div>

I understand that. What I’m saying is that isn’t a parameter. You can get the same result with this code:

```auto
import processing.serial.*; 

String com0, com1;
 
Serial myPort0, myPort1; // The serial port

void setup() 
  { 
  size(400,200); 
 
  // List all the available serial ports: 
  printArray(Serial.list()); 

  com0 = Serial.list()[4];
  myPort0 = new Serial(this, com0, 9600); 
  myPort0.bufferUntil('\n'); 
  
  com1 = Serial.list()[5];
  myPort1 = new Serial(this, com1, 9600); 
  myPort1.bufferUntil('\n');
  } 
 
void draw() 
  { 
  } 
 
void serialEvent(Serial thisPort) 
  { 
  if (thisPort.equals(myPort0))
    {
    String inString0 = myPort0.readString();
    println(com0, inString0);
    }
    
  if (thisPort.equals(myPort1))  
    {
    String inString1 = myPort1.readString();
    println(com1, inString1);
    }
  }

```

The confusing thing is referencing that as a parameter. It makes it sound like there’s an easier way.  
I’m just trying to be helpful for the next person that comes across this, and trust me, there’s a lot of people before me that have asked and have gotten no answer at all. To which I do thank you.
