Perform a reading of data with serial port using the library rxtx
the code works well I can read and print the data but when printing the result of the reading it does not show it to me in this way 20.60 if not character by character and additional to that I want to assign that value to a variable double
when disconnecting from port COM
but I can not get it this is the code I use to read and connect.
public class puertoController {
CommPortIdentifier portId;
Enumeration puertos;
SerialPort serialport;
static InputStream entrada = null;
Thread t;
String valor = "COM1";
private String aux1;
public void init() {
t = new Thread(new LeerSerial());
t.start();
t.interrupt();
}
public void conectar() {
puertos = CommPortIdentifier.getPortIdentifiers();
t = new Thread(new LeerSerial());
while (puertos.hasMoreElements()) {
portId = (CommPortIdentifier) puertos.nextElement();
System.out.println(portId.getName());
if (portId.getName().equals(valor)) {
try {
serialport = (SerialPort) portId.open("LecturaSerialTemperatura", 500);
entrada = serialport.getInputStream();
t.resume();
} catch (Exception e) {
}
}
break;
}
}
public void Desconectar() {
t.interrupt();
serialport.close();
}
public class LeerSerial implements Runnable {
int aux;
public void run() {
while (true) {
try {
byte[] buffer = new byte[1024];
aux = entrada.read();
Thread.sleep(100);
if (aux > 0) {
System.out.println((char) aux);
}
} catch (Exception e) {
}
}
}
}
}