🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.jbuilder.java.api
Article #424

problem with serial com

#424
From: Flavio
Date: Sun, 14 Dec 2003 19:26
125 lines
5050 bytes
hi i got a problem...i need to get from the serial port an string but the source code that i got receive a byte...what i have to change to receive an String????
this is te code that i got

package interfazcomunicacion;
import java.io.*;
import java.util.*;
import javax.comm.*;

import javax.swing.*;

public class java_902 implements Runnable,SerialPortEventListener {
  /*static CommPortIdentifier idPuerto;
  static Enumeration listaPuertos;*/
  static String lectura;
  JTextField t2;
  InputStream entrada;
  SerialPort puertoSerie;
  Thread tLectura;


  // En este ejemplo implementa un thread que es el que se encarga de
  // que la aplicaci�n se quede esperando en el puerto que se haya
  // abierto a que se reciban datos.
  // Primero abre el puerto y luego le fija los par�metros
  public java_902(CommPortIdentifier id, Enumeration lp, JTextField t1 ) {
    t2 = t1;

    CommPortIdentifier idPuerto;
    Enumeration listaPuertos;
    idPuerto = id;
    listaPuertos = lp;
    String io1;

    // Si el puerto no est� en uso, se intenta abrir
    try {
      puertoSerie = (SerialPort)idPuerto.open( "AplLectura",20000 );
    } catch( PortInUseException e ) {}
    // Se obtiene un canal de entrada
    try {
      entrada = puertoSerie.getInputStream();

    } catch( IOException e ) {}

    // A�adimos un receptor de eventos para estar informados de lo
    // que suceda en el puerto
    try {
      puertoSerie.addEventListener( this );
	  } catch( TooManyListenersException e ) {}

    // Hacemos que se nos notifique cuando haya datos disponibles
    // para lectura en el buffer de la puerta
    puertoSerie.notifyOnDataAvailable( true );

    // Se fijan los par�metros de comunicaci�n del puerto
    try {
      puertoSerie.setSerialPortParams( 9600,
        SerialPort.DATABITS_7,
        SerialPort.STOPBITS_2,
        SerialPort.PARITY_EVEN );
    } catch( UnsupportedCommOperationException e ) {}

    // Se crea y lanza el thread que se va a encargar de quedarse
    // esperando en la puerta a que haya datos disponibles
    tLectura = new Thread( this );
    tLectura.start();
    }

  public void run() {


    try {
      // En los threads, hay que procurar siempre que haya alg�n
      // m�todo de escape, para que no se queden continuamente
      // bloqueados, en este caso, la comprobaci�n de si hay datos
      // o no disponibles en el buffer de la puerta, se hace
      // intermitentemente
      Thread.sleep( 2000 );
    } catch( InterruptedException e ) {}
    }


  public void serialEvent( SerialPortEvent _ev ) {
    switch( _ev.getEventType() ) {
      // La mayor�a de los eventos no se trata, �stos son los
      // que se producen por cambios en las l�neas de control del
      // puerto que se est� monitorizando
      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;
      // Cuando haya datos disponibles se leen y luego se
      // imprime lo recibido en la consola
      case SerialPortEvent.DATA_AVAILABLE:
        byte[] bufferLectura = new byte[8];
        try {
          while( entrada.available() > 0 ) {
            int nBytes = entrada.read( bufferLectura );
            }
//cortamos los espacios blancos usando trim

             String io1 = new String(bufferLectura);
             String io = io1.trim().toString();
            this.lectura = io;

            valor nuevo = new valor(t2, this.lectura);

            System.out.println(this.lectura);

         // System.out.println( new String(bufferLectura) );

          EnviarDato suerte = new EnviarDato("ringo", puertoSerie);

        } catch( IOException e ) {}
        //puertoSerie.close();
        break;
      }
    }
  }
Technical Details
Message-ID: <3fdcc78f$1@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!sn-xit-02!sn-xit-01!sn-xit-09!supernews.com!38.144.126.76.MISMATCH!feed2.newsreader.com!newsreader.com!newshosting.com!news-xfer2.atl.newshosting.com!newsfeed.icl.net!newsfeed.fjserv.net!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail

Show Full Headers
Complete Article Headers
X-Google-Language: SPANISH,ASCII
X-Google-Thread: 1089d0,fbf3a8015d8da9d9,start
X-Google-Attributes: gid1089d0,public
X-Google-ArrivalTime: 2003-12-14 12:53:49 PST
From: "Flavio" <ringostar@universia.cl>
Sender: "Flavio" <ringostar@universia.cl>
Reply-To: "Flavio" <ringostar@universia.cl>
Subject: problem with serial com
Newsgroups: borland.public.jbuilder.java.api
X-User-Info: 158.251.2.13     
X-Forwarded-For: 158.251.8.180
NNTP-Posting-Host: 207.105.83.66
Message-ID: <3fdcc78f$1@newsgroups.borland.com>
Date: 14 Dec 2003 12:26:55 -0700
X-Trace: newsgroups.borland.com 1071433615 207.105.83.66 (14 Dec 2003 12:26:55 -0700)
Lines: 126
Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!sn-xit-02!sn-xit-01!sn-xit-09!supernews.com!38.144.126.76.MISMATCH!feed2.newsreader.com!newsreader.com!newshosting.com!news-xfer2.atl.newshosting.com!newsfeed.icl.net!newsfeed.fjserv.net!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail