Communication from Java to Arduino

Hi all,
I have to send data from Java to Arduino.

Which tool do you suggest me (for example, RXTX, JSSC, etc.)?

I need a tool as light as possible, with a short lag (in the millisecond).

The Java program has to send an int array to Arduino several times per second (let's say, 20 times per second).

I'm working with Arduino Uno.

Thanks for the help :slight_smile:

JSSC is newer and IMHO easier to use. I think support for RXTX has petered out.

How many bytes per second do you need to send to the Arduino?

You may find the examples and concepts in Serial Input Basics useful.

...R

Thank you for your guide, it’s very accurate and refined :slight_smile:

I need to send 620 bytes per second to Arduino.

I’ve run a try: I’ve used the first code (the one that uses the functions recvWithStartEndMarkers() and showNewData() ) of the message #1 of your guide, and I’ve used this Java code:

package main;

import jssc.SerialPort;
import jssc.SerialPortException;

public class CodeProjectTest1 {

    public static void main(String[] args) {
        SerialPort serialPort = new SerialPort("COM3");

        try {
            serialPort.openPort();

            serialPort.setParams(SerialPort.BAUDRATE_9600,
                    SerialPort.DATABITS_8,
                    SerialPort.STOPBITS_1,
                    SerialPort.PARITY_NONE);

            serialPort.setFlowControlMode(SerialPort.FLOWCONTROL_RTSCTS_IN
                    | SerialPort.FLOWCONTROL_RTSCTS_OUT);

            System.out.println("String sent: " + serialPort.writeString("<Hello world>"));
        } catch (SerialPortException ex) {
            System.out.println("There is an error on writing string to port: " + ex);
        }
    }
}

This is the output in the Arduino Serial Monitor after a run of the Java program:

<A<Arduino is ready>

And this is the response in the Java console:

run:
String sent: true
BUILD SUCCESSFUL (total time: 0 seconds)

If I run again the Java program, I get a slightly different output in the Arduino Serial Monitor:

<Ar<Arduino is ready>

And if I run the Java program while Arduino Serial Monitor is opened, I get this output on the Java console:

run:
There is an error on writing string to port: jssc.SerialPortException: Port name - COM3; Method name - openPort(); Exception type - Port busy.
BUILD SUCCESSFUL (total time: 0 seconds)

You can't use the serial port for the Arduino Serial Monitor and for another program at the same time.

You may need to set some of the serial port parameters in JSSC. I think you may need to set rtscts to true. There is a JRuby example in this Python - Arduino demo - it may illustrate the issue. It's a good while since I wrote it.

...R

It works! Thank you very much, Robin2.

As you said, I had to set RTS and DTR. In order to do this, I passed these arguments to setParams():

serialPort.setParams(SerialPort.BAUDRATE_9600,
                    SerialPort.DATABITS_8,
                    SerialPort.STOPBITS_1,
                    SerialPort.PARITY_NONE,
                    true, false);

where "true" is related to "setRTS" and "false" is related to "setDTR".

Thank you again :slight_smile: .

Hi everyone...

I'm working on a home automation system using Arduino Uno and wanted to use a JAVA server with Serial communication with the Uno and I found that I can use the JSSC library to do so but I ran into troubles.

my case is that I want the Arduino to wait for 2 consecutive values:

  1. pin number -> corresponding to digital pin connected to a relay I want to control
  2. value -> corresponding to HIGH or LOW

on the other hand the server asks respectively for both values and send them to the Serial port where the Arduino is connected.

BUT...I found that once I send the first value, ex: 1, relay on pin 1 directly turn on without waiting for another command.

This may mean that some garbage bytes are being sent after the 1 I sent !!

anyone can help regarding this??

It's very important and I'm in a big rush.. :frowning:

I suggest you to send Arduino the following strings:

<5,0,>

or the string:

<5,1,>

where 5 is the pin number, 0 is LOW and 1 is HIGH, “<” is the starting marker and “>” is the ending marker (in order to avoid garbage values). Then Arduino should parse it following the guide suggested by Robin2: Serial Input Basics

mahmoud_mahdi:
It's very important and I'm in a big rush.. :frowning:

I can't see how your question has anything in common with the problem posed by @alexander79p.

Without seeing your code it is impossible to say more than was said in Reply #6

...R