Salve GUJ,
Estou implementando uma comunicação com uma balança Toledo em Java, já consegui comunicar só que os bytes recebidos vem todos bagunçados,
Código para conectar utilizando RXTX:
SerialPort serialPort = (SerialPort) portId.open(this.getClass().getName(), 500);
inputStream = serialPort.getInputStream();
serialPort.addEventListener(this);
serialPort.notifyOnDataAvailable(true);
serialPort.setSerialPortParams(4800, SerialPort.DATABITS_8,SerialPort.STOPBITS_2,SerialPort.PARITY_NONE);
Código pra leitura da porta
public void serialEvent(SerialPortEvent event) {
switch (event.getEventType()) {
case SerialPortEvent.DATA_AVAILABLE:
byte[] readBuffer = new byte[1024];
try {
int numBytes = 0;
while (inputStream.available() > 0) {
numBytes = inputStream.read(readBuffer);
}
String result = new String(readBuffer);
result = result.substring(1, numBytes);
System.out.println("Read: "+result);
} catch (IOException e) {
e.printStackTrace();
}
break;
}
}
e a saida foi:
Stable Library
=========================================
Native lib Version = RXTX-2.1-7
Java lib Version = RXTX-2.1-7
Read: ?¬ð`
Read: 00²·9300
Read: 0000?
Read: ?¬ð`
Read: 00²·9300
Read: 0000?
Read: ?¬ð`
Read: 00²·9300
Read: 0000?
A saida correta deveria ser 0025930000, quando eu conecto com o HyperTerminal do windows eu vejo a saída correta, quando conecto o meu projeto em Java sai essa bagunça.
Alguem pode me ajudar???
Valeu…