dephi实现RS232串口通信。本程序需要一根RS232串口线连接两个串口实现。本程序包括两个部分:发送端和接收端
源代码在线查看: sender.pas
unit Sender;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Buttons, OleCtrls, MSCommLib_TLB;
type
TMainForm = class(TForm)
GroupBox1: TGroupBox;
SendedDatas: TMemo;
SendDatas: TBitBtn;
ClearDatas: TBitBtn;
GoodBye: TBitBtn;
MSComm: TMSComm;
procedure GoodByeClick(Sender: TObject);
procedure ClearDatasClick(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure SendDatasClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
MainForm: TMainForm;
implementation
{$R *.dfm}
procedure TMainForm.GoodByeClick(Sender: TObject);
begin
With MSComm Do //关闭串口
Begin
PortOpen:=False;
DTREnable:=False;
RTSEnable:=False;
End;
Close;
end;
procedure TMainForm.ClearDatasClick(Sender: TObject);
begin
SendedDatas.Lines.Clear; //清空Memo的内容
end;
procedure TMainForm.FormCreate(Sender: TObject);
begin
With MSComm Do //设置MSComm的工作参数
Begin
CommPort:=1;
Settings:='9600,n,8,1';
OutBufferSize:=1024;
OutBufferCount:=0;
PortOpen:=True;
End;
end;
procedure TMainForm.SendDatasClick(Sender: TObject);
begin
With MSComm Do //允许MSComm工作
Begin
DTREnable:=True;
RTSEnable:=True;
End;
If Not (SendedDatas.Text='') Then //若输入的数据不为空则发送
MSComm.Output:=SendedDatas.Text
Else
Begin
ShowMessage('请输入要发送的数据!');
SendedDatas.SetFocus;
End;
end;
end.