Ver Mensaje Individual
  #2 (permalink)  
Antiguo 09/11/2006, 02:10
clinisbut
 
Fecha de Ingreso: diciembre-2004
Mensajes: 278
Antigüedad: 19 años, 5 meses
Puntos: 0
Aquí dejo el codigo de la clase:
Código:
package puerto_serie;

import javax.comm.*;
import java.io.*;			//Input & Output Stream
import java.util.*;		//EventListener class



public class ComControl implements Runnable, SerialPortEventListener
{
	static CommPortIdentifier portId;
	
	InputStream 	canalEntrada;
	OutputStream 	canalSalida;
	SerialPort 		puerto;
	Thread 				readThread;
	
	protected String divertCode = "10";
	static String TimeStamp;



	public void main(String[] args)
	{	try
		{	this.portId = CommPortIdentifier.getPortIdentifier("COM1");
		}
		catch(Exception e)
		{	TimeStamp = new java.util.Date().toString();
			System.out.println(TimeStamp + ": COM1 " + this.portId);
			System.out.println(TimeStamp + ": msg1 - " + e);
		}		
	}
	
	//Constructor
	public ComControl()
	{	
		try
		{	this.portId = CommPortIdentifier.getPortIdentifier("COM1");
		}
		catch(Exception e)
		{	TimeStamp = new java.util.Date().toString();
			System.out.println(TimeStamp + ": COM1 " + this.portId);
			System.out.println(TimeStamp + ": msg1 - " + e);
		}		
		
		try
		{	TimeStamp = new java.util.Date().toString();
			this.puerto = (SerialPort) this.portId.open("ComControl", 2000);
		}
		catch(PortInUseException e)
		{	System.out.println("Hay otra aplicación usando el puerto COM1, imposible continuar.");
			return;
		}
		
		try
		{	this.canalEntrada = this.puerto.getInputStream();
			this.canalSalida = this.puerto.getOutputStream();
		}
		catch(IOException e)
		{}
		
                //Como hace referencia esto a serialEvent(SerialPortEvent event)?
		try
		{	this.puerto.addEventListener(this);
		}
		catch(TooManyListenersException e)
		{}
		
		this.puerto.notifyOnDataAvailable(true);
		try
		{ this.puerto.setSerialPortParams(9600,
				SerialPort.DATABITS_8,
				SerialPort.STOPBITS_1,
				SerialPort.PARITY_NONE);
			//this.puerto.setDTR(false);	//Depends on device
			//this.puerto.setRTS(false);	//Depends on device
		}
		catch(UnsupportedCommOperationException e)
		{}
		//Estas dos lineas no las acabo de entender
		this.readThread = new Thread(this);
		this.readThread.start();		
	}
	
	
	//Sobrecargamos el metodo run() de la interface Runnable
//Esto no entiendo a cuento de que viene
	public void run()
	{	try
		{	System.out.println("Buscamos evento");
			Thread.sleep(100);
		}
		catch(InterruptedException e)
		{}
	}
	
	//Definimos el evento del puerto serie
	public void serialEvent(SerialPortEvent event)
	{	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:
				 byte[] bufferLectura = new byte[20];
					try 
					{	while( canalEntrada.available() > 0 ) 
						{	int nBytes = canalEntrada.read( bufferLectura );
            }
						System.out.println( new String(bufferLectura) );
					} catch( IOException e ) {}
        break;
		}
     
	}
	
	public void enviaDatos(String loque)
	{	try
		{	this.canalSalida.write(loque.getBytes());
		}
		catch(IOException e)
		{	}
		
		try
		{	System.out.println("Duermo 2000");
			Thread.sleep(2000);	//Nos aseguramos que da tiempo a enviar los datos antes de cerrar la conexion			
		}
		catch(Exception e)
		{}		
	}
	
	public void cierraPuerto()
	{	this.puerto.close();
	}
}