Thread View: borland.public.jbuilder.java.api
1 messages
1 total messages
Started by Flavio
Sun, 14 Dec 2003 19:26
problem with serial com
Author: Flavio
Date: Sun, 14 Dec 2003 19:26
Date: Sun, 14 Dec 2003 19:26
125 lines
5050 bytes
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;
}
}
}
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads