Serial Events and the which parameter

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?

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.

1 Like

Hello @GhostGlitch,

A working example receiving data from 2 Arduinos:


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

Current reference:
https://processing.org/reference/libraries/serial/index.html

Processing Serial JavaDocs:
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
The source code is there for the adventurous.

:)

1 Like

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.

Hello @GhostGlitch,

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:

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:

:)

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

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.

1 Like