Bueno para la creacion de nuestra aplicacion utlizamos JCreator y tres clases.java las cuales las mostraremos acontinucion. Solo copiar y pegar en JCreator para probar el codigo y que no tenga errores. Las dos primeras clases corresponden al codigo de manejo de puertos y la ultima es la aplicacion que realizamos para enviar el dato en decimal por teclado y mostralo en decimal en los leds y en hexadecimal en el display de 7 segmentos.
1. La clase ioport.java
public class ioPort
{
// declare native methods of 'jnpout32.dll'
// output a value to a specified port address
public native void Out32(short PortAddress, short data);
// input a value from a specified port address
public native short Inp32(short PortAddress);
// load 'jnpout32.dll'
static { System.loadLibrary("jnpout32");}
}
{
// declare native methods of 'jnpout32.dll'
// output a value to a specified port address
public native void Out32(short PortAddress, short data);
// input a value from a specified port address
public native short Inp32(short PortAddress);
// load 'jnpout32.dll'
static { System.loadLibrary("jnpout32");}
}
2. La clase pPort.java
public class pPort
{
ioPort pp; // wrapper class for 'Jnpout32.dll'
// with methods:
// int Out32(int port, int value);
// int Inp32(int port);
short portAddress; // address of data port
short currentVal; // current value of port bits
public pPort()
{
pp = new ioPort();
portAddress = (short)0x378; // Hex Address of Data Byte of PC Parallel Port
setAllDataBits((short)0); // initialize port bits to 0
currentVal = 0x00;
}
// wrap ParallelPort output method
public void output(short port, short value)
{
pp.Out32(port, value);
}
// wrap ParallelPort input method
public short input(short port)
{
return pp.Inp32(port);
}
// output to default Data port
public void output(short value)
{
pp.Out32(portAddress, value);
}
// input from default Data port
public short input()
{
return pp.Inp32(portAddress);
}
/**
* set all bits on Data port to zero
**/
public void setAllDataBits(short value)
{
pp.Out32(portAddress, value);
currentVal = value;
}
// For users who prefer dealing with Pin numbers
// Set Pin <pin> to <value>
public void setPin(short pin, short value)
{
if (pin >= 2 && pin <= 9)
// just set the corresponding Data bit to indicted value
setDataBit((short)(pin-2), value);
}
/**
* Set Data Bit at selected index to a value of 1 or 0
* while preserving current values of all other Data bits
**/
void setDataBit(short index, short value)
{
switch(index)
{
case 0:
if (value==0) // Set Data[0] to 0
currentVal = (short) (currentVal & 0xFE);
// aaaa aaaa currentVal
// AND 1111 1110 mask
// =========
// aaaa aaa0 new currentVal
else // Set Data[0] to 1
currentVal = (short) (currentVal | 0x01);
// aaaa aaaa currentVal
// OR 0000 0001 mask
// =========
// aaaa aaa1 new currentVal
break;
case 1:
if (value==0)
currentVal = (short) (currentVal & 0xFD);
// currentVal = aaaa aa0a
else
currentVal = (short) (currentVal | 0x02);
// currentVal = aaaa aa1a
break;
case 2:
if (value==0)
currentVal = (short) (currentVal & 0xFB);
// currentVal = aaaa a0aa
else
currentVal = (short) (currentVal | 0x04);
// currentVal = aaaa a1aa
break;
case 3:
if (value==1)
currentVal = (short) (currentVal & 0xF7);
// currentVal = aaaa 0aaa
else
currentVal = (short) (currentVal | 0x08); // currentVal = aaaa 1aaa
break;
case 4:
if (value==0)
currentVal = (short) (currentVal & 0xEF);
// currentVal = aaa0 aaaa
else
currentVal = (short) (currentVal | 0x10); // currentVal = aaa1 aaaa
break;
case 5:
if (value==0)
currentVal = (short) (currentVal & 0xDF);
// currentVal = aa0a aaaa
else
currentVal = (short) (currentVal | 0x20); // currentVal = aa1a aaaa
break;
case 6:
if (value==0)
currentVal = (short) (currentVal & 0xBF);
// currentVal = a0aa aaaa
else
currentVal = (short) (currentVal | 0x40); // currentVal = a1aa aaaa
break;
case 7:
if (value==0)
currentVal = (short) (currentVal & 0x7F);
// currentVal = 0aaa aaaa
else
currentVal = (short) (currentVal | 0x80); // currentVal = 1aaa aaaa
break;
default:
System.out.println("index must be 0 - 7");
}
pp.Out32(portAddress, currentVal);
}
}
Importante: Antes de ejecutar el programa debes copiar el archivo jnpout32.dll en la misma carpeta donde estan las aplicaciones de java anteriormente nombradas. El archivo junto con las dos apliciones lo puedes descargar de la siguiente pagina:
{
ioPort pp; // wrapper class for 'Jnpout32.dll'
// with methods:
// int Out32(int port, int value);
// int Inp32(int port);
short portAddress; // address of data port
short currentVal; // current value of port bits
public pPort()
{
pp = new ioPort();
portAddress = (short)0x378; // Hex Address of Data Byte of PC Parallel Port
setAllDataBits((short)0); // initialize port bits to 0
currentVal = 0x00;
}
// wrap ParallelPort output method
public void output(short port, short value)
{
pp.Out32(port, value);
}
// wrap ParallelPort input method
public short input(short port)
{
return pp.Inp32(port);
}
// output to default Data port
public void output(short value)
{
pp.Out32(portAddress, value);
}
// input from default Data port
public short input()
{
return pp.Inp32(portAddress);
}
/**
* set all bits on Data port to zero
**/
public void setAllDataBits(short value)
{
pp.Out32(portAddress, value);
currentVal = value;
}
// For users who prefer dealing with Pin numbers
// Set Pin <pin> to <value>
public void setPin(short pin, short value)
{
if (pin >= 2 && pin <= 9)
// just set the corresponding Data bit to indicted value
setDataBit((short)(pin-2), value);
}
/**
* Set Data Bit at selected index to a value of 1 or 0
* while preserving current values of all other Data bits
**/
void setDataBit(short index, short value)
{
switch(index)
{
case 0:
if (value==0) // Set Data[0] to 0
currentVal = (short) (currentVal & 0xFE);
// aaaa aaaa currentVal
// AND 1111 1110 mask
// =========
// aaaa aaa0 new currentVal
else // Set Data[0] to 1
currentVal = (short) (currentVal | 0x01);
// aaaa aaaa currentVal
// OR 0000 0001 mask
// =========
// aaaa aaa1 new currentVal
break;
case 1:
if (value==0)
currentVal = (short) (currentVal & 0xFD);
// currentVal = aaaa aa0a
else
currentVal = (short) (currentVal | 0x02);
// currentVal = aaaa aa1a
break;
case 2:
if (value==0)
currentVal = (short) (currentVal & 0xFB);
// currentVal = aaaa a0aa
else
currentVal = (short) (currentVal | 0x04);
// currentVal = aaaa a1aa
break;
case 3:
if (value==1)
currentVal = (short) (currentVal & 0xF7);
// currentVal = aaaa 0aaa
else
currentVal = (short) (currentVal | 0x08); // currentVal = aaaa 1aaa
break;
case 4:
if (value==0)
currentVal = (short) (currentVal & 0xEF);
// currentVal = aaa0 aaaa
else
currentVal = (short) (currentVal | 0x10); // currentVal = aaa1 aaaa
break;
case 5:
if (value==0)
currentVal = (short) (currentVal & 0xDF);
// currentVal = aa0a aaaa
else
currentVal = (short) (currentVal | 0x20); // currentVal = aa1a aaaa
break;
case 6:
if (value==0)
currentVal = (short) (currentVal & 0xBF);
// currentVal = a0aa aaaa
else
currentVal = (short) (currentVal | 0x40); // currentVal = a1aa aaaa
break;
case 7:
if (value==0)
currentVal = (short) (currentVal & 0x7F);
// currentVal = 0aaa aaaa
else
currentVal = (short) (currentVal | 0x80); // currentVal = 1aaa aaaa
break;
default:
System.out.println("index must be 0 - 7");
}
pp.Out32(portAddress, currentVal);
}
}
Importante: Antes de ejecutar el programa debes copiar el archivo jnpout32.dll en la misma carpeta donde estan las aplicaciones de java anteriormente nombradas. El archivo junto con las dos apliciones lo puedes descargar de la siguiente pagina:
Fuente:http://www.psicofxp.com/forums/programacion.313/425082-control-de-leds-puerto-paralelo-java.html
3. Por ultimo Aplicacion.java
import javax.swing.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Aplicacion extends JFrame implements ActionListener{
public static pPort lpt = new pPort();
JLabel l1= new JLabel ("Digite aqui el numero");
JTextField t1 = new JTextField(10);
JButton b1= new JButton ("Entrar");
JPanel p1= new JPanel(null);
public Aplicacion()
{
setTitle ("Control de Leds por Puerto Paralelo");
setSize (250,200);
setDefaultCloseOperation(3);
setLocation(100,100);
setContentPane(p1);
p1.add(l1); l1.setBounds(60,20,200,20);
p1.add(t1); t1.setBounds(20,40,200,20);
p1.add(b1); b1.setBounds(60,100,100,40);
b1.addActionListener (this);
}
public void actionPerformed (ActionEvent e)
{
int num;
num=Integer.parseInt(t1.getText());
if (e.getSource()==b1){lpt.output((short)num);}
}
public void mensaje(String texto){JOptionPane.showMessageDialog(this,texto);}
public static void main(String[] args) {
JFrame.setDefaultLookAndFeelDecorated(true);
JDialog.setDefaultLookAndFeelDecorated(true);
Aplicacion a1 = new Aplicacion();
a1.setVisible(true);
}
}
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Aplicacion extends JFrame implements ActionListener{
public static pPort lpt = new pPort();
JLabel l1= new JLabel ("Digite aqui el numero");
JTextField t1 = new JTextField(10);
JButton b1= new JButton ("Entrar");
JPanel p1= new JPanel(null);
public Aplicacion()
{
setTitle ("Control de Leds por Puerto Paralelo");
setSize (250,200);
setDefaultCloseOperation(3);
setLocation(100,100);
setContentPane(p1);
p1.add(l1); l1.setBounds(60,20,200,20);
p1.add(t1); t1.setBounds(20,40,200,20);
p1.add(b1); b1.setBounds(60,100,100,40);
b1.addActionListener (this);
}
public void actionPerformed (ActionEvent e)
{
int num;
num=Integer.parseInt(t1.getText());
if (e.getSource()==b1){lpt.output((short)num);}
}
public void mensaje(String texto){JOptionPane.showMessageDialog(this,texto);}
public static void main(String[] args) {
JFrame.setDefaultLookAndFeelDecorated(true);
JDialog.setDefaultLookAndFeelDecorated(true);
Aplicacion a1 = new Aplicacion();
a1.setVisible(true);
}
}




