Loading...
Logo
Processing Forum
i'm having a problem with PortInUseException sometimes being thrown by Serial when i run an application using the Serial lib on startup.

my workaround is to attempt repeatedly to initialize the serial object if the initialization fails.  basically:

Copy code
  1. public void initSerial () {
  2.       try {
  3.             serial = new Serial(this, Serial.list()[0], BAUD_RATE);
  4.             serialInited = true;
  5.       } catch (RuntimeException e) {
  6.             if (e.getMessage().contains("<init>")) {
  7.                   System.out.println("port in use, trying again later...");
  8.                   serialInited = false;
  9.                   Timeout retrySerialTimeout = new Timeout(this"initSerial", 5000);
  10.             }
  11.       }
  12. }


that Timeout class is my own class; you can use any method you want to re-try Serial initialization (e.g. a frame counter in draw()).  the serialInited boolean is a flag i check in draw() -- if !serialInited, i just draw a big red X.  once serialInited = true, i know Serial was successfully inited, so i continue on with my program.

just a little tidbit in case it helps someone else down the road.