没有环境,写得比较粗糙,用TList保存你的串口列表,增删都没有问题,所有的结果都保存在一个TStringList里面
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, SPComm, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
FCommList: TList;//串口列表
FRecvList: TStringList;// 结果列表
procedure CommReceiveData(Sender: TObject; Buffer: Pointer; BufferLength: Word);
procedure Process(AIndex: Integer);
public
{ Public declarations }
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
{ TForm1 }
constructor TForm1.Create(AOwner: TComponent);
begin
inherited;
FCommList := TList.Create;
FRecvList := TStringList.Create;
end;
destructor TForm1.Destroy;
var
I : Integer;
begin
for I:= 0 to FCommList.Count - 1 do
TSPComm(FCommList.Items).Free;
FCommList.Free;
FRecvList.Free;
inherited;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
spcomm : TSPComm;
begin
//
spcomm := TSPComm.Create//……
//……
spcomm.tag := FCommList.Count + 1;
spcomm.OnReceiveData:=CommReceiveData;
FRecvList.Add('');
FCommList.Add(spcomm);
end;
procedure TForm1.CommReceiveData(Sender: TObject; Buffer: Pointer;BufferLength: Word);
var
spcomm: TSPComm;
strRecv : string;
begin
setLength(strRecv,BufferLength);
Move(Buffer^,pchar(strRecv)^,BufferLength);
spcomm := TSPComm(Sender);
FRecvList.Strings[spcomm.tag] := FRecvList.Strings[spcomm.tag] + strRecv;
// 添加处理函数
Process(spcomm.tag);
end;
procedure TForm1.Process(AIndex: Integer);
begin
if AIndex > FRecvList.Count then Exit;
//取字符 FRecvList.Strings[AIndex]
end;
end.