Thread View: borland.public.jbuilder.java.api
2 messages
2 total messages
Started by Eliot
Tue, 07 Oct 2003 03:36
chain of data
Author: Eliot
Date: Tue, 07 Oct 2003 03:36
Date: Tue, 07 Oct 2003 03:36
47 lines
2526 bytes
2526 bytes
hi i need help...urgent... i need to send an chain of data
trought the serial port and i hace the next class that send just 1
char..somebody can change the source code an tellme how to do to send a
chain onf data???
public class SerialCom {
static Enumeration listaPuertos;
static CommPortIdentifier idPuerto;
static String mensaje;
static SerialPort puertoSerie;
static OutputStream salida;
public static void main( String[] args ) {
listaPuertos = CommPortIdentifier.getPortIdentifiers();
while( listaPuertos.hasMoreElements() ) {
idPuerto = (CommPortIdentifier)listaPuertos.nextElement();
if( idPuerto.getPortType() == CommPortIdentifier.PORT_SERIAL ) {
// if( idPuerto.getName().equals("/dev/term/a") ) {
if( idPuerto.getName().equals("COM1") ) {
// Si el puerto no est� en uso, se intenta abrir
try {
puertoSerie = ( SerialPort )idPuerto.open("AplEscritura",2000);
} catch( PortInUseException e ) {}
// Se obtiene un canal de salida
try {
salida = puertoSerie.getOutputStream();
} catch( IOException e ) {}
// Se fijan los par�metros de comunicaci�n del puerto
try {
puertoSerie.setSerialPortParams( 4800,
SerialPort.DATABITS_7,
SerialPort.STOPBITS_2,
SerialPort.PARITY_EVEN );
} catch( UnsupportedCommOperationException e ) {}
// Se env�a el mensaje
try {
salida.write( mensaje.getBytes() );
} catch( IOException e ) {}
}
}
}
}
}
Re: chain of data
Author: Owen Green
Date: Tue, 07 Oct 2003 12:05
Date: Tue, 07 Oct 2003 12:05
58 lines
2460 bytes
2460 bytes
On Mon, 6 Oct 2003 23:36:49 -0400, Eliot wrote:
> hi i need help...urgent... i need to send an chain of data
> trought the serial port and i hace the next class that send just 1
> char..
I think you need to flush() the output stream:
salida.write( mensaje.getBytes() );
salida.flush();
Also, you are 'swallowing' exceptions, which is a bad practice - if your
program stops working, then it will be difficult to know why :)
In the case of this code, any of the exceptions you are catching and
ignoring would lead to failure of the job, but are not being handled
properly. If one were thrown, the code would just carry on, and perhaps
cause a NullPointerException, or similar.
Instead:
try {
puertoSerie = ( SerialPort)idPuerto.open("AplEscritura",2000);
// Se obtiene un canal de salida
salida = puertoSerie.getOutputStream();
// Se fijan los par�metros de comunicaci�n del puerto
puertoSerie.setSerialPortParams( 4800,
SerialPort.DATABITS_7,
SerialPort.STOPBITS_2,
SerialPort.PARITY_EVEN );
// Se env�a el mensaje
salida.write( mensaje.getBytes() );
salida.flush();
}
catch( PortInUseException pe )
{
pe.printStackTrace();
//handle
}
catch( UnsupportedCommOperationException ue )
{
ue.printStackTrace();
//handle
}
catch( IOException ie )
{
ie.printStackTrace();
//handle
}
finally
{
//clean up
}
Finally, by and large it's easier to get a solution to a problem with a bit
less code to read :)
--
Owen
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