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

By: Johanes Viewed: 164437 times  Printer Friendly Format    


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) {}
                }
            }
        }
    }
}


Most Viewed Articles (in Java )

Latest Articles (in Java)

Comment on this tutorial

Subscribe to Tutorials

Related Tutorials

Archived Comments

1. Hello,

I've got javax.comm and fol

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

2. Hello,

I've got javax.comm and fol

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

3. hai
i can run this program bt 1 exception

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

4. i have a shimmer accelerometer(bluetooth). i need
View Tutorial          By: jason at 2012-03-22 03:19:17

5. Hello,
i was run your given program in ecli

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

6. Hello,

When I put the source code i

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

7. import gnu.io.*;
import java.io.*;
<

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

8. Hi
I can compile it
root$ javac -cp

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

9. how to write an byte array containing 10 different
View Tutorial          By: sanjana at 2015-01-17 06:32:41

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

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