谁帮我把这C程序翻译成delphi呀,题易分大啊咳!(真的,我改了要求呀) (50分)

  • 主题发起人 主题发起人 懒少爷
  • 开始时间 开始时间

懒少爷

Unregistered / Unconfirmed
GUEST, unregistred user!
#include <stdio.h>
#include <bios.h>
#include <dos.h>

#define uint unsigned int
#define uchar unsigned char

void initial(void);

uint menu(uint select, uint qty, char mu[][21]);
void cursor_on(void);
void cursor_off(void);
uchar receive(void);
void send(uchar da);

void reset(void);
void zero(void);
void read_guage(void);
void read_eep(void);
void write_eep(void);
void forward(void);
void backward(void);
void stop_motor(void);
void test(void);

void main (void) {
uint key, i, j, select;

static char mu[10][21]={{"Reset guage "},
{"Zero guage "},
{"Read guage "},
{"Read EEPROM "},
{"Write EEPROM "},
{"Moving forward "},
{"Moving backward "},
{"Stop motor "},
{"A test proccess "},
{"Exit "}};

initial();
select=0;

for(;;) {
select=menu(select, 10, mu);
clrscr();
if(select==0) reset();
if(select==1) zero();
if(select==2) read_guage();
if(select==3) read_eep();
if(select==4) write_eep();
if(select==5) forward();
if(select==6) backward();
if(select==7) stop_motor();
if(select==8) test();
if(select==9) break;
}
}


[red]void initial(void) {
outportb(0x3fb, 0x80); /* prepare set baud */
outportb(0x3f8, 0x0c); /* set buad L */
outportb(0x3f9, 0x00); /* set buad H */
outportb(0x3fb, 0x23); /* set line register */
outportb(0x3fc, 0x00); /* set interrupt enable register */
outportb(0x3f9, 0x00); /* set modem register */

inportb(0x3f8);
inportb(0x3fd);
}
[/red]


void cursor_on(void) {
union REGS cursor_on;
union REGS temp;

cursor_on.h.ch=0x13; cursor_on.h.cl=0x14; cursor_on.h.ah=0x01;
int86(0x10, &amp;cursor_on, &amp;temp);
}

void cursor_off(void) {
union REGS cursor_off;
union REGS temp;

cursor_off.h.ch=0x20; cursor_off.h.ah=0x01;
int86(0x10, &amp;cursor_off, &amp;temp);
}


uint menu(uint select, uint qty, char mu[][21]) {
uint i, update, key;

update=1;
cursor_off();
clrscr();
for(;;) {
if(update==1) {
for(i=0; i<qty; i++) {
gotoxy(30, i+6); printf("%s", &amp;mu[0]);
}
for(i=0; i<qty; i++) {
gotoxy(26, i+6); printf(" ");
}
gotoxy(26, select+6); printf("=>");
update=0;
}

if(bioskey(1)==0) continue;
else key=bioskey(0);

if(key==Up) { /* up key */
if(select>0) {select--; update=1;}
}
if(key==Down) { /* down key */
if(select<qty-1) {select++; update=1;}
}
if((key&amp;0x00ff)==Etr) break; /* enter key */
}
cursor_on();
return(select);
}


[red]uchar receive(void) {
uchar da;
while((inportb(0x3fd)&amp;0x01)==0); /* waitting receive data */
da=inportb(0x3f8);
return(da);
}[/red]


[red]void send(uchar da) {
outportb(0x3f8, da); /* send address */
while((inportb(0x3fd)&amp;0x40)==0); /* waiting send out */
}[/red]
void reset(void) {
send(0);
}

void zero(void) {
send(1);
}

void read_guage(void) {
uint f1, f2, force;
f1=0;
f2=0;
send(2);
f1=receive();
f2=receive();
force=f1+f2*256;
printf("The force guage displayed is: %d", force);
printf("/nPress anykey to return");
getch();
}

