Preciso de ajuda no meu programa. Estou a fazer uma comunicação entre Java e Arduino por serial. E no meu programa de Java tem como objetivo enviar dados e receber dados do Arduino. O problema é que os dados que eu recebo vêm cortados. Vou exemplificar…
Este é o programa Java:
public class Reconhecimento {
public static String devicePortName = "USB-SERIAL CH340";
public static SerialPort sp;
public static void main(String[] args) throws IOException, InterruptedException {
int len = SerialPort.getCommPorts().length;
SerialPort serialPorts[] = new SerialPort[len];
serialPorts = SerialPort.getCommPorts();
//reconhecimento das portas
for (int i = 0; i < len; i++) {
String portName = serialPorts[i].getDescriptivePortName();
System.out.println(serialPorts[i].getSystemPortName() + ": " + portName + ": " + i);
if (portName.contains(devicePortName)) {
sp = serialPorts[i];
sp.openPort();
System.out.println("Conectado a: " + portName + "[" + i + "]");
break;
}
}
sp.setBaudRate(38400);
sp.setComPortTimeouts(SerialPort.TIMEOUT_READ_BLOCKING, 0, 0);
for(int i=0;true;i++) {
sp.setComPortTimeouts(SerialPort.TIMEOUT_READ_BLOCKING, 0, 0);
BufferedReader in = new BufferedReader(new InputStreamReader(sp.getInputStream()));
String msg = in.readLine();
System.out.println(msg);
}
}
}
E este é o programa no arduino:
#include <SoftwareSerial.h>
int i = 0;
void setup() {
// put your setup code here, to run once:
Serial.begin(38400);
while (!Serial) {
;
}
}
void loop() {
// put your main code here, to run repeatedly:
Serial.println(“teste”);
}
Mas o que recebo no Output do Java é isto:
teste
este
te
teste
Como pode ver a mensagem “teste” vem aos cortes. Agradeço toda ajuda possível.