error when reading rxtx serial port

0

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) {
                }
            }
        }
    }
}
    
asked by Alexander Villalobos 03.08.2017 в 19:14
source

1 answer

1

The problem is that you are reading letter by letter aux = entrada.read();

What you should do is read something like this in the buffer:

if(entrada!=null){
    for (; ; ) {
        int index = entrada.read(buffer, 0, buffer.length);
        if (index < 0)
            break;
        String salida = new String(buffer);
        Double datoDouble = Double.valueOf(salida);
    System.out.println(salida);
    }
    Thread.sleep(100);
}

Staying in your code like this:

public class LeerSerial implements Runnable {

    int aux;
    public void run() {
        byte[] buffer = new byte[1024];
        while (true) {
            try {

                if(entrada!=null){
                    for (; ; ) {
                    int index = entrada.read(buffer, 0, buffer.length);
                    if (index < 0)
                        break;
                    String salida = new String(buffer);
                    Double datoDouble = Double.valueOf(salida);
                    System.out.println(salida);
                 }
                 Thread.sleep(100);
               }
            } catch (Exception e) {
                 e.printStackTrace();//por si hay un error poder saber que ocurrió.
            }
        }
    }
}
    
answered by 03.08.2017 в 19:57