title: How to initialise a serial port using java?
body: <p>I know this question has been asked many times and i've looked through the top 20 search result from google and i still cant get my code to work. </p>
  
  <pre><code>portList = CommPortIdentifier.getPortIdentifiers();
  System.out.println("portList... " + portList);
  while (portList.hasMoreElements()) {
      portId = (CommPortIdentifier) portList.nextElement();
      if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
          System.out.println("port identified is Serial.. "+ portId.getPortType());
          if (portId.getName().equals("COM2")) {
              System.out.println("port identified is COM2.. "+ portId.getName());  
              SimpleRead reader = new SimpleRead();
          } else {
              System.out.println("unable to open port");
          }
      }else{
          System.out.println("pordId.PortType = "+portId.getPortType());
          System.out.println("CommPortIdentifier.PORT_SERIAL = "+CommPortIdentifier.PORT_SERIAL);
      }
  }
  </code></pre>
  
  <p>The output i'm getting is </p>
  
  <pre><code>run:
  </code></pre>
  
  <p>portList... javax.comm.CommPortEnumerator@18020cc
  BUILD SUCCESSFUL (total time: 0 seconds)</p>
  
  <p>Does anyone know what is wrong? why doesnt portId have more elements? the program doesnt enter the while loop.</p>
  
  <p>Thanks in advance for reading this post.</p>