recover from PortInUseException
in
Core Library Questions
•
1 year ago
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:
- public void initSerial () {
- try {
- serial = new Serial(this, Serial.list()[0], BAUD_RATE);
- serialInited = true;
- } catch (RuntimeException e) {
- if (e.getMessage().contains("<init>")) {
- System.out.println("port in use, trying again later...");
- serialInited = false;
- Timeout retrySerialTimeout = new Timeout(this, "initSerial", 5000);
- }
- }
- }
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.