给你个例子
const WM_RECVDATA = WM_USER + 1;
//Mainform
TMainForm = class(TForm)
...
private
procedure RecvData(var msg: TMessage);message WM_RECVDATA;
...
end;
procedure TMainForm.RecvData(var msg: TMessage);
var
s: string;
begin
s := string(msg.lparam);
memo1.lines.add(s);
//释放内存
dispose(pointer(msg.lparam));
end;
procedure TMainForm.OnCreate(Sender: TObject);
var
threadid: Cardinal;
begin
...
//创建线程
createthread(nil, 0, @CommFunc, nil, 0, ThreadID);
...
end;
//接收线程
function CommFunc(P: Pointer): Longint; stdcall;
var
buf: pchar;
begin
//分配内存
getmem(buf, 1024)
//读串口略
...
...readfile(...);
...
PostMessage(MainForm.Handle, WM_RECVDATA, 0, integer(buf));
end;