现时DELPHI上有很多串行口控件,SPCOMM控件有Data Bits、Parity、 Stop Bits 等配置,支持 Read/Write 时序控制 (Timing control)、 ReadIntervalTimeout、 WriteIntervalTimout 等 ,支持 DTR/DSR, RTS/CTS 硬件流程控置及 Xon/Xoff 软件流程控置,是目前比较完善的控件。 以下是一个用RS-232进行数据接收、显示的例子。必须将RS232的通信参数设置好才能正确接收数据。(Data Bits,Parity,Stop Bits,COM口参数)
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, SPComm;
type
TForm1 = class(TForm)
Comm1: TComm;
Memo1: TMemo;
procedure Comm1ReceiveData(Sender: TObject; Buffer: Pointer;
BufferLength: Word);
procedure FormCreate(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
procedure TForm1.Comm1ReceiveData(Sender: TObject; Buffer: Pointer;
BufferLength: Word);
var
s: string;
begin
SetLength(S, BufferLength); //接收RS232的数据并显示Memo1上。
Move(Buffer^, PChar(S)^, BufferLength);
Memo1.Lines.Add(S);
Memo1.Invalidate;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
Comm1.startcomm;//创建窗体时,将comm1控件打开。
end;
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
comm1.StopComm;//关闭窗体时,将comm1控件关闭。
end;
end.