Borland Delphi2.0/3.0 with its powerful functions and convenient and fast programming
And loved by the majority of programmers. But when using it to write industrial control programs, you need to
The external device connected to the computer operates, that is, directly reads and writes the I/O address.
At this time, the software seems to have some flaws.
In response to this problem, the author uses Delphi 2.0/3.0 to write in the form of inline assembly
A module PORT95.PAS is installed, which can easily realize direct reading and writing operations on I/O addresses.
The code is simple and fast in execution.
When using it, just add PORT95.PAS to the project file and add Port to users
95, you can directly operate the I/O port in the application.
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
MOVDX,AX
IN AL,DX
end;
{*
* HIGH SPEED Port Read Word function
* Parameter: port address
* Return: word value from given port
* Comment:may problem with some cards and computers that
can't access whole word, usually it works.
*}
function PortReadWord(Addr:Word) : Word; assembler; regi
ster;
asm
MOVDX,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
MOVDX,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
XCHGAX,DX
OUT DX,AL
end;
{*
* HIGH SPEED Port Write word procedure
* Comment:may problem with some cards and computers that
can't access whole word, usually it works.
*}
procedure PortWriteWord(Addr:word; Value:word); assemble
r; register;
asm
XCHGAX,DX
OUTDX,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
XCHGAX,DX
OUT DX,AL
MOV ECX,Delay
@1:
LOOP@1
XCHG AH,AL
INC DX
OUT DX,AL
end;
end. //End of unit
The above PORT95.PAS is suitable for Delphi 2.0/3.0 and Windows 95 operating systems
.