原程序是这样的:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, SPComm;
type
Tfcomm = class(TForm)
Comm1: TComm;
Memo1: TMemo;
Btn_send: TButton;
Button2: TButton;
procedure Btn_sendClick(Sender: TObject);
procedure Formshow(sender: Tobject);
procedure Formclose(sender: Tobject;
var Action: TCloseAction);
procedure Comm1ReceiveData(Sender: TObject;
Buffer: Pointer;BufferLength: Word);
private
{ Private declarations }
public
{ Public declarations }
end;
//变量说明
var
comm1:tcomm;
fcomm:Tfcomm;
viewstring:string;
i:integer;
rbuf,sbuf:array[0..16] of byte;
implementation
{$R *.dfm}
//打开串口
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.Btn_sendClick(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);
move(buffer^,rbuf,bufferlength);
for i:=1 to bufferlength do
viewstring:=viewstring+ inttohex(rbuf,2)+'';
viewstring:='接收'+ viewstring;
memo1.lines.add(viewstring);
memo1.lines.add('' );
end;
end.
将出错的那句改成你的那句后语法没有错误,可以运行了,但。。。。
你对这个程序有什么意见或看法吗?(程序用到了TComm控件 (pcomm))