失
失去轮回
Unregistered / Unconfirmed
GUEST, unregistred user!
我在使用TCOMM时总是无法得到返回数据,请大家帮忙看看什么地方出问题了
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, SPComm,FmtBcd;
type
TFrmMain = class(TForm)
Comm1: TComm;
Button1: TButton;
Memo1: TMemo;
Button2: TButton;
procedure senddate(length:byte);
procedure Comm1ReceiveData(Sender: TObject; Buffer: Pointer;
BufferLength: Word);
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
FrmMain: TFrmMain;
rbuf,sbuf:array[1..100] of byte ;
viewstring:string;
giSendLen,giReceLen:integer;
implementation
{$R *.dfm}
function BcdToInt(iBCD:integer):integer;
var
iRet, i, iTimes:integer;
begin
iRet:=0;
iTimes:=1;
while iBCD>0 do begin
i:=iBCD mod 10;
iBCD:=iBCD div 10;
iRet:=iRet+i*iTimes;
iTimes:=iTimes*16;
end;
result:=iRet;
end;
procedure TFrmMain.senddate(length:byte);
var
i:integer;
begin
viewstring:='';
for i:=1 to length do
begin
viewstring:=viewstring+inttohex(sbuf,2)+' ';
end;
comm1.WriteCommData(@sbuf,length);
viewstring:='发送: '+viewstring;
memo1.Lines.Add(viewstring);
end;
procedure TFrmMain.Comm1ReceiveData(Sender: TObject; Buffer: Pointer;
BufferLength: Word);
var
i,iloop,ipos:integer;
begin
viewstring:='';
move(buffer^,pchar(@rbuf)^,bufferlength);
if BufferLength<(giSendLen+10) then
exit;
giReceLen:=BufferLength-giSendLen;
for i:=1 to giReceLen do
rbuf:=rbuf[i+giSendLen];
for i:=1 to giReceLen do
viewstring:=viewstring+inttohex(rbuf,2)+' ';
ipos:=pos('68',viewstring);
viewstring:='接收'+viewstring;
memo1.Lines.add(viewstring);
end;
procedure TFrmMain.Button1Click(Sender: TObject);
begin
Comm1.StopComm;
Comm1.CommName:='COM1';
Comm1.BaudRate:=1200;
Comm1.StartComm;
end;
procedure TFrmMain.Button2Click(Sender: TObject);
begin
sbuf[1]:=$fe; {帧头}
sbuf[2]:=$fe; {帧头}
sbuf[3]:=$68; {}
sbuf[4]:=$99;
sbuf[5]:=$99;
sbuf[6]:=$99;
sbuf[7]:=$99;
sbuf[8]:=$99;
sbuf[9]:=$99;
sbuf[10]:=$68;
sbuf[11]:=$01;
sbuf[12]:=$02;
sbuf[13]:=$65;
sbuf[14]:=$F3;
sbuf[15]:=$C1;
//2004.06.16 password changed by lhj
sbuf[16]:=$16;
giSendLen:=16;
senddate(16);
// else begin
// s:=uc_getmeterid;
// Memo1.Lines.Add('');
// Memo1.Lines.Add(s);
// Memo1.Lines.Add('');
end;
end.
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, SPComm,FmtBcd;
type
TFrmMain = class(TForm)
Comm1: TComm;
Button1: TButton;
Memo1: TMemo;
Button2: TButton;
procedure senddate(length:byte);
procedure Comm1ReceiveData(Sender: TObject; Buffer: Pointer;
BufferLength: Word);
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
FrmMain: TFrmMain;
rbuf,sbuf:array[1..100] of byte ;
viewstring:string;
giSendLen,giReceLen:integer;
implementation
{$R *.dfm}
function BcdToInt(iBCD:integer):integer;
var
iRet, i, iTimes:integer;
begin
iRet:=0;
iTimes:=1;
while iBCD>0 do begin
i:=iBCD mod 10;
iBCD:=iBCD div 10;
iRet:=iRet+i*iTimes;
iTimes:=iTimes*16;
end;
result:=iRet;
end;
procedure TFrmMain.senddate(length:byte);
var
i:integer;
begin
viewstring:='';
for i:=1 to length do
begin
viewstring:=viewstring+inttohex(sbuf,2)+' ';
end;
comm1.WriteCommData(@sbuf,length);
viewstring:='发送: '+viewstring;
memo1.Lines.Add(viewstring);
end;
procedure TFrmMain.Comm1ReceiveData(Sender: TObject; Buffer: Pointer;
BufferLength: Word);
var
i,iloop,ipos:integer;
begin
viewstring:='';
move(buffer^,pchar(@rbuf)^,bufferlength);
if BufferLength<(giSendLen+10) then
exit;
giReceLen:=BufferLength-giSendLen;
for i:=1 to giReceLen do
rbuf:=rbuf[i+giSendLen];
for i:=1 to giReceLen do
viewstring:=viewstring+inttohex(rbuf,2)+' ';
ipos:=pos('68',viewstring);
viewstring:='接收'+viewstring;
memo1.Lines.add(viewstring);
end;
procedure TFrmMain.Button1Click(Sender: TObject);
begin
Comm1.StopComm;
Comm1.CommName:='COM1';
Comm1.BaudRate:=1200;
Comm1.StartComm;
end;
procedure TFrmMain.Button2Click(Sender: TObject);
begin
sbuf[1]:=$fe; {帧头}
sbuf[2]:=$fe; {帧头}
sbuf[3]:=$68; {}
sbuf[4]:=$99;
sbuf[5]:=$99;
sbuf[6]:=$99;
sbuf[7]:=$99;
sbuf[8]:=$99;
sbuf[9]:=$99;
sbuf[10]:=$68;
sbuf[11]:=$01;
sbuf[12]:=$02;
sbuf[13]:=$65;
sbuf[14]:=$F3;
sbuf[15]:=$C1;
//2004.06.16 password changed by lhj
sbuf[16]:=$16;
giSendLen:=16;
senddate(16);
// else begin
// s:=uc_getmeterid;
// Memo1.Lines.Add('');
// Memo1.Lines.Add(s);
// Memo1.Lines.Add('');
end;
end.