这段 A/D 程序该怎样写 ?(200分)

蓝天

Unregistered / Unconfirmed
GUEST, unregistred user!
下面这段程序是我用 TC 编写的,怎样把它改写为 Delphi 程序?
关键是端口访问和位操作不知道该怎么写。
int AD(int channel,int ida)
{
int BASE=0x280;
outportb(BASE,channel);
while(!(inportb(BASE+5)&0x80));
ida=inportb(BASE+1)|((inportb(BASE+2)&amp;0x0f)<<8);
return(2047-ida);
}
 
端口读写和移位要用汇编来写.
 
你用的是什么A/D板?delphi无法直接访问端口地址,一般现在的A/D版都有
提供for win的dll和activeX的dll,你可以照例程写。
 
我们现在用的A/D板是用汇编来访问的,不过我没关心过是什么型号
有A/D板地址应该能访问.
 
crane is right
 
procedure AD(channel: integer): integer;
const Base = $280;
var Data: byte;
temp: word;
begin
Data := Channel;
asm
mov al, Data;
mov dx, Base;
out al, DX
add dx, 5
@@1: in al, dx
and al, $80
jnb @@1
sub dx, 4
in al, dx
mov ah, al;
inc dx
in al, dx
xchg al, ah
mov temp, ax
end;
result := 2047-temp;
end;
 
>>iie
function not procedure
最好把channel直接声明为byte,省下data
out dx,al
 
procedure outportb(Address:Word;
Data:Byte);assembler;
asm
MOV DX,Address
MOV AL,Data
OUT DX,AL
end;
function inportb(Address:Word):Byte;assembler;
asm
MOV DX, Address
IN AL,DX
end;

function AD(channel,ida:Integer):Integer;
const BASE=0x280;
begin
outportb(BASE,channel);
while((inportb(BASE+5)&amp;0x80)<>0)do
;
ida:=inportb(BASE+1)|(Integer(inportb(BASE+2) and 0x0f) shl8);
Result:=2047-ida;
end;

 
ida:=inportb(BASE+1)or(Integer(inportb(BASE+2) and 0x0f) shl8);
 
>>Hexi 十分感谢,想和您讨论一下

function AD(channel,ida:Integer):Integer;
...
while((inportb(BASE+5)&amp;0x80)<>0)do
;
//最高位为1表示有数可采,为0则等待,我认为应该这样写
while((inportb(BASE+5)&amp;0x80)=0)do
;
...
Delphi 中16进制数应该写为 $0f

 
while ((inportb(BASE+5) and $80)=0)do
;
 
多人接受答案了。
 
procedure outportb(Address:Word;
Data:Byte);assembler;
asm
MOV DX,Address
MOV AL,Data
OUT DX,AL
end;

上面的函数好像不能用,请看:
http://www.gislab.ecnu.edu.cn/delphibbs/DispQ.asp?LID=249806
 
如果是DELPHI3。0以上使用
SmallPort控件。
使用汇编无效,对打印口编程还可以。
 
端口读写、API HOOK、屏幕取词的完整解决方案见我的《delphi深入windows核心编程》一书,
支持win98/2000/xp,
我的主页http://wenjinshan.yeah.net
 

Similar threads

I
回复
0
查看
778
import
I
S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
I
回复
0
查看
657
import
I
I
回复
0
查看
438
import
I
顶部