[b]Saudações pessoal!
Estou fazendo um TC de Rfid e estou utilizando uma classe de comunicação serial da SUN
O dispositivo esta comunicando tudo certinho, consigo escrever e ler dados dele atraves desta classe , o problema é o seguinte… a classe esta lendo byte por byte na qual estou convertendo para string, se o dispositivo ler uma informação tipo “1nGU” a classe ira me retornar palavra por palavra, sendo assim eu não consigo fazer nenhum if ou grava-la em um banco de dados
Gostaria de saber se não tem como eu ler ou somar todos esses bytes para uma palavra só. abaixo esta o codigo ques estou utilizando:[/b]
import gnu.io.CommPort;
import gnu.io.CommPortIdentifier;
import gnu.io.SerialPort;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
public class TwoWaySerialComm {
public TwoWaySerialComm() {
super();
}
void connect(String portName) throws Exception {
CommPortIdentifier portIdentifier =
CommPortIdentifier.getPortIdentifier(portName);
if (portIdentifier.isCurrentlyOwned()) {
System.out.println("Error: `port is currently in use");
} else {
CommPort commPort =
portIdentifier.open(this.getClass().getName(), 2000);
if (commPort instanceof SerialPort) {
SerialPort serialPort = (SerialPort) commPort;
serialPort.setSerialPortParams(9600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
OutputStream out = serialPort.getOutputStream();
InputStream in = serialPort.getInputStream();
// InputStream in = serialPort.getInputStream();
(new Thread(new SerialWriter(out))).start();
(new Thread(new SerialReader(in))).start();
} else {
System.out.println("Error: only serial ports are handled by this example.");
}
}
}
public static class SerialReader implements Runnable {
InputStream in;
public SerialReader(InputStream in) {
this.in = in;
}
public void run() {
byte[] buffer = new byte[1024];
int len = -1;
try {
while ((len = this.in.read(buffer)) > -1) { //<------------ Aqui estou contando quandos bytes que tem no buffer se não
// haver byte nenhum o read me retorna -1 e sai do laço
String ler = (new String(buffer,0 ,len)); //<--------------Aqui estou convertendo para String e zerando o buffer
System.out.println(ler); //<---------- A informação que é pra vir eh "1nGU" acada 2,5seg se eu tirar
// o "ln" ele me imprime assim :1nGU 1nGU 1nGU 1nGU, se eu deixar "ln"
// imprime assim: 1
// n
// G
// U
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static class SerialWriter implements Runnable {
OutputStream out;
public SerialWriter(OutputStream out) {
this.out = out;
}
public void run() {
try {
int c = 0;
while ((c = System.in.read()) > -1) {
this.out.write(c);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
Se alguem puder ajudar eu agradeço muito !