📄 giaotiep lpt.htm
字号:
<P><PRE>Format of BIOS Data Segment at segment 40h:Offset Size Description 08h WORD Base I/O address of 1st parallel I/O port, zero if none 0Ah WORD Base I/O address of 2nd parallel I/O port, zero if none 0Ch WORD Base I/O address of 3rd parallel I/O port, zero if none 0Eh WORD [non-PS] Base I/O address of 4th parallel I/O port, zero if none</PRE>For example change the line <B>addr:=MemW[$0040:$0008];</B>in the source code to <B>addr:=MemW[$0040:$000A];</B> if you want to output to LPT2.<P>Instead of trying to read the address from the DOS information data block you can always use the I/O address fixed to source code. The LPT1 port is typically at I/O-address 378h or 3BCh.<P>To get to know the port address to use you can use for example the method: On modern Windows systems (I tested in Windows 2000) you can get to know the parallel port I/O addrss through device manager. First open the device manager (start - settings - control panel - system - hardware - device manager). Then select there the parallel port you are interrested from Ports (COM & LPT) section. With mouse right button you can get menu where you select Properties. From there select Resources where you should see a screen like this:<P><img src="io_properties.gif" alt="Windows device control IO address view"><P>The details in this image are from the parallel port built into the motherboard of my PC. <P><H2><A NAME="dosprogramming">Parallelport programming in DOS</A></H2><P>The following examples are short code examples howto write to I/O ports using different languages.In the examples I have used I/O address 378h which is oneof the addresses where parallel port can be. <P>The typical parallel port I/O addess configurations seen on PCs with ISA bus:<UL> <LI>LPT1: 3BCh, LPT2: 378h, LPT3: 278h <LI>LPT1: 378h, LPT2: 278h <LI>LPT1: 378h</UL>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). <P>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.<P><H3>Assembler</H3><PRE>MOV DX,0378HMOV AL,nOUT DX,AL </PRE>Where n is the data you want to output.<P><H3>BASIC</H3><PRE>OUT &H378, N</PRE>Where N is the number you want to output.<P><H3>C</H3><PRE> outp(0x378,n);</PRE>or<PRE>outportb(0x378,n);</PRE>Where N is the data you want to output. The actualI/O port controlling command varies from compilerto compiler because it is not part of standardizedC libraries.<P>Here is an example source code for Borland C++ 3.1 compiler:<PRE>#include <stdio.h>#include <dos.h>#include <conio.h>/********************************************//*This program set the parallel port outputs*//********************************************/void main (void){clrscr(); /* clear screen */outportb(0x378,0xff); /* output the data to parallel port */getch(); /* wait for keypress before exiting */}</PRE><P><H3>Using DOS debug to access parallel port</H3><P>DOS program Debug is a simple 8088 assembler that comes with DOS operating system (comes with DOS utilities on most modern WIndows systems as well).Debug allows debugging of simple 16-bit DOS applications (not useful to modern 32-bit Windows programs). Debug program has several built-in debugging tool commands, including commands to read and write I/O ports.<PRE>o- writes one byte of dat to the specified I/O portSYNTAX o port valueport - specifies the port address. The port address can be an 8 or a 16 bit value.value - specified the value to write to this I/O-port. This value is 8 bit value.i- reads one byte of data from the specified I/O portSYNTAX i portport - specifies the port address. The port address can be an 8 or a 16 bit value.</PRE><P>Examples:<PRE>If you type o 3bc ffdebug will output value ff (hex) to port 3bc (hex).If you type i 3bc debug will display 1 byte of data from the parallel port.</PRE><P><H2><A NAME="windowsprogramming">Parallel port controlling in Windows programs</A></H2><P>Writing programs to talk with parallel port was pretty easy in old DOS days and in Win95/98 too. We can use Inporb and outportb or _inp() or _Outp functions in our program without any problem if we are running the program on Dos or WIN95/98. But entering to the new era of NT clone operating systems like WIN NT4, WIN2000, WINXP, all this simplicity goes away. <P>Direct parallel port controlling in possible under Windows 3.x andWindows 95 directly from 16 bit application programs and DLL libraries.So you can use the C example above in Windows 3.x and Windows 95 if youmake your program 16 bit application. If you want to control parallelport from Visual Basic or Delphi then take a look at the libraries at<A HREF="http://www.epanorama.net/counter.php?url=http://www.lvr.com/parport.htm">Parallel Port Central</A> at<A HREF="http://www.epanorama.net/counter.php?url=http://www.lvr.com/parport.htm">http://www.epanorama.net/counter.php?url=http://www.lvr.com/parport.htm</A>.<P>Direct port controlling from application is not possible under Windows NTand to be ale to control the parallel port directly you will need to writesome kind of device driver to do this. You can find also thiskind of drivers from <A HREF="http://www.epanorama.net/counter.php?url=http://www.lvr.com/parport.htm#Programming">Parallel Port Central</A> and <A HREF="http://www.epanorama.net/counter.php?url=http://www.logix4u.net/inpout32.htm">Inpout32.dll for WIN NT/2000/XP</A>. <P><A HREF="http://www.epanorama.net/counter.php?url=http://www.driverlinx.com/DownLoad/DlPortIO.htm">Driverlinx PortIO</A> at <A HREF="http://www.epanorama.net/counter.php?url=http://www.driverlinx.com/DownLoad/DlPortIO.htm">http://www.epanorama.net/counter.php?url=http://www.driverlinx.com/DownLoad/DlPortIO.htm</A> is a worth to check driver for accessing I/O ports directly under Windows 95/NT (works also well with Windows 2000). This free software comes with example programs (both executable and source code available) how to access I/O ports from Visual Basic and Microsoft Visual C programs. <P>The I/O Control Using Using Visual Basic web page at <A HREF="http://www.epanorama.net/counter.php?url=http://www.southwest.com.au/~jfuller/vb/vbout.htm">http://www.epanorama.net/counter.php?url=http://www.southwest.com.au/~jfuller/vb/vbout.htm</A> describes how to make a simple Visual Basic application that controls PC parallel port.<P>If you are looking for a ready made software, then take a look at Kemo M125 kit web page at <A HREF="http://www.epanorama.net/counter.php?url=http://www.kemo-electronic.com/en/module/m125/">http://www.epanorama.net/counter.php?url=http://www.kemo-electronic.com/en/module/m125/</A>. Kemo relay module M125 is designed for switching up to 8 different appliances, lamps or motors according to a computer program (up to 40V and loads up to 0,4A DC or 0,2A AC). The module is operated at the printer port LPT1 in the same way as my parallel port controlling circuit examples (in this module there is one solid state relay connected for each of those 8 data output pin on parallel port). Kemo M125 kit information page has control software available for download. Those software allows controlling of outputs manually and timed operation. Windows software runs in WIN9x, WIN2K and WINXP.There is also programming C source code example available. <P>Parallel port relay relay board kit described at <A HREF="http://www.epanorama.net/counter.php?url=http://electronickits.com/kit/complete/elec/ck1601.htm">http://www.epanorama.net/counter.php?url=http://electronickits.com/kit/complete/elec/ck1601.htm</A> comes with Windows and DOS control software that can be downloaded at the kit page. This software runs at Windows 9x/2000/ME/XP. Information on DOS utilities can be found at <A HREF="http://www.epanorama.net/counter.php?url=http://www.qkits.com/serv/qkits/diy/pages/QK74.asp">http://www.epanorama.net/counter.php?url=http://www.qkits.com/serv/qkits/diy/pages/QK74.asp</A>.<P>Parallel port monitors page at <A HREF="http://www.epanorama.net/counter.php?url=http://neil.fraser.name/software/lpt/">http://www.epanorama.net/counter.php?url=http://neil.fraser.name/software/lpt/</A> has programs that allow you to set and monitor parallel port pin states. The software is available for few different versions that work on Windows 98 / ME / NT / 2000 / XP and DOS / Windows 3.1 / 95 / 98 / ME systems. The software is written using Visual Basic and Euphoria programming languages and comes with source code.<P><A HREF="http://www.epanorama.net/counter.php?url=http://www.beyond-designs.com/PC_ports.htm">Beyond Design PC Serial and Parallel Port Software and Interfaces software VBPortTest</A> at <A HREF="http://www.epanorama.net/counter.php?url=http://www.beyond-designs.com/PC_ports.htm">http://www.epanorama.net/counter.php?url=http://www.beyond-designs.com/PC_ports.htm</A> is an useful utility. VBPortTest parallel port utility is designed to help test and debug parallel port interfaces. Allows access to the three registers (data, status, and control) associated with the PC standard parallel port (SPP). The user can read and write the data and control registers. The program continually reads the status register (the status register is read-only) . Individual register bits are displayed on LEDs along with the hex value for the entire data register. In write mode, the user can toggle individual bits by clicking on the corresponding LED. Hex values can be entered on the keyboard. Bit, byte and strobed byte write modes are possible. Online Help with useful parallel port reference material includes signal descriptions and Centronics handshake timing waveform. VBPortTest is available for download as freeware. Windows 98, ME, and XP compatible. <P>If you want to do programming of parallel port in Windows with C/C++ then one way to go is to use using the inpout32.dll and the free borland C compiler. For more info, you can check out <A HREF="http://hytherion.com/beattidp/comput/pport.htm">http://hytherion.com/beattidp/comput/pport.htm</A> and <A HREF="http://csjava.occ.cccd.edu/~gilberts/bcc55.html">http://csjava.occ.cccd.edu/~gilberts/bcc55.html</A>. The <A HREF="http://csjava.occ.cccd.edu/~gilberts/bcc55.html">second link</A> was a very nice, step by step guide as to how to do things.<P><H3>Standard Windows API interface</H3><P>Standard Windows API for parallel port programming is targeted for sending out characters to printer onneced to parallel port. It is not designed for contrlling single pins on or off.<P>Using the parallel port for digital output can be made to work with the normal Windows API with a simple hardware hack. The main trick is to tie pins 11(Busy) and 12 (Paper Error) to ground. Otherwise, the hardware driver will think the printer it is talking to is busy or experiencing an error and will not output any data. <P>Just send one character to parallel port, and the Windows wii send the value of that character to printer port data pins, plus generate one pulse to strobe line. The port will maintain the last value written to it until another value is written or until the computer is powered down or new data is sent to port. If you output more than one byte at a time the driver will send them to the port in sequence and toggle the Strobe line (line 1) off and on for each byte. The timing involved varies somewhat from one computer to the next.<P>This means that in a Windows environment we can output data to the parallel port. You can send data even through Windows command line with copy command like this:<pre>copy somefile.bin LPT1</pre><P>This will send the contents of file somefile.bin to the parallel port. The value of the last byte in the file will be left as the state of the parallel port after the command execution. <P>Besides need for the hardware hack, there are other limitations on the Windows API method. Windows API does not have built in support for input operations nor reading back the last sent value. Even though the parallel port hardware supports it, the software driver does not. <P><H2><A NAME="linuxprogramming">Parallel port controlling in Linux</A></H2><P>Linux will allow acess to any port using the ioperm syscall.Here is some code parts for Linux to write 255 to printer port:<PRE>#include <stdio.h>#include <stdlib.h>#include <unistd.h>#include <asm/io.h>#define base 0x378 /* printer port base address */#define value 255 /* numeric value to send to printer port */main(int argc, char **argv){
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -