Directly operate the port in delphi2.0/3.0

Source: Internet
Author: User

Borland delphi2.0/3.0 with its powerful functions and convenient and fast Programming
But for the majority of programmers love. However, when you use it to write industrial control programs, you need
The external device connected to the computer performs read/write operations on the I/O address directly,
At this time, the software seems to be somewhat inadequate.
To solve this problem, the author uses Delphi 2.0/3.0 or less embedded assembly method to write
A module port95. pas can be used to conveniently read and write the I/O address.
The code is simple and the execution speed is fast.
You only need to add port95.pas to the project file and add Port in users
95, you can directly operate on the I/O port in the application.
The specific implementation method and 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; 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; Referer; 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; author er; 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. // The unit ends.
The port95.pas mentioned above applies to Delphi 2.0/3.0 and Windows 95 operating systems.
.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.