请高手翻译这段C程序为delphi可以用spcomm等控件

whitehare 2003-06-29 10:24:50
#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&0x80) val+=ini;
c<<=1; ini*=2;
}
// val=0xff-val;
return val;
}

void send_data(unsigned char ch)
{
while((inportb(LineStatus)&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)&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)&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&1) res++;
ch>>=1;
}
res&=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&=0xf0;

ch=(addr&0xf0)>>4; check=check+ones(ch);
ch=addr&0x0f; check=check+ones(ch)*2;
ch=(order&0xf0)>>4; check=check+ones(ch)*4;
ch=order&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)&1)
inportb(SBUF);

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

while(!(inportb(LineStatus)&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[i][j]);
ch=rev(TempChar);
check=check+ch;//msg_dz[i][j]);
printf("check= %d ,checkback= %d \n",&check,&checkback);
// send_data(0x55);//msg_dz[i][j]);
// check+=rev(0x55);//msg_dz[i][j]);
}
while(!(inportb(LineStatus)&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)&1));
checkback=inportb(SBUF);

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

}

pc同单片机用rs485方式来连接
...全文
66 5 打赏 收藏 举报
写回复
5 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
jinmingdi 2003-07-29
请QQ:16458743联系
  • 打赏
  • 举报
回复
whitehare 2003-06-30
说什么重赏之下必有勇夫
  • 打赏
  • 举报
回复
whitehare 2003-06-30
delphi7/win2000
  • 打赏
  • 举报
回复
Leo 2003-06-30
to whitehare(小野免) :
你用什么操作系统,DELPHI是什么版本?
  • 打赏
  • 举报
回复
whitehare 2003-06-29
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[i],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;

//这是我正在调试代码,请高手给于指教
  • 打赏
  • 举报
回复
相关推荐
发帖
驱动开发/核心开发
加入

2.1w+

社区成员

硬件/嵌入开发 驱动开发/核心开发
社区管理员
  • 驱动开发/核心开发社区
申请成为版主
帖子事件
创建了帖子
2003-06-29 10:24
社区公告
暂无公告