void read_eep(void) {
uint addr, da;
da=0;
printf("Please input EEPROM address: ");
scanf("%d", &amp;addr);

send(3);
send((uchar)addr);
da=receive();

printf("/nRead data is: %d", da);
printf("/nPress anykey to return");
getch();
}

void write_eep(void) {
uint addr, da;
printf("Please input EEPROM address: ");
scanf("%d", &amp;addr);
printf("/nPlase input data: ");
scanf("%d", &amp;da);
send(4);

send((uchar)addr);
send((uchar)da);

printf("The data has been writted, press anykey to return.");
getch();
}

void forward(void) {
send(5);
}

void backward(void) {
send(6);
}

void stop_motor(void) {
send(7);
}

void test(void) {
uint i, force, force_max;

send(8);

force_max=0;

for(i=0; ;i++) {
force=receive();
force+=receive()*256; /* force=force+receive()*256 */
if((force&amp;0xff00)==0xff00) break;
if(force>force_max) force_max=force;
printf("/n i= %d force= %d", i, force);
}

printf("/n Maxim force is: %d", force_max);
printf("/n Press anykey to return.");
getch();
}
 
这还叫芬达?题易?
 
别急别急!不要行开!
我只要你把打开COM口,初始化,写入数据,读出数据那几行翻过来就可以了.
 
一、使用嵌入式汇编,我用的是对LPT1的操作,自己改串口

function GetLpt1Data:Byte
var
data : Byte;
begin
asm
mov dx,0379H;
in al,dx;
mov data,al;
end;
end;

procedure SetLpt1Data(data:Byte);
asm
mov al,data;
mov dx,0378H;
out dx,al;
end;
end;


二、使用API函数,

var
buf : String;
hCom : Integer;
1、打开
hCom:=CreateFile(PChar(PortBox.Text),GENERIC_READ or

GENERIC_WRITE,0,nil,OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,0);

2、可按实际需要设置
Done:=CommSetup(hCom,BaudBox.ItemIndex);
Done:=CommTimeout(hCom,StrToInt(Edit1.Text)*1000);

3、读写端口
Done:=WriteFile(hCom,PChar(buf)^,1,RetNum,nil);

Done:=ReadFile(hCom,PChar(buf)^,1,RetNum,nil);
4、关闭串口
CloseHandle(hCom);
 
例子我看了好多,不懂啊.这样吧,请用WriteFile和ReadFile改写下面的scanf()和send()语句,
请不要烦我啊,还可以加100大元啊(我最多只有这么多了).

void write_eep(void) {
uint addr, da;
printf("Please input EEPROM address: ");
scanf("%d", &amp;addr);
printf("/nPlase input data: ");
scanf("%d", &amp;da);
send(4);

send((uchar)addr);
send((uchar)da);

printf("The data has been writted, press anykey to return.");
getch();
 
scanf只是用来读取来自键盘的输入,在DELPHI中可以用EDIT控件来接收
用户的输入,或者不想用控件,也可以用INPUTBOX来接收输入,
至于SEND函数,下面有两个和TC名字一样的读写端口的函数,把它加入
你的文件,直接象TC一样用就可以了。

可改写为如下:
function inportb(address:Word):Byte;
begin
Result:=0;
asm
Mov dx,address
In al, dx
Mov Result,al
end;
end;

procedure outportb(address:Word;data:Byte);
begin
asm
Mov dx,address
Mov al,data
Out dx,al
end;
end;

procedure send(word da)
begin
outportb(0x3f8, da); /* send address */
while((inportb(0x3fd)&amp;0x40)==0); /* waiting send out */
end;




 
大谢!大谢!
不管我做不做出来,有50分给你!其他跟来还有分,继续啊!
 
接受答案了.
 
端口读写、API HOOK、屏幕取词的完整解决方案见我的《delphi深入windows核心编程》一书,
支持win98/2000/xp,
我的主页http://wenjinshan.yeah.net
 
后退
顶部