Java - Writing to serial port using Java Communication API




import java.io.IOException;
import java.io.OutputStream;
import java.util.Enumeration;
import javax.comm.CommPortIdentifier;
import javax.comm.PortInUseException;
import javax.comm.SerialPort;
import javax.comm.UnsupportedCommOperationException;

/**
 *
 * @author dhanoopbhaskar
 */
public class SerialPortWriter {

    Enumeration portList;
    CommPortIdentifier portIdentifier;
    String message;
    SerialPort serialPort;
    OutputStream outputStream;
    String portName = "";

    public SerialPortWriter(String portName) {
        this.portName = portName;
        /**
         * gets a list of ports
         */
        portList = CommPortIdentifier.getPortIdentifiers();


        while (portList.hasMoreElements()) {

            portIdentifier = (CommPortIdentifier) portList.nextElement();
            /**
             * check whether the port is serial
             */
            if (portIdentifier.getPortType() == CommPortIdentifier.PORT_SERIAL) {

                /**
                 * check whether the port name is portName (eg.COM1)
                 */
                if (portIdentifier.getName().equals(portName)) {
                    try {
                        /**
                         * String - application name
                         * int - time out value
                         */
                        serialPort = (SerialPort) portIdentifier.open("SerialPortWriter", 2000);
                    } catch (PortInUseException ex) {
                        System.out.println("#1 " + ex);
                    }

                    try {
                        outputStream = serialPort.getOutputStream();
                    } catch (IOException ex) {
                        System.out.println("#2 " + ex);
                    }

                    try {
                        /**
                         * int baudrate
                         * int dataBits
                         * int stopBits
                         * int parity
                         */
                        serialPort.setSerialPortParams(9600,
                                SerialPort.DATABITS_8,
                                SerialPort.STOPBITS_1,
                                SerialPort.PARITY_NONE);

                        serialPort.setDTR(true);
                    } catch (UnsupportedCommOperationException ex) {
                        System.out.println("#3 " + ex);
                    }
                }
            }
        }
    }

    public void sendMessage(String message) {

        try {
            outputStream.write(message.getBytes());
        } catch (IOException ex) {
            System.out.println(ex);
        }
    }

    public void close() {
        serialPort.close();
    }
}






To test the above program you need to download Java Communication API.



After the download has completed extract the archive and you will get the following files, and it is very important to have them installed on your system for a proper operation:
  • comm.jar
  • win32com.dll
  • javax.comm.properties
comm.jar should be placed in:

    %JAVA_HOME%/lib

    %JAVA_HOME%/jre/lib/ext

win32com.dll should be placed in:

    %JAVA_HOME%/bin

    %JAVA_HOME%/jre/bin

    %windir%System32

javax.comm.properties should be placed in:

    %JAVA_HOME%/lib

    %JAVA_HOME%/jre/lib
   

Post a Comment

0 Comments