2008-12-03 53 views
9

Soy principiante en Java. Estoy leyendo datos del dispositivo a través del puerto serie. Obtengo datos por cada minuto, pero la primera lectura llega a la mitad, después de que los datos estén llegando correctamente.Lectura del puerto serie en Java

salida que estoy recibiendo es:

6050.003120815340006050.003120815350006050.0

salida correcta debería ser así:

03120815340006050.003120815350006050.0


Mi código es:

import java.io.*; 
import java.util.*; //import gnu.io.*; 
import javax.comm.*; 

public class SimpleRead implements Runnable, SerialPortEventListener { 
    static CommPortIdentifier portId; 
    static Enumeration portList; 

InputStream inputStream; 
SerialPort serialPort; 
Thread readThread; 
byte[] readBuffer; 

public static void main(String[] args) { 
    portList = CommPortIdentifier.getPortIdentifiers(); 
    System.out.println("portList... " + portList); 
    while (portList.hasMoreElements()) { 
     portId = (CommPortIdentifier) portList.nextElement(); 
     if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) { 
      System.out.println("port identified is Serial.. " 
        + portId.getPortType()); 
      if (portId.getName().equals("COM2")) { 
       System.out.println("port identified is COM2.. " 
         + portId.getName()); 
       // if (portId.getName().equals("/dev/term/a")) { 
       SimpleRead reader = new SimpleRead(); 
      } else { 
       System.out.println("unable to open port"); 
      } 
     } 
    } 
} 

public SimpleRead() { 
    try { 
     System.out.println("In SimpleRead() contructor"); 
     serialPort = (SerialPort) portId.open("SimpleReadApp1111",500); 
     System.out.println(" Serial Port.. " + serialPort); 
    } catch (PortInUseException e) { 
     System.out.println("Port in use Exception"); 
    } 
    try { 
     inputStream = serialPort.getInputStream(); 
     System.out.println(" Input Stream... " + inputStream); 
    } catch (IOException e) { 
     System.out.println("IO Exception"); 
    } 
    try { 
     serialPort.addEventListener(this); 

    } catch (TooManyListenersException e) { 
     System.out.println("Tooo many Listener exception"); 
    } 
    serialPort.notifyOnDataAvailable(true); 
    try { 

     serialPort.setSerialPortParams(9600, SerialPort.DATABITS_8, 
       SerialPort.STOPBITS_1, SerialPort.PARITY_NONE); 

     // no handshaking or other flow control 
     serialPort.setFlowControlMode(SerialPort.FLOWCONTROL_NONE); 

     // timer on any read of the serial port 
     serialPort.enableReceiveTimeout(500); 

     System.out.println("................"); 

    } catch (UnsupportedCommOperationException e) { 
     System.out.println("UnSupported comm operation"); 
    } 
    readThread = new Thread(this); 
    readThread.start(); 
} 

public void run() { 
    try { 
     System.out.println("In run() function "); 
     Thread.sleep(500); 
     // System.out.println(); 
    } catch (InterruptedException e) { 
     System.out.println("Interrupted Exception in run() method"); 
    } 
} 

public void serialEvent(SerialPortEvent event) { 

    // System.out.println("In Serial Event function().. " + event + 
    // event.getEventType()); 
    switch (event.getEventType()) { 
    /* 
    * case SerialPortEvent.BI: case SerialPortEvent.OE: case 
    * SerialPortEvent.FE: case SerialPortEvent.PE: case SerialPortEvent.CD: 
    * case SerialPortEvent.CTS: case SerialPortEvent.DSR: case 
    * SerialPortEvent.RI: case SerialPortEvent.OUTPUT_BUFFER_EMPTY: break; 
    */ 
    case SerialPortEvent.DATA_AVAILABLE: 
     readBuffer = new byte[8]; 

     try { 

      while (inputStream.available()>0) { 

       int numBytes = inputStream.read(readBuffer); 
      // System.out.println("Number of bytes read " + numBytes); 
      } 

      System.out.print(new String(readBuffer)); 

     } catch (IOException e) { 
      System.out.println("IO Exception in SerialEvent()"); 
     } 
     break; 
    } 
    // System.out.println(); 
/* String one = new String(readBuffer); 
    char two = one.charAt(0); 
    System.out.println("Character at three: " + two);*/ 
} 

} 
+0

Agregue cuatro espacios en blanco delante de las primeras líneas, de modo que esté formateado como código. – guerda

+0

¿Acabas de copiar eso de otro sitio? - http://www.java-samples.com/showtutorial.php?tutorialid=11 –

Respuesta

2

Esto se ve como si estuviera leyendo el resto de algún mensaje que fue enviado antes de empezar.

Intente leer tantos datos como sea posible al iniciar el programa para borrar los búferes de hardware. Después de eso, comienza tu procesamiento.

3

Pruebe a vaciar el búfer de entrada del puerto antes de leer. De lo contrario, si el extremo remitente ha enviado datos durante el inicio de su programa (o muy de cerca, eso podría corresponder al sistema operativo), obtendrá datos antiguos almacenados en el búfer.

Además, si es posible, considere agregar estructura de mensaje al protocolo, para que pueda detectar cuándo ha leído algo que no es, de hecho, un mensaje completo, y deséchelo. Esto a menudo es muy útil con este tipo de problemas.

4

Usar la siguiente:

while (inputStream.available()>0) { 
    int numBytes = inputStream.read(readBuffer); 
    System.out.print(new String(readBuffer)); 
} 

va a imprimir el resultado fuera del bucle while. Sin embargo, el código dentro del ciclo puede ejecutarse más de una vez, por lo que se perderá una gran cantidad de datos.

+0

probé el código anterior pero el método serialEvent no se ejecuta. ¿Podría decirme por qué? –

Cuestiones relacionadas