Subject: [Original] - under different platforms to access author parallel port: yangsongx (to achieve "international leader" to fill "gaps") level: credit value: 100 Quantity forum: C / C C language issue Points: 100 responses Number of people: 19 Posted: 2004-10-13 10:49:33
Primer
These days, I have organized the procedures for the pair of parallel to the pair, and I wrote a little, I hope to make good comments.
text start
* Foreword *
This paper is mainly to discuss the method of programming the parallel port under different platforms. Due to the limited conditions, it is only limited to the Intel386 system.
PC microcomputer structure.
Chapter 1 * Introduction *
The parallel port is widely used in stepper motor control, data acquisition, etc. This article is to discuss access to parallel ports under different platforms.
So far, parallel access programs are implemented under DOS, Windows 95/98, Windows NT / 2000 / XP and Linux.
There are at least one 25-pin side of the PC, we can easily find it behind the chassis. Among them, the 18th to 25th
The ground line, the second to 9-pin is the data output line, and the remaining state lines and input lines are left.
Chapter 2 * Realization *
On the PC, the parallel port has the corresponding register (register) that is mapped to the system address space.
According to the method of IBM design PC, the parallel entry base address on the PC is typically 0x378 and 0x278 (if there is a second parallel port).
The port mapping address is stored in the BIOS of the PC. According to the design of the BIOS, start from the low end 40 [0] h, there are 256 (100h) Byte's space,
Used to store the address of the external device of the PC. So we can also view these contents through tool debug, on the command line:
C: /> Debug
Enter the Debug environment, use D instructions to view the contents of 0x40 [0]:
-d 40:00
0040: 0000 F8 03 F8 02 E8 03 E8 02 BC 03 78 03 78 02 C0 9F
0040: 0010 22 C8 20 80 02 85 00 20 00 00 2e 00 2e 00 64 20
0040: 0020 20 39 34 4B 30 52 3A 27 30 52 30 52 0D E0 00 00
...
The device address is stored in 16-bit (2Byte). The offset amount is 0x00 ~ 0x07, which is 4 serial addresses (corresponding to COM1 ~ COM4).
The next 0x08 ~ 0x0f offset is 4 parallel addresses (corresponding to LPT1 to LPT4). In Intel's X86 system, memory storage
It is the Little-Endian order, that is, the small byte is in front. In this way, we can see the address of the LPT1 from the above memory.
Is 0x3bc, the LPT2 address is 0x378.
2.1 DOS, Win95 / 98 Concial Access
Access the parallel port in DOS, Win95 / 98, relatively simple, just call the following two functions:
#include
INT_OUTP (Unsigned Short Port, INT DATABYTE);
INT _INP (UNSIGNED SHORT Port);
Where _outp is output, _inp is input. For example, I want to write high levels (ie 0xFF) for 8 output data lines, and you can use: / * to make 8 output data lines high 1 * /
_outp (0xff, 0x378);
To make the first, 2, 3 data lines are high, then available:
_outp (0x07, 0x378);
The rest are pushed.
2.2 Access to the Concial Pixel in Win NT / 2000 / XP
Since the Windows NT / 2000 kernel is different from Windows 98/95, the method of accessing the parallel port is also adjusted. and also
That is to say, the methods mentioned earlier are under NT core, no longer practical! You can try the front _outp and _inp at NT.
Look, it will be found that when these two functions are executed, the program appears "Execute Illegal Programs" error box, and then the program is over. This is because
Under Windows NT / 2000, from security, the program will be granted a certain privilege (Restriction).
Typically, this privileged level will be divided into two: user mode and core mode. Running in user mode
The program level is Ring 3. The program level running in the core mode is Ring 0. And under Ring 3 is not allowed to access the port directly.
Therefore, the method of calling library functions used earlier _outp, etc., is unrelenting under Windows NT / 2000.
Since we understand the above reasons, we will understand: To achieve access to hardware ports under Windows NT / 2000,
A certain way to get RING 0 permissions. This job can be implemented by Windows DDK (Device Drive Kits). Windows
The lower DDK is also a series of API functions, just these functions are specifically written for hardware drivers under Windows. Due to the driver
With the permissions running in Ring 0, the user program cannot enter the contradiction in Kernel mode. Another question to solve
The title is that the driver and user program are defined to define a communication interface. In this way, the user program is implemented through the driver interface defined.
Access to parallel ports. The way we use is to first use DDK to write a driver that can access the parallel port (usually XXXX.sys model file),
Thereafter, the exposed interface is specified by a DLL file.
Fortunately, some people have done the corresponding drivers for the corresponding access to our visitors in this regard, and provide the source code to let us
Learn. You can find the source code and use documentation in References [1]. The document is written very clear, detailed, here I don't say much.
2.3 Access to Pixabay under Linux
Due to Linux's openness and high user customizability, let Linux support parallel port, first let Linux kernel support parallel.
Generally we put the parallel port support function in the kernel when we install Linux from the disc. But if you are upgrading the kernel, compile the kernel
I have to pay attention: Don't remove the parallel port support function. Otherwise, you will find that it is "not willing to" to generate signals. To make the kernel support parallel port
(If everyone is installed Linux from the CD, don't the following steps, because the system will automatically load the parallel port support function),
Set the "Parallel Port Support" option when setting the system core. This option can be in Make Config or make Xconfig
Or Make Gconfig or make menuConfig is found. If you don't understand the above commands, you can refer to compiled documents.
In this way, when compiling the core, we predefine the macro config_parport, the compiler is notified to support the parallel port support part of the code.
Enter the kernel. Of course, we can also compile the parallel port support function into a module (the location of these modules is in the "/ lib / module / kernel version number / kernel / drivers / parport" directory, the corresponding file is PARPORT.O, PARPORT_PC.O, etc.
When you need to use a parallel port, use the command insmod, rmmod to load, remove the parallel support function. Where INSMOD refers to "INSERT MODULE",
RMMOD refers to "Remove Module".
After determining the Linux kernel supports the parallel port function, we can enter the following topics.
Visit Linux comparison function, and the previously described Windows95 / 98 _outp is extremely similar: OUTB, INB function,
The parameter meanings are also the same as the previous _outp, _inp.
However, it should be pointed out that Linux also uses similar protection measures similar to Windows NT / 2000 to prohibit user processes directly to hardware ports.
Access (user spatial program does not allow access to content in the kernel space). In order to simplify the parallel journey, Linux provides a Helper Function.
This is IOPERM. This function can tell the kernel: give a certain control and user process, and then give control rights when the user program is over
come back. If we don't call IOPERM in advance, the program that access to the parallel port can be compiled, but it will be wrong at runtime, which is generally on the screen.
The error prompt appears "Segment Failure" appears.
The two function prototypes used are as follows:
#include
INT IOPERM (Unsigned long from, unsigned long num, int turn_on);
Void outb (unsigned char BYTE, UNSIGNED Port);
The IOPERM is the starting from the start until NUM byte, and the I / O map address within this range can be accessed for ordinary user program. on
The behavior is permitted when the TURN_ON parameter is "true" (non-zero). This behavior is prohibited when "fake". This can be done in the program,
The call is no longer adjusted when the hardware port is accessible.
The sample code for accessing the parallel port under Linux is given below:
...
IOPERM (0x000, 0x3FF, 1); // open the space between 0x000 ~ 0x3FF to the user program
// This range is enough for access to the parallel port.
OUTB (0xFF, 0x378); / / write data to the parallel port data. Here is the 8 data bits all in 1
...
IOPERM (0x000, 0x3FF, 0); // Remove the control, the user program will no longer be able to
// Access the contents of the kernel space.
Chapter 3 * Summary *
So far, the programming of parallel port is already implemented in the following platform:
* Windows 98 SE
* Windows 2000 PROFESSIONAL
* Linux (Redhat 9.0 Kernel 2-4-20)
* Linux (Slackware 9.1 Kernle 2-4-22)
*references*
[1] Accessing Parallel Port. Http://www.logix4u.net/index.htm