将 mscomm32.ocx 控件注册于 delphi 下。发送过程:procedure comm_send(comm_out_array:array of byte); //var i,j:integer; comm_out_array:array of byte;begin //发送数据 if mscomm.PortOpen=true then mscomm.Output:=comm_out_array;end;
接收过程:procedure receive();
var in_variant:variant;
in_array:array of byte;
i:integer;
begin
if mscomm.InBufferCount>0 then
begin
in_variant:=mscomm.Input; //接收 i
n_array:=in_variant;
end;
end;接收可用oncomm中断也可用查询。
接收过程:procedure receive();
var in_variant:variant;
in_array:array of byte;
i:integer;
begin
if mscomm.InBufferCount>0 then
begin
in_variant:=mscomm.Input; //接收 i
n_array:=in_variant;
end;
end;接收可用oncomm中断也可用查询。
// 看过后给分!
unit Unit1;interfaceuses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, OleCtrls, MSCommLib_TLB;type
TForm1 = class(TForm)
MSComm1: TMSComm;
Memo1: TMemo;
ComboBox1: TComboBox;
ComboBox2: TComboBox;
Button1: TButton;
Button2: TButton;
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 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.