请高手翻译这段C程序为delphi可以用spcomm等控件(重赏之下必有勇夫)(200分)

  • 主题发起人 主题发起人 祝海钢
  • 开始时间 开始时间

祝海钢

Unregistered / Unconfirmed
GUEST, unregistred user!
#define SBUF 0x3f8

#define baudL 0x3f8
#define baudH 0x3f9

#define LineCtrl 0x3fb
#define ModemCtrl 0x3fc
#define LineStatus 0x3fd


#define LINE 6
#define WIDTH 48


#include <dos.h>
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
#include <process.h>

unsigned char rev(unsigned char c) //倒置
{
unsigned char i,val=0,ini=1;

for (i=0; i<8; i++){
if (c&amp;0x80) val+=ini;
c<<=1; ini*=2;
}
// val=0xff-val;
return val;
}

void send_data(unsigned char ch)
{
while((inportb(LineStatus)&amp;0x60)!=0x60);
outportb(LineCtrl,0x3b); //8-data,1-stop,9th bit=0
outportb(SBUF,rev(ch));
}

void send_addr(unsigned char ch)
{
while((inportb(LineStatus)&amp;0x60)!=0x60);
outportb(LineCtrl,0x2b); //8-data,1-stop,9th bit=1(SEND address)
outportb(SBUF,ch);
}

void send_order_para(unsigned char ch)
{
while((inportb(LineStatus)&amp;0x60)!=0x60);
outportb(LineCtrl,0x3b); //8-data,1-stop,9th bit=0
outportb(SBUF,ch);
}

unsigned char ones(unsigned char ch)
{
unsigned char i,res=0;

for (i=0; i<4; i++)
{
if (ch&amp;1) res++;
ch>>=1;
}
res&amp;=1;

return res;
}


unsigned char verify(unsigned char addr,unsigned char order) //生成一个addr 和order的校验码
{
unsigned char check,ch;

check=addr^order; check=check^(check<<4);
check&amp;=0xf0;

ch=(addr&amp;0xf0)>>4; check=check+ones(ch);
ch=addr&amp;0x0f; check=check+ones(ch)*2;
ch=(order&amp;0xf0)>>4; check=check+ones(ch)*4;
ch=order&amp;0x0f; check=check+ones(ch)*8;

return check;
}



void main()
{
unsigned char address=0x01,order=0x01, check,checkback,ch;
int i,j;
unsigned char TempChar;
FILE *fp;

outportb(LineCtrl,0x80);
outportb(baudL,0x02);
outportb(baudH,0x00); //baud-rate=57.6k
/*
波特率改成57.6k
*/

outportb(ModemCtrl,0x00); //dis-connect SOUT and SIN in computer

outportb(LineCtrl,0x2b); //8-data,1-stop,9th bit=1(SEND address)

if (inportb(LineStatus)&amp;1)
inportb(SBUF);

order=0x01; //命令0x01为发送整屏数据
send_addr(address);
send_order_para(order);
check=verify(address,order);
send_order_para(check);

while(!(inportb(LineStatus)&amp;1));
//等待单片机应答。这里需要处理一下、不能死等,否则在单片机未上电等情况下会死机
checkback=inportb(SBUF);

if (checkback!=check)
exit(1); //NO ACK or Wrong ACK
//出口引向何处也需要重新处理

check=0x00;
for (i=0; i<48; i++)
for (j=0; j<96; j++)
{
TempChar=0x55;
send_data(TempChar);//msg_dz[j]);
ch=rev(TempChar);
check=check+ch;//msg_dz[j]);
printf("check= %d ,checkback= %d /n",&amp;check,&amp;checkback);
// send_data(0x55);//msg_dz[j]);
// check+=rev(0x55);//msg_dz[j]);
}
while(!(inportb(LineStatus)&amp;1));
checkback=inportb(SBUF);

if (checkback!=check)
exit(1); //NO ACK or Wrong ACK

order=0x02; //0x02为显示命令
send_addr(address);
send_order_para(order);
check=verify(address,order);
send_order_para(check);

while(!(inportb(LineStatus)&amp;1));
checkback=inportb(SBUF);

if (checkback!=check) //check
exit(1); //NO ACK or Wrong ACK

}

