How to store data from Arduino in a Java string?

Asked

Viewed 533 times

3

I need to store data from Arduino in the Java via serial communication only the values sometimes not filled in completely. I am using the library rxtx.

      //trecho do código para leitura
                    int available = input.available();
                    byte chunk[] = new byte[available];
                    input.read(chunk, 0, available);
                    output.write(0);
                    output.flush(); 
     String teste = new String(chunk); 
 System.out.println(teste);
 close();//fecha comunicação serial 

Of Arduino sends the data like this @678&, But averzes the Java stores like this @6, 7, 8&, etc or just get a piece.

  • This is probably because the loop in Java runs faster than the time it takes to transmit all the information. That is, each time it runs, only a chunk of the information is in the buffer. What you can do is define a character that indicates the end of the information and while Java does not read this character, concatenates the information that arrived with the previous one.

  • @Andersoncarloswoss thanks for the tip, I had thought about it but had no idea how to implement, I think now I know how to do

  • @Andersoncarloswoss you could give example of how to do this ,I’m not getting

  • I do not know if I have knowledge of Java for such. Excuse me. But maybe someone who has can do reading the above comments.

  • Only with the above section is it impossible to produce a reliable answer, but you don’t need to reinvent the wheel. http://playground.arduino.cc/Interfacing/Java The sample code does exactly what you want, you just have to adapt to the code or edit the question so you can frame the solution.

  • When working with Rxtx, errors can occur when assembling and unmounting strings, picking character by character, loss of information problems while uploading or because of the Arduino loop as @Andersoncarloswoss said. Try using Javino, which has two libraries, one for Arduino and one for Java and allows you to do this synchronization through 3 operation modes. Also, it ends with that problem of libraries (dll) for those who use windows. So I’m going to leave this suggestion that helps control the flow of sending information from controllers to Java and vice versa.

Show 1 more comment

1 answer

1

The biggest problem in the case is not getting the data coming from Arduino but how to better control the flow of serial communication. The system is critical enough that it cannot wait 1s to forward the next data? It is important to know it needs to be the most real team possible to think also on energy consumption.

  • If yes, concatenate the inputs sent by Arduino

Note that the data is "getting lost" because while Java is processing the Arduino is submitting various data and this will make further processing.

In this case you need start and end markers to know when the information has come to an end. Assuming "@" and "&" are your bookmarks:

    int available = input.available();
    byte chunk[] = new byte[available];
    input.read(chunk, 0, available);
    output.write(0);
    output.flush();
    String teste = new String(chunk);
    String dado = "";
    if("@".equals(teste)){
        //Limpar lixo de dado anterior 
        dado = "";
    }else if(teste.contains("@") && teste.contains("&")){
        //Dado foi pego por completo
        dado = teste;
    }else{
        if(teste.contains("@")){
            dado += teste.replaceAll("@", "");
        }else if("&".equals(teste)){
            //Dado foi pego por completo. 
            //A partir daqui pode realizar operações como salvar no BD, tratar dado etc
        }else if(teste.contains("&")){
            dado += teste.replaceAll("&", "");
        }
    }
  • If not, I suggest using the Arduino code functions delay or Sleep

Browser other questions tagged

You are not signed in. Login or sign up in order to post.