1

I want to scan available com port using Java. I used the following code using Comm library but its not working

java.util.Enumeration<CommPortIdentifier> portEnum = CommPortIdentifier.getPortIdentifiers();
int i = 0;
String[] r = new String[10];
while (portEnum.hasMoreElements()){
    CommPortIdentifier portIdentifier = portEnum.nextElement();
    r[i] = portIdentifier.getName();
    i++;
}
jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(r));

portEnum.hasMoreElements() returns false every time.

I don't have the old fashioned RS232 port on my computer I'm using a USB to RS232 converter that I tested with putty.

I just want to scan available ports so I don't mind using others libraries.

ANSWER: I Used the code in comments bellows and its worked

import jssc.SerialPortList;

public class Main {

    public static void main(String[] args) {
        String[] portNames = SerialPortList.getPortNames();
        for(int i = 0; i < portNames.length; i++){
            System.out.println(portNames[i]);
        }
    }
}
B.Letz
  • 136
  • 1
  • 16
MrsIl
  • 53
  • 1
  • 5
  • Do you have everything set up properly as explained here: https://stackoverflow.com/questions/15955530/commportidentifier-getportidentifiers-is-empty – Sven Aug 29 '17 at 08:14
  • Yes I Have all the files in the right directories. – MrsIl Aug 29 '17 at 08:29

1 Answers1

2

Which java version are you using? There might be problems with jdk 8.

Have you tried https://code.google.com/archive/p/java-simple-serial-connector/

Example usage:

import jssc.SerialPortList;

public class Main {

    public static void main(String[] args) {
        String[] portNames = SerialPortList.getPortNames();
        for(int i = 0; i < portNames.length; i++){
            System.out.println(portNames[i]);
        }
    }
}

For more examples visit https://code.google.com/archive/p/java-simple-serial-connector/wikis/jSSC_examples.wiki

Sven
  • 36
  • 4
  • Whilst this may theoretically answer the question, [it would be preferable](//meta.stackoverflow.com/q/8259) to include the essential parts of the answer here, and provide the link for reference. – GhostCat Aug 29 '17 at 09:32