程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> Delphi >> 在DELPHI2.0/3.0中直接操作端口

在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的源代碼如下:
  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. //單元結束
  
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved