Problem with reading bytes from Arduino Uno

Hi, i have follow problem with reading data from Arduino. I am sending string "Hellow world" from Arduino. In my Java app i' m reading this string as bytes. But i'm receiving choppy data. Example:

Native lib Version = RXTX-2.1-7 Java lib Version = RXTX-2.1-7 /dev/tty.usbmodem621 Held Hello world Hel lo world H ello world

My java cods:

inBytes = connection.readBlocked(11);
String text = new String(inBytes, "UTF-8");
                System.out.println(text);

where

connection

is connection from open port. Method readBlocked is

 @Override
    public byte[] readBlocked(int num) throws IOException {
        byte[] buff;
        buff = new byte[num];
        this.inputStream.readFully(buff, 0, buff.length);
        
        return buff;
    }

fortran: Hi, i have follow problem with reading data from Arduino. I am sending string "Hellow world" from Arduino. In my Java app i' m reading this string as bytes. But i'm receiving choppy data. Example: My java cods:

inBytes = connection.readBlocked(11);
String text = new String(inBytes, "UTF-8");
                System.out.println(text);

where is connection from open port. Method readBlocked is

 @Override
    public byte[] readBlocked(int num) throws IOException {
        byte[] buff;
        buff = new byte[num];
        this.inputStream.readFully(buff, 0, buff.length);
        
        return buff;
    }

More... https://github.com/iqnev/JavaRxtx/blob/master/src/testcomunication/TestComunication.java

I will ask the moderator to move this Thread to the section on interfacing. There are other Threads about Java there.

You have not posted the full Java code so I may be making unnecessary comments.

It is important that the Java code allows time for the Arduino to reset when it opens the serial port. It is important that the Java code keeps the serial port open until it is completely finished with the Arduino.

It would be a good idea for the Java program to keep reading bytes from the Arduino until it detects an end marker - perhaps a line feed or the character '>' or whatever suits your situation. I suspect your code is timing out before all the data is received.

I think you are using the RxTx library. The newer JSSC library seems to cause fewer problems.

...R

fortran:
Hi,
::::SNIP::::

 @Override

public byte readBlocked(int num) throws IOException {
       byte buff;
       buff = new byte[num];
       this.inputStream.readFully(buff, 0, num); // << == This line
       
       return buff;
   }

fortran,
you may want num, not buff.length. I believe “length” is generally used to READ the buffer, NOT WRITE to it.
Jesse

This is not a problem, because

num == buff.length

. The main problem: The string

Hello World

convert to ASCII is

072 101 108 108 111 032 119 111 114 108 100

but i don't receive that sequence every time. Example i receive:

072 108 101 108 111 032 119 111 114 108 100

101 072 108 108 111 032 111 114 108 119 100

101 072 108 111 032 114 108 111 119 100 108

But length of receive data is 11 bytes.

You are assuming the serial data is guaranteed to be received. It is NOT. Data can be lost or corrupt for a variety of reasons.

You should be sending start and end markers before and after the packet. Read and discard data until the start marker arrives. Read and store data until the end marker arrives. Then, use the data.

If you KNOW that you are supposed to get 11 characters, what possible reason is there for dynamically allocating an array. Use a static array of the proper size!