Arduino NetBeans and how to make it run

I have just spent last couple of hours(!) searching for example how to make Arduino working with JAVA and I’m bit frustrated.

There is an official example: Arduino Interfacing Java

BUT:

  • The web page with source libraries RXTX Java library is not available anymore! (at least I was not able to open it)
  • After another search I found that Arduino is NOT using these libraries anymore! Therefore the example is outdated and not working anymore.
  • I haven’t found any other official documentation regarding this issue, neither any step by step tutorial…

I hope I’m wrong and I miss some important official tutorial(s). But be honest, if someone is not able to find it after hours of searching, it means the documentation is not good.
And yes, I know the other possibility is, I’m just stupid. In that case, please show me where it is… Happy to see where I made the mistake.
:-\

Anyway, to be constructive and for others with the same problem, I will add How-To step by step tutorial here into this post.
My plan is to communicate with Arduino using JAVA, NetBeans in Windows 7 and MacOS

NetBeans settings
Arduino IDE is using JAVA to communicate with Arduino board therefore the best approach is to use the same library.
In the time of writing this thread, jSSC 2.8.0 library is used (Git).

  • Find the jSSC library JAR file in Arduino IDE instalation. \Arduino\lib\jssc-2.8.0.jar
  • Add the jSSC library to NetBeans project libraries:
    Right click on project
    Properties
    Libraries
    Add JAR/Folder

How to Read data from Arduino
Arduino code

void setup() {
  Serial.begin(9600);
}

void loop() {
  Serial.println("Test");
  delay(1000);
}

JAVA code

package Project;

import java.util.logging.Level;
import java.util.logging.Logger;

import jssc.SerialPort;
import jssc.SerialPortException;

public class Project {
    public static void main(String[] args) {
        String str;

        //In the constructor pass the name of the port with which we work
        SerialPort serialPort = new SerialPort("COM3");

        try {
            //Open port
            serialPort.openPort();

            //We expose the settings. You can also use this line - serialPort.setParams(9600, 8, 1, 0);
            serialPort.setParams(SerialPort.BAUDRATE_9600,
                    SerialPort.DATABITS_8,
                    SerialPort.STOPBITS_1,
                    SerialPort.PARITY_NONE);



            Thread.sleep(1000);//Very important !!!



            //Read data from port
            for (int i = 0; i < 10; i++) {
                str = serialPort.readString();

                if (str != null) {
                    System.out.println(str);
                }

                Thread.sleep(500);
            }

            //Closing the port
            serialPort.closePort();

        } catch (SerialPortException ex) {
            System.out.println(ex);
        } catch (InterruptedException ex) {
            System.out.println(ex);
        }
    }
}

How to write data to Arduino
Arduino code

void setup() {
  Serial.begin(9600);
}

void loop() {

  char i = Serial.read();
  if (i == 'o') {
    start = true;
  }
  else if (i == 'c') {
    start = false;
  }

  if (start == true) {
    // Do something
  }
}

JAVA code

package Project;

import java.util.logging.Level;
import java.util.logging.Logger;

import jssc.SerialPort;
import jssc.SerialPortException;

public class Project {
    public static void main(String[] args) {
        String str;

        //In the constructor pass the name of the port with which we work
        SerialPort serialPort = new SerialPort("COM3");

        try {
            //Open port
            serialPort.openPort();

            //We expose the settings. You can also use this line - serialPort.setParams(9600, 8, 1, 0);
            serialPort.setParams(SerialPort.BAUDRATE_9600,
                    SerialPort.DATABITS_8,
                    SerialPort.STOPBITS_1,
                    SerialPort.PARITY_NONE);



            Thread.sleep(1000);//Very important !!!


            serialPort.writeBytes("o".getBytes());


            //Closing the port
            serialPort.closePort();

        } catch (SerialPortException ex) {
            System.out.println(ex);
        } catch (InterruptedException ex) {
            System.out.println(ex);
        }
    }
}

Finally, everything is working…
Please consider, above shown examples are just prove to work examples and should be optimized.

The Arduino can not "interface with Java". It CAN write to the serial port. Java CAN read from the serial port. That the Arduino is the thing on the other end is just a happy accident.

And yes, I know the other possibility is, I'm just stupid.

I wasn't going to say that.