Serial I/O with Java under Windows





Serial Port I/O RS232
I was developing an application in which I needed to communicate to a device via a serial port on a PC. The OS that I had was Windows XP.

Tthe latest version of the Java Communications API is 3.0. The only problem is that if you want to deploy to a windows platform then you will be unable to use it.

Quoting Sun's Java Communications 3.0 API :
"Implementations of the API are currently available for Solaris SPARC, Solaris x86, and Linux x86."

No mention of Windows. I was unable to find an older version of the API which did work on windows so I had to look elsewhere.

The solution came from Trent Jarvi's RXTX package. Go to the downloads section to get the files relevant to your platform.

To install on windows:

  •  unzip,
  •  copy rxtxSerial.dll to %JAVA_HOME%\bin,
    (%JAVA_HOME% is the folder where JRE is installed on your system; e.g. C:\Program Files\Java\jre1.5.0_06)
  •  copy RXTXcomm.jar to %JAVA_HOME%\lib\ext

For other packages check out the INSTALL file in the zip archive.

To use it in your Java source, place "import*;" in your source file. You can use Sun's API to figure out what to do with it.

There is a good demo called SerialDemo which demonstrates how to use serial I/O. Just change the import statement. Do not forget to include the JAR file into your library path in your IDE.