//初始化
procedure Tform1.cominit;
begin
hcom:=createfile(pchar(CurrentPort),generic_read or generic_write,0,nil,open_existing,
file_attribute_normal or file_flag_overlapped,0);
if hcom<>invalid_handle_value then
begin
setupcomm(hcom,4096,4096);
getcommstate(hcom,lpdcb);
getCommTimeouts(hcom,lpcom);
lpdcb.baudrate:=CurrentBaud;
lpdcb.stopbits:=0;
lpdcb.bytesize:=8;
lpdcb.Parity:=Noparity;
lpdcb.evtchar:=char($AA);
lpcom.readintervaltimeout:=MAXDWORD;
lpcom.readtotaltimeoutmultiplier:=0;
lpcom.readtotaltimeoutconstant:=0;
setcommstate(hcom,lpdcb);
setcommtimeouts(hcom,lpcom);
setcommmask(hcom,ev_rxflag);
// setcommmask(hcom,ev_rxchar);
end;
end;
//发送数据
writefile(hcom,Data1.S_Byte,Data1.S_Number,writed,@lpol);
Wait1:=WaitForSingleObject(lpol.hEvent,200);
WaitcommEvent(hcom,dwevtmask,@lpol);
Wait1:=WaitForSingleObject(lpol.hEvent,200);
if ( Wait1 = WAIT_OBJECT_0) then
//接收数据
readnumber := coms.cbInQue;
//得到串口中的数据数目
bo:=readfile(hcom,Temp_Data,readnumber,numbertoread,@lpol);//串口的数据保存到Temp_Data中
for i:=1 to ReadNumberdo