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.
– Woss
@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
– Vale
@Andersoncarloswoss you could give example of how to do this ,I’m not getting
– Vale
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.
– Woss
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.
– Pedro Ferreira
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.
– Pantoja