Ver Mensaje Individual
  #12 (permalink)  
Antiguo 16/11/2006, 01:54
clinisbut
 
Fecha de Ingreso: diciembre-2004
Mensajes: 278
Antigüedad: 19 años, 5 meses
Puntos: 0
com.java //Clase para la comunicacion con el puerto serie
Código:
import javax.comm.*;
import java.io.*;
import java.util.*;
import javax.swing.*;


public class com
{
	protected Enumeration ports;						//Listado de puertos
	protected CommPortIdentifier port_id;		
	public	SerialPort	puerto;					//Subclase de CommPort
		
	private InputStream inputStream;	//Para el envio de datos al dispositivo.
	private OutputStream outputStream;			//Para la lectura del dispositivo
		
		
	protected String response;					//Ultima linea leida desde el puerto serie
	protected boolean debug = true;			//flag para el controlar la salida debug
		
	private final int TIMEOUT = 30;		//Tiempo de espera maximo para abrir el puerto	
		
	private boolean tapa_abierta=false;	
	private JPanel contenido;
	private Programa app;
		
	//Constructor
	//com(JPanel contenido)
	com(Programa app)
	{	ports=CommPortIdentifier.getPortIdentifiers();
		//this.contenido = contenido;
		this.app = app;
	}
	
	
	//Busca los puertos disponibles en el PC
	public ArrayList listarPuertos()
	{	
		ArrayList<String> nombre_puertos = new ArrayList<String>();
		CommPortIdentifier temp_port;
		while(ports.hasMoreElements())
		{	temp_port=(CommPortIdentifier)ports.nextElement();
			if( CommPortIdentifier.PORT_SERIAL==temp_port.getPortType() )
			{	nombre_puertos.add( (String) temp_port.getName());
			}
		}
		return nombre_puertos;
	}
	
	
	//Abrir puerto
	public Object abrirPuerto(String nombre_puerto)
	{	//Cogemos el identificador del puerto
		try
		{	this.port_id = CommPortIdentifier.getPortIdentifier(nombre_puerto);
		}
		catch(Exception e)
		{	return (String)e.getMessage();
		}
		
		//Intentamos abrir el puerto
		try
		{	puerto = (SerialPort) this.port_id.open("Impresión Etiquetas", TIMEOUT*1000);
		}
		catch(PortInUseException e)
		{	return "Ya hay otra aplicación usando el puerto "+nombre_puerto+".\nEscoge otro.";
		}
		catch(Exception e)
		{	return "Problema abriendo el puerto";
		}
		
		//Configuramos el puerto
		try
		{ puerto.setSerialPortParams(9600, SerialPort.DATABITS_8, SerialPort.STOPBITS_1, SerialPort.PARITY_NONE);
		}
		catch(UnsupportedCommOperationException e)
		{}
		
		//Creamos los canales de entrada/salida de datos
		try
		{	//inputStream = puerto.getInputStream();
			outputStream = puerto.getOutputStream();
			//puerto.notifyOnDataAvailable(true);
		}
		catch(IOException e)
		{	return (String) e.getMessage();
		}
		
		//Start the thread that reads the serial comm
		(new SerialReaderThread(app, this.puerto)).start();

		return "1";	//Todo OK
	}
	
	
	public void send(String loQue)
	{
		try
		{	this.outputStream.write(loQue.getBytes());
		}
		catch(IOException e)
		{}
		
		try
		{	Thread.sleep(100);
		}
		catch(Exception e)
		{}
	}
	
	
	//Cerrar puerto
	public void cerrarPuerto()
	{	try
		{	puerto.close();
		}
		catch(Exception e){}
	}

}