在DELPHI2.0/3.0中直接操作端口_Delphi

  • 内容
  • 评论
  • 相关

Borland Delphi2.0/3.0以其强大的功能及方便快捷的程序设计
而为广大程序员所喜爱。但是在用它来编写工业控制程序时,需要对
计算机所连接的外部设备进行操作,即直接对I/O地址进行读写操作,
这时该软件就显得有些美中不足了。
针对这一问题,笔者使用Delphi 2.0/3.0以内嵌汇编的方式编写
了一个模块PORT95. PAS,可方便地实现直接对I/O地址的读写操作,代
码简捷且执行速度较快。
使用时只要将PORT95.PAS加到工程文件中,并在users中加上Port
95,就可以在应用程序中直接对I/O端口进行操作。
具体的实现方法及PORT95.PAS的源代码如下:

http://www.gaodaima.com/65539.html在DELPHI2.0/3.0中直接操作端口_Delphi

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 problem 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,DX
OUT DX,AL
end;
{*
* HIGH SPEED Port Write word procedure
* Comment:may problem 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. //单元结束
上述PORT95.PAS适用于Delphi 2.0/3.0、Windows 95 操作系统
。 

欢迎大家阅读《在DELPHI2.0/3.0中直接操作端口_Delphi》,跪求各位点评,若觉得好的话请收藏本文,by 搞代码

原创文章,转载请注明: 转载自搞代码

本文链接地址: 在DELPHI2.0/3.0中直接操作端口_Delphi

微信支付二维码

微信 赏一包辣条吧~

支付宝支付二维码

支付宝 赏一听可乐吧~

评论

0条评论

发表评论

电子邮件地址不会被公开。 必填项已用*标注