| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 693 人关注过本帖
标题:看不懂的串口程序
只看楼主 加入收藏
firedance
Rank: 1
等 级:新手上路
帖 子:14
专家分:0
注 册:2007-5-7
收藏
 问题点数:0 回复次数:2 
看不懂的串口程序

大家好,我有两个串口程序不太明白,能帮忙解决一下吗?
一:
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, Buttons, StdCtrls, OleCtrls, MSCommLib_TLB;

type
TCommForm = class(TForm)
Label1: TLabel;
memDisplay: TMemo;
Label2: TLabel;
Label3: TLabel;
edtCommport: TEdit;
edtCommsetting: TEdit;
OpenDialog1: TOpenDialog;
SaveDialog1: TSaveDialog;
MSComm1: TMSComm;
Display: TLabel;
Button1: TButton;
BitBtn1: TBitBtn;
StaticText1: TStaticText;
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure MSComm1Comm(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
CommForm: TCommForm;
Receiver:TObject;
Display:TObject;
ss,caption:string;
savef,readf:file of char;
i,j:longint;
implementation

{$R *.dfm}

//初始化
procedure TCommForm.FormCreate(Sender: TObject);
begin
MSComm1.commport:=1;
MSComm1.settings:='9600,N,8,1'; //将通信口设置为9600bit/s,无奇偶校验,8个数据位,1个停止位
MSComm1.inputlen:=1;
MSComm1.inbuffercount:=0;
MSComm1.portopen:=true;
ss:='';
i:=0;
j:=0;
assignfile(savef,SaveDialog1.FileName);
rewrite(savef);
assignfile(readf,OpenDialog1.FileName);
reset(readf);
end;

//设置确定
procedure TCommForm.Button1Click(Sender: TObject);
begin
if MSComm1.portopen then
MSComm1.portopen:=false;
MSComm1.commport:=StrToInt(edtCommport.text);
MSComm1.settings:=edtCommsetting.Text;
end;

procedure TCommForm.MSComm1Comm(Sender: TObject);
var
filenrc:char;
buffer:variant;
s1:string;
c:char;
begin
case MSComm1.commEvent of
comEvSend:
begin
while not(eof(readf)) do
begin
read(readf,filenrc);
MSComm1.output:=filenrc;
j:=j+1;
Display.caption:=inttostr(j);
if MSComm1.OutBufferCount>=2 then
break;
end;
end;
comEvReceive:
begin
buffer:=MSComm1.Input;
s1:=buffer;
c:=s1[1];
ss:=ss+c;
i:=i+1;
Display.caption:=c+inttostr(i);
write(savef,c);
if (c=chr(10))or(c=chr(13)) then
begin
Display.caption:='cr'+IntToStr(i);
memDisplay.lines.add(ss);
ss:='';
end;
end;
end;
end;
end.

上面的程序我看不懂,也不知道它是怎么进行传输的?我不知道怎么操作它,这是书上的一个例子,求各位帮我分析一下


二:
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, OleCtrls, MSCommLib_TLB;

type
TForm1 = class(TForm)
Memo1: TMemo;
Label1: TLabel;
ComboBox1: TComboBox;
ComboBox2: TComboBox;
Label2: TLabel;
Label3: TLabel;
Button1: TButton;
Button2: TButton;
MSComm1: TMSComm;
procedure FormCreate(Sender: TObject);
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure MSComm1Comm(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
begin
MSComm1.InBufferCount:=0; //清空接收缓冲区
MSComm1.InputLen:=0; //Input读取整个缓冲区内容
MSComm1.RThreshold:=1; //每次接收到字符即产生OnComm事件
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
MSComm1.Settings:=ComboBox1.Text;
if ComboBox2.Text='COM1' then //假设只考虑COM1和COM2两种情况
MSComm1.CommPort:=1
else
MSComm1.CommPort:=2;
MSComm1.PortOpen:=true; //打开串口
MSComm1.DTREnable:=true; //数据终端准备好
MSComm1.RTSEnable:=true; //请求发送
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
MSComm1.PortOpen:=false; //关闭串口
MSComm1.DTREnable:=false;
MSComm1.RTSEnable:=false;
end;

procedure TForm1.MSComm1Comm(Sender: TObject);
var
recstr:Olevariant;
begin
if MSComm1.CommEvent=2 then
begin
recstr:=MSComm1.Input;
Memo1.Text:=Memo1.Text+recstr;
end;

end;
end.

上面这个程序有一部分不懂,就是:
procedure TForm1.MSComm1Comm(Sender: TObject);
var
recstr:Olevariant;
begin
if MSComm1.CommEvent=2 then
begin
recstr:=MSComm1.Input;
Memo1.Text:=Memo1.Text+recstr;
end;

end;

不知道是什么意思,为什么Comm1.Input=2呢?


辛苦大家了,谢谢








搜索更多相关主题的帖子: 串口 
2007-05-17 09:23
anthony634
Rank: 6Rank: 6
来 自:西南交大
等 级:贵宾
威 望:24
帖 子:653
专家分:10
注 册:2006-6-8
收藏
得分:0 

前面是端口初始化,下面是事件驱动

case MSComm1.commEvent of
comEvSend://发送
begin
while not(eof(readf)) do
begin
read(readf,filenrc);//读文件
MSComm1.output:=filenrc;//写入缓冲
j:=j+1;
Display.caption:=inttostr(j);
if MSComm1.OutBufferCount>=2 then
break;
end;
end;
comEvReceive://接收事件
begin
buffer:=MSComm1.Input;//从接收缓冲返回字符
s1:=buffer;
c:=s1[1];
ss:=ss+c;
i:=i+1;
Display.caption:=c+inttostr(i);
write(savef,c);
if (c=chr(10))or(c=chr(13)) then
begin
Display.caption:='cr'+IntToStr(i);
memDisplay.lines.add(ss);
ss:='';
end;
end;

if MSComm1.CommEvent=2 then
实际是:if MSComm1.CommEvent=ComEvReceive then //ComEvReceive的值是2,建议写成ComEvReceive,这样可读性好点
表示接收事件,接收到Rthreshold个字符,持续到接收缓冲区中的数据被清除。


[此贴子已经被作者于2007-5-17 10:38:53编辑过]

2007-05-17 10:37
firedance
Rank: 1
等 级:新手上路
帖 子:14
专家分:0
注 册:2007-5-7
收藏
得分:0 

谢谢

2007-05-17 19:09
快速回复:看不懂的串口程序
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.022254 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved