Sei sulla pagina 1di 8

Reading from Parallel Port using Inpout32.

dll
PC parallel port is 25 pin D-shaped female connector in the back of the computer. It is normally
used for connecting computer to printer, but many other types of hardware for that port is
available today.

Not all 25 are needed always. Usually you can easily do with only 8 output pins (data lines) and
signal ground. I have presented those pins in the table below. Those output pins are adequate for
many purposes.

pin function
2 D0
3 D1
4 D2
5 D3
6 D4
7 D5
8 D6
9 D7
Pins 18,19,20,21,22,23,24 and 25 are all ground pins

Parallel port programming in DOS


The following examples are short code examples how to write to I/O ports using different
languages. In the examples I have used I/O address 378h which is one of the addresses
where parallel port can be.

The typical parallel port I/O addess configurations seen on PCs with ISA bus:

 LPT1: 3BCh, LPT2: 378h, LPT3: 278h


 LPT1: 378h, LPT2: 278h
 LPT1: 378h

Those are the typical I/O addresses used in ISA bus based systems. In PCI bus based
systems the LPT1 port on motherboard is typically at I/O-address 378h or 3BCh. If the
systems has extra LPT ports on multi-IO card in PCI bus, those extra LPT ports work
differently than the "normal parallel port" described in this document, and the same
control methods can't be applied to them (they are on different I/O addresses and could
use different control register system that could be card specific, the driver software that
comes with the card makes them to look like LPT ports for the applications using standard
operating system printing routines).

The following examples are for DOS system (they might or might not work on other
systems). The code examples are designed to be used with LPT1 port that resides in I/O
address 378h.
Assembler

MOV DX, 0378H


MOV AL, n
OUT DX, AL
Where n is the data you want to output.

Reading the input pins in parallel port input pins


PC parallel port has 5 input pins. Those inputs can accept TTL level signals (0-0.7V = logic 0,
2.4-5V = logic 1). You can connect a TTL level output signal to it directly we can connect spiel
switches to the inputs by connecting the switch between parallel port ground and input pin, and
then adding a 10 kohm pull-up resistor from the pin to +5V. When the switch is activated, the pin
goes to logic state 0. Usually it is a good idea to isolate the PC from the signal source, and in this
case it is usually a good idea to use a relay or opt coupler (the relay/opt coupler output is wired in
the place of the switch, otherwise works in the same way as the switch connection).

The input pins can be read from the I/O address LPT port base address + 1.

The meaning of the buts in byte you read from that I/O port:

 D0: state not specified


 D1: state not specified
 D2: state not specified
 D3: state of pin 15 (ERROR) inverted
 D4: state of pin 13 (SELECTED)
 D5: state of pin 12 (PAPER OUT)
 D6: state of pin 10 (ACK)
 D7: state of pin 11 (BUSY) inverted

Here are some code snippets to read LPT port:

Assembler
MOV DX, 0379H
IN AL, DX
You get the result for read from AL register

Parallel port interfacing in Win32, using Java JNI


NoThis is a modified version of Inpout32.DLL, to use the JNI (Java Native Interface). w,
JnpOut32 works as a package

//import java.lang.Integer;
public class ioTest
{
static short datum;
static short Addr;
static pPort lpt;

static void do_read()


{
// Read from the port
datum = (short) lpt.input(Addr);

// Notify the console


System.out.println("Read Port: " + Integer.toHexString(Addr) +
" = " + Integer.toHexString(datum));
}

static void do_write()


{
// Notify the console
System.out.println("Write to Port: " + Integer.toHexString(Addr) +
" with data = " + Integer.toHexString(datum));
//Write to the port
lpt.output(Addr,datum);
}

static void do_read_range()


{
// Try to read 0x378..0x37F, LPT1:
for (Addr=0x378; (Addr<0x380); Addr++) {

//Read from the port


datum = (short) lpt.input(Addr);

// Notify the console


System.out.println("Port: " + Integer.toHexString(Addr) +
" = " + Integer.toHexString(datum));
}
}

public static void main( String args[] )


{

lpt = new pPort();


// Try to read 0x378..0x37F, LPT1:

do_read_range();

// Write the data register

Addr=0x378;
datum=0x77;
do_write();

// And read back to verify


do_read();

// One more time, different value


datum=0xAA;
do_write();

// And read back to verify


do_read();

// etc...

Addr++;
do_read();

Addr--;
do_read();

do_read_range();

}
//======================================================================
//========================================================================
//============

/* Definitions in the build of jnpout32.dll are: */


/* short _stdcall Inp32(short PortAddress); */
/* void _stdcall Out32(short PortAddress, short data); */
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");}
}

//========================================================
//=========================================================
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);
}

Potrebbero piacerti anche