How to list all COM ports in Combobox or Listbox and allow user to select the desired COM Port

edited January 2017 in Library Questions

How to list all COM ports in Combobox or Listbox and allow user to select the desired Port speed.

Answers

  • Answer ✓

    You can install the controlP5 library or use G4P builder library using the Processing's library manager. You can check the provided examples and their websites:

    http://www.sojamo.de/libraries/controlP5/
    http://www.lagers.org.uk/g4p/

    Kf

  • Thank you very much.

  • edited January 2017

    Can you advise on how to define Maximum number of visible items when DropDown is clicked. As of now it is showing only one item

  • You didn't mention what kind of dropdown you are using, cp5 or g4p? When you have problems it helps a lot if you post the current version of your sketch or some example that shows the problem. Otherwise we have to guess what you tried and that might not be very helpful.

    Anyways, here is a simple example using cp5:

    import processing.serial.*;
    import controlP5.*;
    
    ControlP5 cp5;
    
    void setup() {
      size(400, 400);
    
      cp5 = new ControlP5(this);
      cp5.addScrollableList("port")
         .setPosition(100, 100)
         .setSize(200, 200)
         .setBarHeight(20)
         .setItemHeight(20)
         .addItems(Serial.list())
         ;
    }
    
    void draw() {
      background(240);
    }
    
    void port(int n) {
      println(cp5.get(ScrollableList.class, "port").getItem(n).get("text"));  
    }
    
  • edited January 2017

    Sorry, Missed out on that sir. here is the code. I am unable to use the selected Item or default Item set using lstPortList.setvalue(0);

    `    //Imports
        import processing.serial.*;
        import controlP5.*;
        import java.util.*;
    
        //Variable Declarations
        ControlP5 lstPortList;
        ControlP5 lstBaud;
        String tport;
        int tbaud;
        Serial myPort;      // The serial port
        int whichKey = -1;  // Variable to hold keystoke values
        int inByte = -1;    // Incoming serial data
        int Indexport =0;
        int Indexbaud =1;
    
        ///Setup/////
        void setup() {
          size(400, 400);
          // create a font with the third font available to the system:
          PFont myFont = createFont(PFont.list()[2], 14);
          textFont(myFont);
    
          // List all the available serial ports:
          // printArray(Serial.list());
    
          lstPortList = new ControlP5(this);
          String[] portNames =Serial.list();
          lstPortList.addScrollableList("Port")
            .setPosition(10, 10)
              .setSize(100, 100)
                .setBarHeight(20)
                  .setItemHeight(20)
                    .addItems(portNames)
                      .setFont(createFont("Arial", 13))
                        .setColorForeground(color(40, 128))
                          .setValue(Indexport);
    
          lstBaud = new ControlP5(this);
          List BaudList = Arrays.asList("4800", "9600", "19200", "38400", "57600", "115200");
          lstBaud.addScrollableList("Baud")
            .setPosition(200, 10)
              .setSize(100, 100)
                .setBarHeight(20)
                  .setItemHeight(20)
                    .addItems(BaudList)
                      .setFont(createFont("Arial", 13))
                        .setColorForeground(color(40, 128))
                          .setValue(Indexbaud);
          tport= port(Indexport);//lstPortList.get(ScrollableList.class, "port").getItem(Indexport).get("text");
          tbaud= baud(Indexbaud); //lstBaud.get(ScrollableList.class, "port").getItem(Indexbaud).get("text");
          myPort = new Serial(this, tport, tbaud );
        }
    
        void draw() { 
    
          background(0);
          text("Last Received: " + inByte, 10, 130);
          text("Last Sent: " + whichKey, 10, 100);
        }
    
        void serialEvent(Serial myPort) {
          inByte = myPort.read();
        }
    
        void keyPressed() {
          // Send the keystroke out:
          myPort.write(key);
          whichKey = key;
        }
        void controlEvent(ControlEvent theEvent) {
          if (theEvent.isGroup()) {
            // get name of item sending an event
            println("My name is: " + theEvent.getGroup().getCaptionLabel().getText());
          }
        }
    
        String port(int n) {
          return lstPortList.get(ScrollableList.class, "Port").getItem(n).get("text");  
        }
        int baud(int n) {
          return lstBaud.get(ScrollableList.class, "Baud").getItem(n).get("text");  
        }`
    

    Thanks.

  • I am using CP5 and am trying since morning to list all the COM ports and Baud Speeds and allow user to select and then should connect to that COM port at that Baud.

  • Answer ✓

    Your code is barely readable, please format your code, edit post, select code and hit ctrl+o.

    It doesn't really make sense to have a list to select the port but set it right away in setup() before you have a chance to select it.

    But anyways, you don't need two instances of controlP5, one is enough. Then you could store each controller in a variable and then access it later if needed.

    Example:

    import processing.serial.*; 
    import controlP5.*; 
    
    ControlP5 cp5; 
    ScrollableList portList;
    
    void setup() { 
      size(400, 400); 
    
      // initiate cp5-library
      cp5 = new ControlP5(this); 
    
      String[] portNames =Serial.list(); 
      portList = cp5.addScrollableList("COM Port") 
        .setPosition(10, 10) 
        .setSize(200, 100) 
        .setBarHeight(20) 
        .setItemHeight(20) 
        .addItems(portNames) 
        .setValue(0);
    
      println(portList.getCaptionLabel().getText());
    }
    
    void draw() {
      background(0);
    }
    
  • edited January 2017

    Thanks. Code has improved. But need to give option to users to change COM port and Baud. May I request for help on selection change code.

    `
    //Imports
    import processing.serial.*;
    import controlP5.*;
    import java.util.*;
    
    //Variable Declarations
    ControlP5 cp5;
    ScrollableList PortList;
    ScrollableList BaudList;
    String tport;
    int tbaud;
    Serial myPort;      // The serial port
    int whichKey = -1;  // Variable to hold keystoke values
    int inByte = -1;    // Incoming serial data
    int Indexport =0;
    int Indexbaud =1;
    
    ///Setup/////
    void setup() {
      size(400, 400);
      // create a font with the third font available to the system:
      PFont myFont = createFont(PFont.list()[2], 14);
      textFont(myFont);
    
      // List all the available serial ports:
      // printArray(Serial.list());
    
      cp5 = new ControlP5(this); 
      String[] portNames =Serial.list();
    
      PortList = cp5.addScrollableList("Port") 
        .setPosition(10, 10) 
          .setSize(100, 100) 
            .setBarHeight(20) 
              .setItemHeight(20) 
                .addItems(portNames) 
                  .setFont(createFont("Arial", 13))
                    .setColorForeground(color(40, 128))
                      .setValue(Indexport);
      List BList = Arrays.asList("4800", "9600", "19200", "38400", "57600", "115200");
      BaudList= cp5.addScrollableList("Baud")
        .setPosition(200, 10)
          .setSize(100, 100)
            .setBarHeight(20)
              .setItemHeight(20)
                .addItems(BList)
                  .setFont(createFont("Arial", 13))
                    .setColorForeground(color(40, 128))
                      .setValue(Indexbaud);
    //  tport= port(Indexport);//lstPortList.get(ScrollableList.class, "port").getItem(Indexport).get("text");
    //  tbaud= baud(Indexbaud); //lstBaud.get(ScrollableList.class, "port").getItem(Indexbaud).get("text");
      myPort = new Serial(this,  PortList.getCaptionLabel().getText(), int (BaudList.getCaptionLabel().getText()));
    }
    
    void draw() { 
    
      background(0);
      text("Last Received: " + inByte, 10, 130);
      text("Last Sent: " + whichKey, 10, 100);
    }
    
    void serialEvent(Serial myPort) {
      inByte = myPort.read();
    }
    
    void keyPressed() {
      // Send the keystroke out:
      myPort.write(key);
      whichKey = key;
    }
    
    `
    
  • Answer ✓

    You can use the function "controlEvent()" that is called by all cp5-controllers.

    void controlEvent(ControlEvent theEvent){
      if(theEvent.getController() == portList){
        println("port selected");
      }
    
      if(theEvent.getController() == baudList){
        println("baud rate selected");
      }
    }
    

    Notice that i changed your variable-names to start with lower-case. That's just a convention to distinguish classes from variables.

    You would then have to check if the selected value has changed. Then, if you already have an open serial port you would want to close it and open a new one with the selected parameters.

  • Thank you so much for all the help.

Sign In or Register to comment.