Saturday, August 2, 2014

Using Java RxTx library for Serial Communication With Arduino - Part 1

Hi All,

Today I'm going to show you how you can use the Java RxTx library for Serial Communication with Arduino. This small java program is just to identify from which port your Arduino is connected to the PC. So this will be a good start if you are going to use RxTx library with Arduino. 

First you have to download the RxTx library from one of these locations. 
I used the link 1 to get the library for Windows x64. You can download x86 or x64 version according to your Operating System. 

Now we have to install RxTx in our program. You can do this in two ways. Both of them are easy, lets try the first one. I'm doing this with eclipse. 

First create your project in eclipse. 

Add a new class. I will name it ""

Now lets add the RxTx library to the project. Extract the downloaded file and add it to the project using Right Click the Project -> Build Path -> Configure Build Path -> Libraries -> Add External JARs -> OK

Now Copy the two dll files (rxtxParallel.dll, rxtxSerial.dll) from the extracted library and paste it to the project folder. 

Now you are ready for coding. So use the following code. 

Now Run the program. My Arduino was connected to COM7 and here's the output. 

You can check the Part 2 here

Hope this was helpful. 

Thank You. :-) 

Reference : 


  1. Thanks for detailed post. Take a look at this library alternative to RXTX

    1. You are most welcome :-). This is great. It would be helpful for others too.Thanks for sharing.

  2. good one its works Thank you very much yaar!

  3. After seeing the problems and struggling with RXTX and JSSC for a while when I was working with the Arduino, I developed the JAVA Arduino Communication Library

    While I developed the library with the Arduino board specifically in mind, it can really be used to read and write effortlessly with any serial device.

    My library has (hopefully) great documentation and tells you exactly how to install and use it. After downloading both JARs in the library and including them in your classpath, just include the following statement before your class definition:

    import arduino.*;

    Now, all you'll have to do is know the serial port with which you want to communicate, and the baud rate at which you wish to communicate, and you're good to go with the following example code.

    String portName = ""; //Your port name here

    int BAUD_RATE = 9600; //Baud rate for communication

    Arduino anySerialDevice = new Arduino(portName, BAUD_RATE); //Even though the class is called 'Arduino', you can use this to connect to ANY serial device


    anySerialDevice.serialWrite('1'); //serialWrite is an overridden method, allowing both characters and strings.

    anySerialDevice.serialWrite('1', 20); //its second parameter even allow delays. more details can be found in the documentation.

    anySerialDevice.serialRead(12); //reads the specified no. of characters from the serial.


    The library also comes with example code that I thought would help speed along the process. I have tested the library on both Windows and Mac OSX and works reliably.

    PS: While the class is called Arduino, to simplify reading the code for novice arduino developers, as I have mentioned before, the use of my library can be extended to any serial device.

  4. HI,
    I used example 1 and 2 now i want to send serial data on my local ApacheMQ broker ...for this communication i already test an example but i dont have java skills so please help me .and please look the below code

    import javax.jms.Connection;
    import javax.jms.ConnectionFactory;
    import javax.jms.Destination;
    import javax.jms.JMSException;
    import javax.jms.MessageProducer;
    import javax.jms.Session;
    import javax.jms.TextMessage;
    import org.apache.activemq.ActiveMQConnection;
    import org.apache.activemq.ActiveMQConnectionFactory;
    public class Sender {

    private ConnectionFactory factory = null;
    private Connection connection = null;
    private Session session = null;
    private Destination destination = null;
    private MessageProducer producer = null;

    public Sender() {


    public void sendMessage() {

    try {
    factory = new ActiveMQConnectionFactory(
    connection = factory.createConnection();
    session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
    destination = session.createQueue("SAMPLEQUEUE");
    producer = session.createProducer(destination);
    TextMessage message = session.createTextMessage();
    message.setText("Hello ...This is a sample message..sending from FirstClient");
    System.out.println("Sent: " + message.getText());

    } catch (JMSException e) {

    public static void main(String[] args) {
    Sender sender = new Sender();


    Thanks regards,

  5. Could u please guide with this. This error is shown when i did the same as mentioned above.

    java.lang.UnsatisfiedLinkError: D:\Users\H224707\workspace\RxTx-Demo\rxtxSerial.dll: Can't load AMD 64-bit .dll on a IA 32-bit platform thrown while loading
    Exception in thread "main" java.lang.UnsatisfiedLinkError: D:\Users\H224707\workspace\RxTx-Demo\rxtxSerial.dll: Can't load AMD 64-bit .dll on a IA 32-bit platform
    at java.lang.ClassLoader$NativeLibrary.load(Native Method)
    at java.lang.ClassLoader.loadLibrary0(Unknown Source)
    at java.lang.ClassLoader.loadLibrary(Unknown Source)
    at java.lang.Runtime.loadLibrary0(Unknown Source)
    at java.lang.System.loadLibrary(Unknown Source)
    at FirstSteps.main(

    1. Hi, Is your OS 32-bit or 64-bit? Have you used the correct .dll for your OS? If not try downloading the correct dll and execute the program. Use the following link to download the dll.