Using a Serial port. Can processing boot even if the port is un available

I would want to be able to boot the sketch even if the USB cord is unplugged to my arduino.
Eventually I would write a message “COM Not Available” and maybe make a button to re-connect.

But Processing will stop in the setup.

So I tried:

 void setup(){
  size(400, 300, JAVA2D);

  
  //myPort0 = new Serial(this, "COM4", 9600);
  printArray(Serial.list());
  
  if (Serial.list().length > 0)
  {
  myPort = new Serial(this,Serial.list()[0], 9600);
  myPort.bufferUntil(lf); 
  //myPort = new Serial (this, "/dev/tty.wchusbserial5d10", 115200);
   myPort.clear();
  }
  else
  {myPort.stop();
  }
  
 
  f = createFont("Arial",46,true);
  
 
}

But no boot.
Please help,
Thanks

Try and catch this:

:)

Thanks a lot, that was clever, making a bool and conditioning everything on that.

I wrote something like,

if (Serial.list().length > 0)
  {
    serialConnect =true;
    myPort = new Serial(this, Serial.list()[0], 9600);
    myPort.bufferUntil(lf); 
    //myPort = new Serial (this, "/dev/tty.wchusbserial5d10", 115200);
    myPort.clear();
  } else
  {
    serialConnect =false;
  }

And then only listen to the serial port if the Serialconnect boolean is true.

Thanks a lot, Learned something new.

1 Like