How to get list of available serial ports in my pc using Java?
-
12-10-2019 - |
Question
I just run some codes to get a list of available ports n my cmputer and it returned me false when I have 3 com ports that are free. How do I solve this prob?
My codes:
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
It appears your setup of the javax.comm API may not be correct. Make sure you have done the following:
- Placed the
comm.jar
file in thejre/lib/ext
directory. - Placed the
javax.comm.properties
file in thejre/lib
directory. - Placed the
win32com.dll
in thejre/bin
directory.
Each of the above components "should" be available here.
OTHER TIPS
I'm using ubuntu and my computer does not have any serial/pararel port.
You need to simulate this ports in this case.
My answer:
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow