Search code examples
javaserial-portmodbus

List all serial port in java


I have created an application in java for Modbus hardware device. I can install my application on Windows system which works fine. But I am facing Issue when I tried to use my application Linux operating system. The reason behind this issue is serial port, because of which I am not able to connect my application with the hardware other than Windows operating system. Now I want to find the list of serial ports(operating system wise) & add them into the JComboBox at the place of comport(refer below image). I need to distribute this application on Windows, Linux, MacOS. I can detect the operating system through my code. I have checked a lot resources, but none of them were useful to me.

Need some help to detect the serial port of the system.

Thanks in advance.

Connection establishment page


Solution

  • This code might help you. It will give you the list of connected devices at serial port. Use https://fazecast.github.io/jSerialComm/ library to run this code.

    public class Test {
        public static void main(String[] args) {
            SerialPort[] ports = SerialPort.getCommPorts();
            List<String> list = new ArrayList<String>();
            for (SerialPort port : ports) {
                System.out.println(port.getSystemPortName());
                list.add(port.getSystemPortName());
            }
            System.out.println("List of serial port is : "+list);
        }
    }