Thursday, February 25, 2010

Reading data from serial port in JAVA (using javax.comm package)

For accessing serial port (COM1 OR COM2 ) through JAVA you need to install java communication API using which you can access javax.comm package .
Download the API package from here:-

The following files are the core of JAVA Communiccation API, and they are very important to have them installed on your system for a proper operation:

    * comm.jar
    * win32com.dll

For the jdk (Java Developnment Kit) to recognize the serial ports on your machine, it is important to properly place these files in the right folders on your local machine :
comm.jar should be placed in:

   jdk directory/lib

   jdk directory/jre/lib/ext

win32com.dll should be placed in:

    jdk directory/bin

    jdk directory/jre/bin

    Windows/System32(Inside drive in which windows is installed) should be placed in:

    jdk directory/lib

   jdk directory/jre/lib

Now you can use the following code:-

Note that the above code is set for COM2
Please change it to COM1 if your computer supports that by simply replacing COM2 by COM1 in the code.(its COM2 in my case).
Also the above code reads data to console.
You can get it on any JAVA application just by accessing the  read buffer present in serialEvent function.
Still any confusion contact me at


No comments:

Post a Comment

customised by Vaibhav