How to verify the Serial.list()

edited December 2015 in Arduino

Hi guys, i'm trying to figure out how can i know what are the ports in the serial array. I'm using this code:

    while(i<Serial.list().length&&!trovato){
              if((Serial.list()[i]=="COM1")||(Serial.list()[i]=="COM2")||(Serial.list()[i]=="COM3")||(Serial.list()[i]=="COM4")||(Serial.list()[i]=="COM5"))

The if condition is always false, but if i print the Serial.list() array i can see that i have a COM4 and the lenght is 1. I need to use that because i don't know if arduino will be connected on another COM on an other PC.

The full if is

        int i=0;
                while(i<Serial.list().length&&!trovato){
                  if((Serial.list()[i]=="COM1")||(Serial.list()[i]=="COM2")||(Serial.list()[i]=="COM3")||(Serial.list()[i]=="COM4")||(Serial.list()[i]=="COM5")){
                    pos=i;
                    trovato=true;
                  }
                  else
                  i++;
                }
                if(trovato){
                  porta= new Serial(this, Serial.list()[pos], 9600);
                  porta_connessa=true;
                }
                else
                  porta_connessa=false;

Thanks

Answers

  • edited December 2015 Answer ✓

    In order to check String we need to rely on its equals() method: :-B
    https://Processing.org/reference/String_equals_.html

    No need to invoke Serial.list() more than once. Just cache its result in some variable. :-\"
    String[] ports = Serial.list();

    Below's a simple template w/ some Serial "tricks": *-:)

    // forum.Processing.org/two/discussion/13849/how-to-verify-the-serial-list
    // GoToLoop (2015-Dec-08)
    
    import processing.serial.Serial;
    
    String msg;
    
    void setup() {
      noLoop();
    
      String port = findPort();
    
      if (port != null)  new Serial(this, port, 9600).bufferUntil(ENTER);
      else               exit();
    }
    
    void draw() {
      println(msg);
    }
    
    void serialEvent(Serial s) {
      msg = s.readString().trim();
      redraw = true;
    }
    
    static final String findPort() {
      String[] ports = Serial.list();
      println(ports);
      println(ports.length);
    
      for (String p : ports)  for (int i = 1; i <= 5; ++i)
        if (p.equals("COM" + i))  return p;
    
      return null;
    }
    
  • Thank You so much

  • I've used this method and it works very well but i have a problem now. When the sketch find the port "COMx" and execute:

    port= new Serial(this, "COMx", 9600);

    i have an error : "The port COMx is busy" Does it depend on arduino? ora have i missed something in the sketch? I had nothing (program or other things) opened on the pc.

  • edited December 2015

    I don't have the hardware. Serial.list() just returns length = 0 here.
    Sorry I can't help ya out w/ that. :(

  • One reason could be another application, that blocks the arduino-port. For example you cannot use the serial-port while you have arduino's Serial Monitor open.

    Then you sometimes have more than one Serial-port on your system. Did you verify that you are really selecting the arduino-port? You can verify with

    import processing.serial.*;
    printArray(Serial.list());
    

    Then another note on your method to find the right serial-port:
    I think on windows the name for all serial-ports start with "COM" follwed by any number. And the arduino-port is not always between 1 and 5, i've definetly seen higher numbers.

    So if you have only one port, you can just use

    port= new Serial(this, Serial.list()[0], 9600);

    But otherwise, you would have to set it manually or try to connect to all serial-ports available and implement some kind of hand-shake with your arduino, to verify, that you found the right port.

Sign In or Register to comment.