pc同单片机用rs485方式来连接
 
Viewstring:string;
// i:integer;
rbuf,sbuf:array[1..6] of byte;

implementation

{$R *.dfm}

//===============================================================================
function TForm1.ones(ch: Byte): Byte;
var
i,res:integer;
begin
// res byte=0;
for i:=0 to 3 do
begin
if (ch and 1)<>$00 then
res:=res+1;
ch:=ch shr 1;
end;
res :=res and 1;
result:=res;
end;

function TForm1.rev(c: Byte): Byte;
var
i : integer;
val: Byte;
ini: Byte;
begin
val:=$00;
ini:=$01;
for i:=0 to 7 do
begin
if (c and $80)<>$00 then
val:=ini+val;
c:=c shl 1;
ini:=ini*2;
end;
// val:=$ff-val;
Result:=val;
end;

procedure TForm1.send_addr(ch: Byte);
begin
Comm1.Parity:=Mark;
Comm1.ParityCheck:=True;
Comm1.RtsControl:=RtsEnable;
sleep(1);
Comm1.WriteCommData(@ch,2);
// Comm1.RtsControl:=RtsDisable;
sleep(1);
Comm1.Parity:=Space;
// Comm1.ParityCheck:=False;
Comm1.RtsControl:=RtsDisable;
end;

procedure TForm1.send_data(ch: Byte);
begin
Comm1.RtsControl:=RtsEnable;
sleep(1);
Comm1.WriteCommData(@ch,1);
sleep(1);
Comm1.RtsControl:=RtsDisable;
// sleep(100);
end;

procedure TForm1.send_order(ch: Byte);
begin
Comm1.RtsControl:=RtsEnable;
sleep(1);
Comm1.WriteCommData(@ch,1);
sleep(1);
Comm1.RtsControl:=RtsDisable;
end;

function TForm1.verify(addr, order: Byte): Byte;
var
check,ch:Byte;
begin
check:=addr xor order;
check:=check xor(check shl 4);
check:=check and $f0;

ch:=(addr and $f0) shr 4; check:=check+ones(ch);
ch:=addr and $0f; check:=check+ones(ch)*2;
ch:=(order and $f0) shr 4; check:=check+ones(ch)*4;
ch:=order and $0f; check:=check+ones(ch)*8;

result:=check;
end;

procedure TForm1.Comm1ReceiveData(Sender: TObject; Buffer: Pointer;
BufferLength: Word);
var
i:integer;
begin
viewstring:='';
move(buffer^,pchar(@rbuf)^,bufferlength);
for i:=1 to bufferlength do
viewstring:=viewstring+ inttohex(rbuf,2)+' ' ;
end;

procedure TForm1.Button6Click(Sender: TObject);
var
address: Byte;
order: Byte;
check,checkback,ch:Byte;
i,j:integer;
TempChar:Byte;
begin
address:=$01;
order:=$01; //命令0x01为发送整屏数据
Comm1.StartComm;
send_addr(address);
send_order(order);
check:=verify(address,order);
send_order(check);
while(viewString='') do
Application.ProcessMessages;
showmessage(viewstring);
viewstring:='';
check:=$00;
for i:=0 to 47 do
for j:=0 to 95 do
begin
TempChar:=$05;
ch:=rev(TempChar);
send_data(ch);
check:=check+ch;
end;
while(viewString='') do
Application.ProcessMessages;
showmessage(viewstring+ '---'+inttohex(check,2));//就是这里读不到返回值,出现内存错误
viewstring:='';
order:=$02; //0x02为显示命令
send_addr(address);
send_order(order);
check:=verify(address,order);
send_order(check);
while(viewString='') do
Application.ProcessMessages;
showmessage(viewstring);
viewstring:='';
Comm1.StopComm;
end;

//这是我正在调试代码,请高手给于指教
 
[?][?][green]说什么重赏之下必有勇夫[/green][?][:(]
 
哇重赏呢
 
to 祝海钢
showmessage(viewstring+ '---'+inttohex(check,2));

这个为什么之值阿,
 
跟据要求直接写不就得了,还非要翻译什么C的程序干吗?
我觉得直接写比你翻译人家的程序更麻烦,你说不是吗?[:D][^]
 
后退
顶部