Delphi operates in parallel!

zhaozj2021-02-16  64

Parallel port is referred to as parallel, it has 3 ports: data port, status port, control port, common parallel port is LPT1, and its 3 ports of the 3 ports are: 378h, 379h and 37ah.

First, parallel reading and writing

In assembly language, you can operate parallel with in, OUT instructions, and no corresponding function in Delphi, the method can read and write parallel port, fortunately, Delphi can embed assembler, embedding the contractual instruction in, OUT It is convenient to read and write parallel ports. We can also access the parallel port by calling the Windows API function or a third party, but VxD is access to the parallel port, but by using the embedded assembly method, it is more convenient and fast.

Use the following READPORT functions and WritePort procedures to read and write parallel ports, parameter port is the port address to operate.

Function Readport (Port: Word): byte;

VAR

B: BYTE;

Begin

ASM

MOV DX, Port;

IN Al, DX;

MOV B, Al;

END;

Result: = B;

END;

Procedure Writeport (Port: Word; Conbyte: Byte);

Begin

ASM

MOV DX, Port;

MOV Al, Conbyte;

OUT DX, Al;

END;

END;

Second, bit operation

To control the parallel port, you can read the data of the parallel port, then perform the bit operation, and then rewrite the parallel port, you can implement control of the bit of the parallel port.

The logical operator AND to perform the logical and operation of the number of two to operate: ie 1 "and" 1 of 1, other 0 "and" 1, 1 "and" 0, 0 " 0 The result is 0.

The logical operator or the logic or calculation of the two to operate: ie, as long as the two digits of "or" is 1, the result is 1; otherwise, "or" the result is 0.

Using the AND operator, the specified position 0, for example: hexadecimal 84H binary: 10000100, its third bit is 1, to set the third position of 0, and other bit constant, you can use: The binary value of $ 84 and $ fb = 80, 80h is 10000000.

The second bit of the specified position 1, for example: sixteen-entered 84h, if the second position is 1, and other bit constant, can be used: $ 84 or $ 02 = $ 86, 86h The binary value is 10000110.

example:

1. Set the potential of the D2 bit of the data port 378h to low, that is, 0:

B: = Readport ($ 378);

B: = B and $ FB;

Writeport ($ 378, b);

2. Set the potential of the D2 bit of the data port 378h to high, that is, 1:

B: = Readport ($ 378);

B: = B OR $ 04;

Writeport ($ 378, b);

3. Judging the potential of the D2 bit of the data port 378h:

B: = Readport ($ 378);

IF ((B And $ 04) = $ 04) THEN

// Code of potential is high

Else

// Potential is low-time code

or:

B: = Readport ($ 378);

IF ((b or $ fb) = $ ff) THEN

// Code of potential is high

Else

// Potential is low-time code

Third, specific implementation

The following example is the potential of the respective bits of the data port 378h of the parallel port 378h. 8 bits of the data port: D0 ~ D7 correspond to 2 to 9 feet of the parallel interface, and the description of the parallel interface other is to view the relevant information, here is not much. First run Delphi, create a new project, press the F12 to join the code of the read and write port in Unit1 in Form1:

Function Readport (Port: Word): byte;

Procedure Writeport (Port: Word; Conbyte: Byte);

Function Readport (Port: Word): byte;

VAR

B: BYTE;

Begin

ASM

MOV DX, Port;

IN Al, DX;

MOV B, Al;

END;

Result: = B;

END;

Procedure Writeport (Port: Word; Conbyte: Byte);

Begin

ASM

MOV DX, Port;

MOV Al, Conbyte;

OUT DX, Al;

END;

END;

Add 8 Checkbox components to modify their CAPTION attributes to D0 to D7, respectively, and arrange them on right to left.

Double-click Checkbox1, add the following program code to the checkbox1 component's OnClick event:

Procedure TFORM1.CHECKBOX1CLICK (Sender: TOBJECT);

VAR

B: BYTE;

Begin

B: = 0;

