If serial isn't available on startup

edited November 2015 in Arduino

I've been thinking about this problem for a couple weeks, came up with several possible solutions, but none have worked, so I'm looking for suggestions, ideas, or a good general direction to start thinking along.

I wrote a rather large applet that does a couple different things; sometimes, but not always, it will be connecting to an arduino via an HC-06 bluetooth module (the code is exactly the same as using serial through USB) . Sometimes I will need to run the program without connecting to serial. If I use Serial.list() it doesn't choose the right port, if the port is not available, the program doesn't run at all.
I need a method that connects if the port is available, but just ignores it and runs the program if it is not. Is there a function I can use to check the port before initializing, that way if it's not there i can tell my program not to use any of the portions that require serial communication? Thanks -J

Tagged:

Answers

  • edited November 2015 Answer ✓

    Serial.list() will give you a list of the serial-ports that are available. You should be able to check if your arduino-port is available like this:

    import processing.serial.*;
    String myPortName = "/dev/cu.usbmodemFD121";
    
    void setup(){
      if(Serial.list().length >=3 && Serial.list()[2].equals(myPortName)){
        println("arduino found");
      }else{
        println("arduino not found");
      }
    }
    

    you migt have to change the name and index, on my system the arduino shows up with this name as the third serial-port. I suppose it is different on your system.

Sign In or Register to comment.