Working with COM Ports using JAVA

In this article, I’ll show you how to interact with the COM Ports of a machine from Java, many a times you need to perform some I/O on the ports, may be reading coordinates from  a GPS device, values from a Barcode scanner or may be your friend, an electronics guy wants you to help him in his project.

  • Dependencies

Working with COM Ports requires a library – javax.comm. I found one here . Download the library and extract it.

  • Setting up the environment
  1. Place the comm.jar on your project’s classpath
  2. Place the win32com.dll in <jdk>\jre\bin directory
  3. Place the javax.comm.properties in <jdk>\jre\lib.

Listing the available ports

import java.util.Enumeration;
import javax.comm.CommPortIdentifier;

public class ListAllPorts {

    public static void main(String args[]) {
        try {
            Enumeration portList = CommPortIdentifier.getPortIdentifiers();
            while (portList.hasMoreElements()) {
                CommPortIdentifier portId = (CommPortIdentifier) portList.nextElement();
                if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
                    System.out.println("Serial port: " + portId.getName());
                } else if (portId.getPortType() == CommPortIdentifier.PORT_PARALLEL) {
                    System.out.println("Parallel port: " + portId.getName());
                } else {
                    System.out.println("Other port: " + portId.getName());
                }
            }
        } catch (Exception ex) {
            System.out.println(ex);
        }
    }
}

Every machine may have different identifiers for the ports, so first know the available ports using the above program.

Reading from a Serial Port (RS-232)

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Enumeration;
import java.util.TooManyListenersException;
import javax.comm.CommPortIdentifier;
import javax.comm.PortInUseException;
import javax.comm.SerialPort;
import javax.comm.SerialPortEvent;
import javax.comm.SerialPortEventListener;
import javax.comm.UnsupportedCommOperationException;

public class ComPortRead implements SerialPortEventListener {

    CommPortIdentifier portId;
    Enumeration portList;
    SerialPort serialPort;
    BufferedReader bufferedReader;

    public ComPortRead() {
        try {
            portList = CommPortIdentifier.getPortIdentifiers();
            while (portList.hasMoreElements()) {
                portId = (CommPortIdentifier) portList.nextElement();
                if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
                    if (portId.getName().equals("COM11")) {
                        System.out.println("Serial port: " + portId.getName());
                        serialPort = (SerialPort) portId.open("GPSApp", 1900);
                        serialPort.setSerialPortParams(9600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
                        serialPort.addEventListener(this);
                        serialPort.notifyOnDataAvailable(true);
                        bufferedReader = new BufferedReader(new InputStreamReader(serialPort.getInputStream()));
                    }
                }
            }
        } catch (PortInUseException | TooManyListenersException | IOException | UnsupportedCommOperationException ex) {
            System.out.println(ex);
        }
    }

    @Override
    public void serialEvent(SerialPortEvent event) {
        String data;
        switch (event.getEventType()) {
            case SerialPortEvent.DATA_AVAILABLE:
                try {
                    while (((data = bufferedReader.readLine()) != null)) {
                        System.out.println(data);
                    }
                } catch (Exception ex) {
                    System.out.println(ex);
                }
                break;
        }
    }

    public static void main(String ar[]) throws Exception {
        ComPortRead comPortRead = new ComPortRead();
    }
}

One thought on “Working with COM Ports using JAVA

Leave a comment