if CheckBox1.checked Then

B: = B OR $ 01;

if Checkbox2.checked THEN

B: = b or $ 02;

If Checkbox3.checked Then

B: = B OR $ 04;

if Checkbox4.checked THEN

B: = B or $ 08;

if Checkbox5.checked THEN

B: = B OR $ 10;

if Checkbox6.checked THEN

B: = B OR $ 20;

If Checkbox7.checked Then

B: = B OR $ 40;

if Checkbox8.checked THEN

B: = B OR $ 80;

Writeport ($ 378, b); // Write data port

END;

After the input is complete, set CheckBox2 to Checkbox8's ONClick event for the checkbox component to checkbox1: CheckBox1click.

At this time, the compilation running program has been able to control the potential of each bit of the LPT1 data port by clicking this 8 Checkbox.

Then add the function of the data port status of the monitoring parallel port.

Add a TIMER component in Form1: Timer1, modify its enabled attribute to false, the interval property is 1.

Add:

Procedure TFORM1.TIMER1TIMER (Sender: TOBJECT);

VAR

B: BYTE;

Begin

B: = Readport ($ 378); // Read Data Port

Checkbox1.checked: = ((b or $ fE) = $ ff);

Checkbox2.checked: = ((b or $ fd) = $ ff);

Checkbox3.checked: = ((b or $ fb) = $ ff);

Checkbox4.checked: = ((b or $ f7) = $ ff);

Checkbox5.checked: = ((b or $ EF) = $ ff); checkbox6.checked: = ((b or $ df) = $ ff);

Checkbox7.checked: = ((b or $ bf) = $ ff);

Checkbox8.checked: = ((b or $ 7f) = $ ff);

END;

Add a Checkbox component, the modified CAPTION property is "Monitor Parallel Port" and adds in its OnClick event:

Procedure TFORM1.CHECKBOX9CLICK (Sender: TOBJECT);

Begin

Timer1.enabled: = Checkbox9.checked;

END;

Compile the running program, click "Monitor Parallel Port" to monitor the status of the parallel LPT1 data port 378h and modify its status in real time.

In order to facilitate viewing, verify the status of the data port 378h, I made a small parallel parallel test circuit, which uses a print interface, 8 LED (LED) and 8 1K resistors, the connection line is shown :

After the circuit diagram is completed, install it to the parallel port of the computer, run the programmed program to easily view the potential of each bit of the data port 378h.

Finally, let's make another walking lantern experiment.

First declare a global variable TB: add "TB: BYTE" below "Form1: TForm1":

VAR

FORM1: TFORM1;

TB: BYTE;

Add a Timer and a Checkbox in Form1, modify Timer2's enabled attribute to false, modify the interval property of 300, double-click Timer2, join in its ONTIMER event:

Procedure TFORM1.TIMER2TIMER (Sender: TOBJECT);

VAR

B: BYTE;

Begin

IF TB = 0 THEN

TB: = 1

Else

Tb: = TB * 2;

WRITEPORT ($ 378, TB);

B: = Readport ($ 378);

Checkbox1.checked: = ((b or $ fE) = $ ff);

Checkbox2.checked: = ((b or $ fd) = $ ff);

Checkbox3.checked: = ((b or $ fb) = $ ff);

Checkbox4.checked: = ((b or $ f7) = $ ff);

Checkbox5.checked: = ((b or $ EF) = $ ff);

Checkbox6.checked: = ((b or $ df) = $ ff);

Checkbox7.checked: = ((b or $ bf) = $ ff);

Checkbox8.checked: = ((b or $ 7f) = $ ff);

END;

Modify the CAPTION attribute of the checkbox10 is "Take the Turning Mark", double-click CheckBox10, join in its onclick event:

Procedure TFORM1.CHECKBOX10CLICK (Sender: TOBJECT);

Begin

Timer2.enabled: = CheckBox10.Checked;

END;

Compile the running program.

转载请注明原文地址:https://www.9cbs.com/read-20102.html

New Post(0)