USB (com port list)

edited August 2015 in Arduino

Hi all,

I'm working on a project with arduino and lately i got interested in processing too looks as a nice interface programming software which I'd like to use, so let's get to the main question. I'm totally new to processing so take it easy with me :D I saw some tutorials how to connect arduino with processing thru com port but I need to set my exact arduino com port and sometimes the com port changed depending on what computer i sit.

****Question: ****

  • Is there a way to get a drop menu with the available PC com ports and chose which one i need? It's not hard to change just a number in the code but i wanna make a drop down list with the available com ports so in this way i would not go and mess with the code every time picking ports.

Answers

  • edited August 2015

    This is how you find the list of COM ports, using Serial.list().

    void getSerialPorts() {
      COMList.setItems(Serial.list(), 0);
      if (Serial.list().length == 1) {
        println("Only one COM port seen");
        myPort = new Serial(this, Serial.list()[0], baudRate);
        PortOpen = true;
        myPort.bufferUntil('\n');
      }
      println(Serial.list());
    }
    

    Here if there is only one COM port I go ahead and open it as that will be the connected Arduino (hopefully).

    To create the GUI with a drop down list to allow the COM port to be selected I use quark's G4P (Available here)

    The COMList.setItems(...) instruction updates the list of available COMports in the drop list control.

    In case the sketch is run before the Arduino is connected, you may need to add a 'Scan' button that calls the getSerialPorts() function again as the list won't update unless you do it, but check to make sure the port is not already open ...

    public void Scan_click1(GButton source, GEvent event) { //_CODE_:Scanbutton:768853:
      println("Scanbutton - GButton event occured " + System.currentTimeMillis()%10000000 );
      if (PortOpen)
        myPort.stop(); // Stop the Serial port as an error will result if its already open when we re-scan and re-connect
      getSerialPorts();
    } //_CODE_:Scanbutton:768853:
    

    The COM port drop list object will have code something like this attached:

    public void COMList_click1(GDropList source, GEvent event) { //_CODE_:COMList:292706:
      println("COMList - GDropList event occured " + System.currentTimeMillis()%10000000 );
      //  myPort.stop(); // Close the port in case it's already open
      myPort = new Serial(this, Serial.list()[source.getSelectedIndex()], baudRate);
      myPort.bufferUntil('\n');
    } //_CODE_:COMList:292706:
    

    Hope this helps.

    Phil.

  • Well thanks, I'm kinda new in processing so I'm in the learning process so I'm sorry if i can't take things fast.

        import processing.serial.*;
    
        Serial myPort;
        void setup()
        {
          size(200,200);
          background(0,0,255);
        }
    
        void draw()
        {
    
        }
    
        void getSerialPorts() {
           COMList.setItems(Serial.list(), 0);
          if (Serial.list().length == 1) {
            println("Only one COM port seen");
            myPort = new Serial(this, Serial.list()[0], 9600);
            PortOpen = true;
            myPort.bufferUntil('\n');
          }
          println(Serial.list());
        }
    

    I tried this code but i got some errors: COMList not recognized PortOpen does not exist and Type String[] of the last argument to method println(Object...) doesn't exactly match the vararg parameter type.

  • Hi Domino60,

    Sorry for confusing you by just quoting snippets from my own code.

    PortOpen is a global Boolean variable. I declare it before the start of setup() boolean PortOpen = false;

    COMList is an object created by the GUI Builder that you should be using to create your GUI for use with the G4P library. Did you add GUI Builder using the Add Tool... option in the Tools menu of Processing? Once you create a GUI with a drop list, call it COMList and GUI Builder will create an object called COMList which has the method 'setItems' so you can update the drop list with the list of your active COM ports as in COMList.setItems(Serial.list(), 0);

    Above screen shot shows GUI Builder open and I've highlighted the drop list and on the right, you can see I named it COMList.

    The function public void COMList_click1(GDropList source, GEvent event) is created for you by the GUI Builder and it's called when an item in the COMList drop list is clicked on. The prinln call in there is also created by GUI Builder to help you know when that function has been called by printing the info in the debug area at the bottom of the Processing IDE window.

    I'm still pretty new to Processing myself so hopefully this won't confuse you even more!

    Phil.

Sign In or Register to comment.