Is there a way to know when a port is connected?

edited August 2015 in Library Questions

Hi guys!! I have the code bellow:

    import processing.serial.*; 

    PrintWriter output;
    Serial myPort;    // The serial port
    PFont myFont;     // The display font
    String inString;  // Input string from serial port
    int lf = 10;      // ASCII linefeed 
    char uno = ' '; //To compare data
    boolean hay_puerto = false; //True if there are ports on the list

    void setup() 
    { 
      size(400, 200); 
      output = createWriter("data.txt"); //Create txt file
      noLoop();
`//To know if there is a port `
      String[] s  = Serial.list();
      println(s.length);
      if (s.length!=0)
      {
        hay_puerto = true;
      } else
      {
        hay_puerto = false;
      }

`//If there is a port, start connection`
      if (hay_puerto)
      {
        myPort = new Serial(this, Serial.list()[0], 9600); 
        myPort.bufferUntil(lf);
      } else
      {
        println("No serial port connected");
      }
    } 

    void draw() 
    { 
      background(0); 
      text("received: " + inString, 10, 50);
    } 

    void serialEvent(Serial p) 
    {
      uno = myPort.readChar();
      if (uno == '$')
      {
        inString = myPort.readString();

        if (inString != null) 
        {
          println(inString);
          output.println(inString);
        }
      } else
      {
        myPort.clear();
      }
      //inString = p.readString();
      redraw();
    }

    void keyPressed()
    {
      if (key=='x')
      {
        output.flush();  //writes data in the file
        output.close();  //Closes document
        exit();
      }
    }

My question is: Is there another way to know if the port is available? I mean, in my code I check for the size of the port list, and if it is zero it means that there is no port. If I don't do this, the program gets an error because there is no port. The problem is happen when I connect the port (while running the program), my program does not detect it so it does not do anything when I send something.

I hope my question is clear, and thanks :).

Answers

  • You check for the availability in setup() that executes just once after the program starts, so there is no way for your program to know if you've connected during the runtime.

  • edited August 2015

    So, Do I have to check into the draw function??

  • Answer ✓

    Try that, but keep in mind that it is not a good idea to assign new Serial object each frame, so rather then checking if serial list is >0, you need to also check if it has changed since previous frame.

  • Ok, I will try and say if that works :)

  • Thank you again, it worked really well. I will post the code later. I have to fix some issues :P.

  • Is it possible to know if the port has been disconnected?? (Is it the same way as checking if the port is connected?)

  • forget about it, I already fix it :P

  • Good job ;)

  • This is a functions that search if a port is conected:

    boolean buscaPuertos(String nombrePuerto)
    {
      fill(0);
      String[] s = Serial.list(); //Gets the list of connected ports
      //println(s);
      boolean existePuerto = false; //To know if a port exists
    //To know if there is any port in the list
      if (s.length > 0)
      {
       //to search the name of a specific port in the list
        for (String c : s)
        {
          if (c.equals(nombrePuerto))
          {
            println("Exist", 10, 10);
            existePuerto = true;
            return existePuerto;
          } else
          {
            println("Doesnt exist", 10, 10);
            existePuerto = false;
            return existePuerto;
          }
        }
      } else
      {
        println("There is not any port connected", 10, 10);
        existePuerto = false;
        return existePuerto;
      }
      return existePuerto;
    }
    

    I hope it would be useful :D.

Sign In or Register to comment.