|
package skynet.smart;
import javax.comm.CommPortIdentifier;
import javax.comm.PortInUseException;
import javax.comm.UnsupportedCommOperationException;
import java.io.InputStream;
import javax.comm.SerialPort;
import java.io.OutputStream;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Properties;
public class CommSerie
{
static Enumeration listaPuertos;
static CommPortIdentifier idPuerto;
static Enumeration portList;
static String mensaje;
static SerialPort puertoSerie;
static InputStream entrada;
static OutputStream salida;
private String puerto="COM1"; //Por defecto
/**
* Constructor. Inicializa el puerto
*/
public CommSerie()
{
try{
// Lee de un fichero de propiedades el puerto a inicializar
Properties fichero = new Properties();
fichero.load(this.getClass().getResourceAsStream("config.properties"));
this.puerto = fichero.getProperty("puerto");
}catch(IOException e){}
this.mensaje = null;
listaPuertos = CommPortIdentifier.getPortIdentifiers();
while( listaPuertos.hasMoreElements() ) {
idPuerto = (CommPortIdentifier)listaPuertos.nextElement();
if( idPuerto.getPortType() == CommPortIdentifier.PORT_SERIAL ) {
if( idPuerto.getName().equals(puerto) ) {
// Si el puerto no est� en uso, se intenta abrir
try {
puertoSerie = ( SerialPort )idPuerto.open("SkynetSmartCard",2000);
} catch( PortInUseException e ) {}
// Se obtiene un canal de entrada
try {
entrada = puertoSerie.getInputStream();
} catch (IOException 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( 9600,
SerialPort.DATABITS_8,
SerialPort.STOPBITS_1,
SerialPort.PARITY_EVEN);
} catch( UnsupportedCommOperationException e ) {}
}
}
}
}
/**
* Metodo que le envia un reset a la tarjeta
*/
public void enviaReset(){
// En flanco de bajada inicia el ATR
puertoSerie.setRTS(true);
puertoSerie.setRTS(false);
}
/**
* Metodo que lee la respuesta del puerto
*/
public String leeRespuesta(){
//Se lee la respuesta
byte[] readBuffer = new byte[18];
String sol="";
try {
int numBytes = 0;
while (entrada.available() > 0){
numBytes = entrada.read(readBuffer);
}
sol = new String(readBuffer);
if (sol.length() > 13)
sol = sol.substring(11,13);
else
sol = "NULL";
} catch (IOException e) {}
return sol;
}
/**
* Metodo que cierra el puerto serie abierto
*/
public void cierraPuerto(){
puertoSerie.close();
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
I am a software developer grown with programming paradigm evolution. Think that C is God's Programming Language.