测试一下spcomm控件的 使用,通不过,大虾请进(20分)

  • 主题发起人 主题发起人 kevin3000
  • 开始时间 开始时间
K

kevin3000

Unregistered / Unconfirmed
GUEST, unregistred user!
测试一下spcomm控件的 使用,出错提示是“发送失败”;
或者说是“连接串口出错”。
我对通讯不了解。请各位大虾关照。
 
没有装对吧
 
你指SPCOMM吗?安装的时候没有任何异常提示。
 
就是下面这段代码。
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, SPComm;
type
TFCOMM = class(TForm)
Comm1: TComm;
Button1: TButton;
Button2: TButton;
Memo1: TMemo;
Button3: TButton;
procedure Button3Click(Sender: TObject);
procedure FormShow(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure Button1Click(Sender: TObject);
procedure Comm1ReceiveData(Sender: TObject; Buffer: Pointer;
BufferLength: Word);
private
{ Private declarations }
public
{ Public declarations }
end;

var
FCOMM: TFCOMM;
viewstring:string;
i:integer;
rbuf,sbuf:array[1..16] of byte;

implementation

{$R *.dfm}

procedure TFCOMM.Button3Click(Sender: TObject);
begin
application.Terminate;
end;

//打开串口
procedure TFCOMM.FormShow(Sender: TObject);
begin
comm1.StartComm;
end;

//关闭串口
procedure TFCOMM.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 fcomm.comm1.writecommdata(@sbuf,1) then
begin
commflg:=false;
break;
end;
//发送时字节间的延时
sleep(2);
viewstring:=viewstring+inttohex(sbuf,2)+'';
end;
viewstring:='发送'+viewstring;
fcomm.memo1.lines.add(viewstring);
fcomm.memo1.lines.add('');
if not commflg then
messagedlg('发送失败 !',mterror,[mbyes],0);
end;

procedure TFCOMM.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 TFCOMM.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)+'';
viewstring:='接收'+viewstring;
memo1.lines.add(viewstring);
memo1.lines.add('');
end;

end.
 
接受答案了.
 

Similar threads

S
回复
0
查看
835
SUNSTONE的Delphi笔记
S
S
回复
0
查看
765
SUNSTONE的Delphi笔记
S
后退
顶部