On my latest project, i need to connect my java application to a GSM Modem, in order to sending and receiving SMS text. But before my application connected to opened comm port, i need to check which port number is opened. So this is a small java class to check my opened comm port. Btw, im using rtxt libraries to help me communicate with the opened port.
import gnu.io.CommPortIdentifier; import java.util.Enumeration; public class Main { private void doSomething() { Enumeration portList = CommPortIdentifier.getPortIdentifiers(); System.out.println(portList.hasMoreElements()); while (portList.hasMoreElements()) { CommPortIdentifier portId = (CommPortIdentifier) portList.nextElement(); System.out.println(portId.getName()); } } public static void main(String[] args) { Main main = new Main(); main.doSomething(); } }
Dont forget to add rxtx libraries your java’s path. You can find the Window’s installation path, here.
Hope it helps other, have fun 🙂