How to use java to get the list of available serial ports in my computer?

I just run some code to get the list of available ports in my cmputer. When I have three free COM ports, it returns my error How can I solve this problem?

My code:

public static void main(String[] args) {
        //SerialParameters params=new SerialParameters();
       // System.out.println(CommPortIdentifier.PORT_SERIAL );
        Enumeration portList = CommPortIdentifier.getPortIdentifiers();
        System.out.println(portList.hasMoreElements());
        while(portList.hasMoreElements()){
            System.out.println("Has more elements");
             CommPortIdentifier portId = (CommPortIdentifier) portList.nextElement();
               if (portId.getPortType() == CommPortIdentifier.PORT_PARALLEL) {
                    System.out.println(portId.getName());
               }
               else{
                     System.out.println(portId.getName());
               }

        }
}

Output: false

Solution

Your javax Comm API settings may be incorrect Make sure you have done the following:

>Place the comm.jar file in the JRE / lib / ext directory. > Javax The comm.properties file is placed in the JRE / lib directory. > Will win32com DLL in JRE / bin directory

Each of the above components "should" can be used here

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>