C
caoshangfei99
Unregistered / Unconfirmed
GUEST, unregistred user!
200分求用spcomm接收单片机发送的数据的代码,思路也行。顺便帮我看看我的代码,为什么不能接收。。只能发送,却不能接收。。为什么??
procedure TForm1.FormShow(Sender: TObject);
begin
comm1.StartComm;
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
close;
end;
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
comm1.StopComm;
end;
procedure senddata;
var
i:integer;
commflg:boolean;
begin
viewstring:='';
commflg:=true;
for i:=1 to 6 do
begin
if not form1.comm1.writecommdata(@sbuf,1) then
begin
commflg:=false;
break;
end;
//发送时字节间的延时
sleep(2);
viewstring:=viewstring+inttohex(sbuf,2)+'';
end;
viewstring:='发送'+ viewstring;
form1.memo1.lines.add(viewstring);
if not commflg then messagedlg('发送失败 !' ,mterror,[mbyes],0);
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
sbuf[1]:=byte($f0); //帧头
sbuf[2]:=byte($01); //命令号
sbuf[3]:=byte($ff);
sbuf[4]:=byte($ff);
sbuf[5]:=byte($01);
sbuf[6]:=byte($f0); //帧尾
senddata;//调用发送函数
end;
procedure TForm1.Comm1ReceiveData(Sender: TObject; Buffer: Pointer;
BufferLength: Word);
var
i:integer;
begin
viewstring:='' ;
move(buffer^,pchar(@rbuf)^,bufferlength);
showmessage(inttostr(bufferlength));
for i:=1 to bufferlength do
begin
viewstring:=viewstring+inttohex(rbuf,2)+'';
end;
viewstring:='接收'+viewstring;
memo1.lines.add(viewstring);
memo1.lines.add('');
end;
procedure TForm1.Comm1ReceiveError(Sender: TObject; EventMask: Cardinal);
begin
memo1.Lines.add('接受数据错误');
end;
procedure TForm1.Timer1Timer(Sender: TObject);
begin
button1.Click;
end;
end.
为什么我这段代码只执行发送,却不执行接收呢??
procedure TForm1.FormShow(Sender: TObject);
begin
comm1.StartComm;
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
close;
end;
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
comm1.StopComm;
end;
procedure senddata;
var
i:integer;
commflg:boolean;
begin
viewstring:='';
commflg:=true;
for i:=1 to 6 do
begin
if not form1.comm1.writecommdata(@sbuf,1) then
begin
commflg:=false;
break;
end;
//发送时字节间的延时
sleep(2);
viewstring:=viewstring+inttohex(sbuf,2)+'';
end;
viewstring:='发送'+ viewstring;
form1.memo1.lines.add(viewstring);
if not commflg then messagedlg('发送失败 !' ,mterror,[mbyes],0);
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
sbuf[1]:=byte($f0); //帧头
sbuf[2]:=byte($01); //命令号
sbuf[3]:=byte($ff);
sbuf[4]:=byte($ff);
sbuf[5]:=byte($01);
sbuf[6]:=byte($f0); //帧尾
senddata;//调用发送函数
end;
procedure TForm1.Comm1ReceiveData(Sender: TObject; Buffer: Pointer;
BufferLength: Word);
var
i:integer;
begin
viewstring:='' ;
move(buffer^,pchar(@rbuf)^,bufferlength);
showmessage(inttostr(bufferlength));
for i:=1 to bufferlength do
begin
viewstring:=viewstring+inttohex(rbuf,2)+'';
end;
viewstring:='接收'+viewstring;
memo1.lines.add(viewstring);
memo1.lines.add('');
end;
procedure TForm1.Comm1ReceiveError(Sender: TObject; EventMask: Cardinal);
begin
memo1.Lines.add('接受数据错误');
end;
procedure TForm1.Timer1Timer(Sender: TObject);
begin
button1.Click;
end;
end.
为什么我这段代码只执行发送,却不执行接收呢??