Serial communication between Arduino and Java

265 views Asked by At

I print with Arduino the value in a package with this structure:

<3B,3B,3B,3B,3B,5B>

I get it on a Java application that runs on UDOO board. This is my code.

public void run() {
            byte[] buffer = new byte[64];
            int len = 0;
            String mStringReceived = "";
            int ind1,ind2;
            String DATA[];
            try {
                while((len = this.in.read(buffer)) > -1) {

                    mStringReceived += new String(buffer, 0, len);

                    if(mStringReceived.contains("<") && mStringReceived.contains(">")){

       ind1=mStringReceived.indexOf("<");
       mStringReceived= mStringReceived.subString(ind1,mStringReceived.lenght);
       ind1=0;
       ind2=mStringReceived.indexOf(">");

             if(ind1<ind2){ //Always true
                  mStringReceived=mStringReceived.subString(ind1,ind2);
                  DATA=mStringReceived.replaceAll("<|>","").split(",");





            }       
            }
               mStringReceived="";
            }
            } catch (IOException ioe) {
                ioe.printStackTrace();
            }
        }

So, I got a buffer of 64 bytes . I get the package ( < to > ) , and I split each set of bytes , grouped by a comma ( as Arduino ago ) in an array . If you run into a mode ' debugging ' ( step by step ) , it works perfectly . Each cycle obtain a given clear for my package . But if I run in the normal way , I do not get any data. Why? The speed is set for each system 9600. Thanks !

1

There are 1 answers

3
Saket Mittal On

Rules of Serial

The asynchronous serial protocol has a number of built-in rules - mechanisms that help ensure robust and error-free data transfers. These mechanisms, which we get for eschewing the external clock signal, are:

  • Data bits
  • Synchronization bits
  • Parity bits
  • Baud rate

The critical part is making sure that both devices on a serial bus are configured to use the exact same protocols.

Note: 9600 bps is baud rate you are using.

Baud Rate

The baud rate specifies how fast data is sent over a serial line. It’s usually expressed in units of bits-per-second (bps). Baud rates can be just about any value within reason. The only requirement is that both devices operate at the same rate. One of the more common baud rates, especially for simple stuff where speed isn’t critical, is 9600 bps. Other “standard” baud are 1200, 2400, 4800, 19200, 38400, 57600, and 115200.