我是用MScomm控件作的,在DElphi7中通过,就是串口一但有数据进入就产生读取事件,且一次就读入该次的所有数据。 //初始化 with mscomm1 do begin commport:=1; settings:='9600,n,8,1'; inbuffersize:=1024; outbuffersize:=1024; inputmode:=comInputmodebinary; inputLen:=0; Sthreshold:=0; Rthreshold:=1; inBufferCount:=0; OutbufferCount:=0; if portOpen then begin showMessage('错误,通讯端口已被打开!'); end; try portOpen:=true; except raise exception.Create('串口通讯出错!'); end; showmessage('串口通讯成功!'); end;procedure TMainF.MSComm1Comm(Sender: TObject); var indata:olevariant; fields:variant; tem,outbuffer,realaddr,username:string; begin case mscomm1.CommEvent of comEvReceive: begin dataCount:=mscomm1.InBufferCount; indata:=mscomm1.Input; for i:=0 to dataCount-1 do tem:=tem+inttohex(indata[i],2);//tem中的数据就是串口一次收到数据的十六进制的字符串 end; end; end;
//初始化
with mscomm1 do
begin
commport:=1;
settings:='9600,n,8,1';
inbuffersize:=1024;
outbuffersize:=1024;
inputmode:=comInputmodebinary;
inputLen:=0;
Sthreshold:=0;
Rthreshold:=1;
inBufferCount:=0;
OutbufferCount:=0;
if portOpen then
begin
showMessage('错误,通讯端口已被打开!');
end;
try
portOpen:=true;
except
raise exception.Create('串口通讯出错!');
end;
showmessage('串口通讯成功!');
end;procedure TMainF.MSComm1Comm(Sender: TObject);
var
indata:olevariant;
fields:variant;
tem,outbuffer,realaddr,username:string;
begin
case mscomm1.CommEvent of
comEvReceive:
begin
dataCount:=mscomm1.InBufferCount;
indata:=mscomm1.Input;
for i:=0 to dataCount-1 do
tem:=tem+inttohex(indata[i],2);//tem中的数据就是串口一次收到数据的十六进制的字符串
end;
end;
end;