使用ACTIVEX和DELPHI开发串口通讯
2004-03-25 17:25
525 查看
于Delphi中没有串口控件可用,所以首先需要把ActiveX控件MSCOMM加到元件选项板上。这是一个非常好的控件它不仅能对串口进行操作,而且还可以对Modem进行控制。下面结合一个具体的实例来说明如何用MSCOMM控件开发出串口通信程序。
创建一个Communication.dpr工程,把窗体的Name属性变为CommForm,将标题改为The Communication Test,选择File/Save As将新的窗体存储为CommFrm.pas。
其相应代码如下:
变量说明
var
CommForm: TCommForm;
ss :string;
savef,readf :file of char;
i,j :longint;
初始化
procedure TCommForm.FormCreate(Sender: TObject);
begin
mscomm.commport:=1;
mscomm.settings:='9600,n,8,1';
mscomm.inputlen:=1;
mscomm.inbuffercount:=0;
mscomm.portopen:=true;
ss:='';
i:=0;
j:=0;
assignfile(savef,'save1');
rewrite(savef);
assignfile(readf,'read1');
reset(readf);
end;
设置确定
procedure TCommForm.btnConfirmClick(Sender: TObject);
begin
if mscomm.portopen then
mscomm.portopen:=false;
mscomm.commport:=strtoint(edtCommport.text);
mscomm.settings:=edtCommsetting.Text;
end;
传输事件
procedure TCommForm.MSCommComm(Sender: TObject);
var
filenrc :char;
buffer :variant;
s1:string;
c :char;
begin
case mscomm.commEvent of
comEvSend:
begin
while not(eof(readf)) do
begin
read(readf,filenrc);
mscomm.output:=filenrc;
j:=j+1;
lblDisplay.caption:=inttostr(j);
if mscomm.outbuffercount>=2 then
break;
end;
end;
comEvReceive:
begin
buffer:=mscomm.Input;
s1:=buffer;
c:=s1[1];
ss:=ss+c;
i:=i+1;
lblDisplay.caption:=c+inttostr(i);
write(savef,c);
if (c=chr(10))or(c=chr(13)) then
begin
lblDisplay.caption:='cr'+inttostr(i);
memDisplay.lines.add(ss);
ss:='';
end;
end;
end;
end;
创建一个Communication.dpr工程,把窗体的Name属性变为CommForm,将标题改为The Communication Test,选择File/Save As将新的窗体存储为CommFrm.pas。
其相应代码如下:
变量说明
var
CommForm: TCommForm;
ss :string;
savef,readf :file of char;
i,j :longint;
初始化
procedure TCommForm.FormCreate(Sender: TObject);
begin
mscomm.commport:=1;
mscomm.settings:='9600,n,8,1';
mscomm.inputlen:=1;
mscomm.inbuffercount:=0;
mscomm.portopen:=true;
ss:='';
i:=0;
j:=0;
assignfile(savef,'save1');
rewrite(savef);
assignfile(readf,'read1');
reset(readf);
end;
设置确定
procedure TCommForm.btnConfirmClick(Sender: TObject);
begin
if mscomm.portopen then
mscomm.portopen:=false;
mscomm.commport:=strtoint(edtCommport.text);
mscomm.settings:=edtCommsetting.Text;
end;
传输事件
procedure TCommForm.MSCommComm(Sender: TObject);
var
filenrc :char;
buffer :variant;
s1:string;
c :char;
begin
case mscomm.commEvent of
comEvSend:
begin
while not(eof(readf)) do
begin
read(readf,filenrc);
mscomm.output:=filenrc;
j:=j+1;
lblDisplay.caption:=inttostr(j);
if mscomm.outbuffercount>=2 then
break;
end;
end;
comEvReceive:
begin
buffer:=mscomm.Input;
s1:=buffer;
c:=s1[1];
ss:=ss+c;
i:=i+1;
lblDisplay.caption:=c+inttostr(i);
write(savef,c);
if (c=chr(10))or(c=chr(13)) then
begin
lblDisplay.caption:='cr'+inttostr(i);
memDisplay.lines.add(ss);
ss:='';
end;
end;
end;
end;
相关文章推荐
- 使用Delphi开发多媒体播放音轨问题的FAQ(原创)
- 在Delphi中开发使用多显示器的应用程序
- 使用Delphi开发IE按钮扩展
- 使用HTML界面的Delphi开发环境建立
- delphi 开发使用的基类D
- 在Delphi中开发使用多显示器的应用程序
- 使用Delphi和Web Services技术开发短信应用程序
- 使用Delphi快速开发ActiveX控件
- 老歌新唱--使用VB6开发的ActiveX实现.NET程序的混淆加密
- 使用MFC开发ActiveX(cpy)
- delphi开发日志——基窗体,使用面向对象编程的编程思想创建基类
- 使用Delphi快速开发ActiveX控件
- delphi开发日志——基窗体,使用面向对象编程的编程思想创建基类
- delphi 开发使用的基类C
- 使用Delphi 2005 开发 WinCE 程序
- 简单介绍 VC2003 使用 ATL 开发 ActiveX 控件
- 使用Delphi和WebServices技术开发短信应用程序
- delphi 开发使用的基类B
- [Delphi]在Delphi中使用由.NET开发的Web Service返回的DataSet类型
- 在Delphi中开发使用多显示器的应用程序