Borland Delphi2.0 / 3.0 design with its powerful function and convenient and easy programming
And love the majority of programmers. But when using it to write industrial control procedures, it is necessary to
The external device connected to the computer performs operation, ie the I / O address is read and write directly.
At this time, the software seems to be a little in the United States.
In response to this problem, the author uses Delphi 2.0 / 3.0 to write compilation.
A module port95. PAS, convenient implementation directly to the I / O address read and write operation, generation
The code is simple and the speed is faster.
As long as you use the port95.pas to the project file, plus port in Users
95, you can operate directly to the I / O port directly.
The specific implementation method and the source code of Port95.PAS are as follows:
Unit port95;
Interface
Function PortReadbyte (AddR: Word): Byte;
Function PortReadword (AddR: Word): Word;
Function PortReadwordls (AddR: Word): Word;
Procedure PortwritebyTe (AddR: Word; Value: Byte);
Procedure PortwriteWord (AddR: Word; Value: Word);
Procedure PortwriteWordls (AddR: Word; Value: Word);
IMPLEMentation
{*
* Port read byte function
* Parameter: Port Address
* Return: Byte Value from Given Port
*}
Function PortReadbyte (AddR: Word): Byte; Assembler; Regi
Ster;
ASM
MOV DX, AX
IN Al, DX
END;
{*
* High Speed Port Read Word Function
* Parameter: Port Address
* Return: Word Value from Given Port
* Comment: May Problex With Some Cards and Computers That
Can't to Access Whole Word, USUALY IT WORKS.
*}
Function PortReadword (AddR: Word): Word; Assembler; Regi
Ster;
ASM
MOV DX, AX
IN AX, DX
END;
{*
* Low Speed Port Read Word Function
* Parameter: Port Address
* Return: Word Value from Given Port
* Comment: Work In Cases, ONLY TO Adjust Delay IF NEED
*}
Function PortReadwordls (AddR: Word): Word; Assembler; Re
Gister;
Const
DELAY = 150;
// depending of cpu speed and cards speed
ASM
MOV DX, AX
IN Al, DX
// read LSB Port
MOV ECX, DELAY
@1:
Loop @ 1 // delay Between Two Reads
XCHG AH, Al
INC DX
// Port 1
IN AL, DX // Read MSB Port
Xchg Ah, Al // Restore Bytes ORDER
END;
{* Port write byte function *}
Procedure PortwritebyTe (AddR: Word; Value: Byte); Assemble
r; register;
ASM
XCHG AX, DXout DX, Al
END;
{*
* High Speed Port Write Word Procedure
* Comment: May Problex With Some Cards and Computers That
Can't to Access Whole Word, USUALY IT WORKS.
*}
Procedure PortwriteWord (AddR: Word; Value: Word); Assemble
r; register;
ASM
XCHG AX, DX
OUT DX, AX
END;
{*
* Low Speed Port Write Word Procedure
*}
Procedure PortwriteWordls (AddR: Word; Value: Word); assemb
Ler; register;
Const
DELAY = 150;
// depending of cpu speed and cards speed
ASM
XCHG AX, DX
Out dx, al
MOV ECX, DELAY
@1:
LOOP @ 1
XCHG AH, Al
INC DX
Out dx, al
END;
End. // Unit end
The above port95.PAS is suitable for Delphi 2.0 / 3.0, Windows 95 operating system
.