Sample Java program shows how to write to COM port using Java.

By: Johanes  

This Sample Java program shows how to write to COM port using Java.

If you are one of those living still in the 20th century then your JDK probably doesn't come with the Java COMM API. So you would have to download the Java Comm Api from http://java.sun.com/products/javacomm/. On the other hand if you are using one of the later versions of JDK then the Java Comm API should be inbuilt together with your JDK. Once you have this then create a sample program to read from the PC's COM port. And then copy and paste the below code into that program. Compile and run it. If the compilation fails then it is probably due to some classpath problem. You will have to add the javax.comm api to your classpath properly.

import java.io.*;
import java.util.*;
import javax.comm.*;

public class SimpleWrite {
    static Enumeration portList;
    static CommPortIdentifier portId;
    static String messageString = "Hello, world!\n";
    static SerialPort serialPort;
    static OutputStream outputStream;

    public static void main(String[] args) {
        portList = CommPortIdentifier.getPortIdentifiers();

        while (portList.hasMoreElements()) {
            portId = (CommPortIdentifier) portList.nextElement();
            if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
                 if (portId.getName().equals("COM1")) {
                //if (portId.getName().equals("/dev/term/a")) {
                    try {
                        serialPort = (SerialPort)
                            portId.open("SimpleWriteApp", 2000);
                    } catch (PortInUseException e) {}
                    try {
                        outputStream = serialPort.getOutputStream();
                    } catch (IOException e) {}
                    try {
                        serialPort.setSerialPortParams(9600,
                            SerialPort.DATABITS_8,
                            SerialPort.STOPBITS_1,
                            SerialPort.PARITY_NONE);
                    } catch (UnsupportedCommOperationException e) {}
                    try {
                        outputStream.write(messageString.getBytes());
                    } catch (IOException e) {}
                }
            }
        }
    }
}



Archived Comments

1. no output as well, i'm getting portlist and portId as NULL, so no loop
View Tutorial          By: raul at 2015-10-09 20:05:02

2. no output is shown
View Tutorial          By: shubham bansal at 2015-02-21 15:36:31

3. how to write an byte array containing 10 different elements to serial port and read it back in the s
View Tutorial          By: sanjana at 2015-01-17 06:32:41

4. Hi
I can compile it
root$ javac -cp .:comm-2.0.jar SimpleCOMPortWriter.java but it fa

View Tutorial          By: Tebogo Modiselle at 2014-10-31 08:47:09

5. import gnu.io.*;
import java.io.*;


public class ListPortClass implemen

View Tutorial          By: Anonymous at 2013-03-29 02:39:00

6. Hello,

When I put the source code in Ecplise I get the win32.dll error. Cannot run IA

View Tutorial          By: Pascal at 2013-03-25 09:12:47

7. Hello,
i was run your given program in eclipse and command prompt but it doesn't give any out

View Tutorial          By: vishnu chauhan at 2013-01-24 11:42:16

8. i have a shimmer accelerometer(bluetooth). i need to use java to send data to the shimmer to trigger
View Tutorial          By: jason at 2012-03-22 03:19:17

9. hai
i can run this program bt 1 exceptions returned plz help me


Erro

View Tutorial          By: Raheela at 2010-05-25 21:59:38

10. Hello,

I've got javax.comm and followed carefully the installation guide on Linux(ub

View Tutorial          By: Ashkan at 2009-12-19 21:55:23

11. Hello,

I've got javax.comm and followed carefully the installation guide on Linux(ub

View Tutorial          By: Ashkan at 2009-12-19 21:51:19


Most Viewed Articles (in Java )

Latest Articles (in Java)

Comment on this tutorial