In Processing, size() matters for Serial ports

Mark Allen of Machine Project is teaching an Arduino course using some of the notes from my Bionic Arduino class. He and his students were seeing in Windows XP & 2000, when trying to getting Processing to talk to Arduino, the cryptic error:

  gnu.io.PortInUseException: Unknown Application
               at
  gnu.io.CommPortIdentifier.open(CommPortIdentifier.java:354)

Both his class and Mark and I separately spent a lot of time trying to figure out what the problem was. Eventually we discovered that it’s because the sketch I had written had a setup() like this:

  void setup() {
    port = new Serial(this, portname, 19200);
    size(400,400);
  }

but instead should have been like this:

  void setup() {
    size(400,400);
    port = new Serial(this, portname, 19200);
  }

Yes, size() must come before new Serial() on Windows or it will not work. There is a bug report describing a similar problem and is marked RESOLVED, INVALID because as the reference documentation for size() states, “The size() function must be the first line in setup()”.

I’ve been using Processing for a long time and I found this feature of size() surprising. I always figured size() to be just a dimensioning function, not a critical part of sketch startup. I’ve seen many sketches that apparently function correctly where size() is located outside of setup() or is not the first statement in setup(). Perhaps the Processing sketch parser should check the sketch before running it to make sure not statements come before size().

I apologize to any Windows users attempting to use the Processing sketches I wrote for Bionic Arduino. I’ve updated the sketches appropriately so the above issue doesn’t come up.

Leave a Reply

Your email address will not be published. Required fields are marked *