Can't send integer to Arduno from Java

Hi, my new problem ;D

I want to send integers from Java application to Arduino, the problem is that sketch is perfectly working with Serial monitor from Arduino compiler, but don’t want to do it with my Java soft, maybr you know, where is the problem:

package arduino;


public class Main {
    static InputStream input;
    static OutputStream output;

    public static void main(String[] args) throws Exception{
        System.out.println("Using port: " + Preferences.get("serial.port"));
        CommPortIdentifier portId = CommPortIdentifier.getPortIdentifier(

        SerialPort port = (SerialPort)"serial talk", 4000);
        input = port.getInputStream();
        output = port.getOutputStream();

            boolean k=true;

                while (input.available()!=0)


And Arduino sketch:

void setup()

void loop () 
  char serInString[20]="";
  int i=0;
    while (Serial.available()) 
    serInString[i] =;
  delay(100);  // wait a bit, for serial data


Firstly, you should be aware that java’s idea of an int is a 4 byte value, but an arduino int is 2 bytes. In java you could use a short which is a 2 byte value.

Secondly, using the RXTX library you only send bytes over serial (, if you want to send a int then you’re going to have to split it into two bytes then reassemble that on the arduino.

So in java you would do something like:

short x = 1200;
byte upper = x >> 8; //Get the upper 8 bits
byte lower = x & 0xFF; //Get the lower 8bits

Then on the arduino you’d have something like:

byte upper =;
byte lower =;
int x = (upper<<8) | lower; //Reassemble the number
Serial.print(x, DEC);

Is working, thanks very much, maybe you know where I can find some materials about synchronization? Sending a lot of integers can cause some mistakes...

Do you mean synchronization in Java between multiple threads, or between the computer and the Arduino?

If it's the first then the Java Tutorial on concurrency is a good information source.

If it's the latter, then I'm assuming you're getting mistakes because the computer is sending the data faster than the arduino can process it. (This means the serial buffer will be filling up and bytes will be lost, resulting in mismatched upper and lower values). You can get around this by having the arduino send acknowledgements back so the computer knows when it is ready to send more data.

As an example, in a project I've been working on I have a Java class which manages the serial connection, and it counts the number of bytes that have been sent, when 100 bytes have been sent it will not send any more until a certain message is received from the arduino. The arduino then counts the number of bytes it has recieved and processed, and when that number reaches 100, it sends the request for more to the computer.

Yes, the problem was loosing bytes betweent Arduino and Computer, your method is good, firstly i was thinking about sending reply after each integer, but it would loose to much time. I will try yours method. Thanks!