1,184
社区成员
发帖
与我相关
我的任务
分享// TWaitComm
constructor TWaitComm.Create( hCom:THandle;dcb:TDCB;po:POVERLAPPED;Buf:PChar;pBufState:TPPortBufState );
begin
FhCom := hCom;
FDcb := dcb;
Fpo := po;
FState := tsRun;
Priority := tpLowest;
FPortBuf := Buf;
FpPortBufState := pBufState;
inherited Create(False);
end;
procedure TWaitComm.SetState( const State:TThreadState );
begin
FState := State;
end;
procedure TWaitComm.ProcRun;
var
dwNOBR,dwErrorFlags:DWORD;
ComStat:TCOMSTAT;
iBufFreeSize:integer;
begin
if (WaitCommEvent(FhCom, FdwEvtMask, Fpo)) then
begin
if ((FdwEvtMask and EV_RXCHAR)=EV_RXCHAR) then
begin
//等待缓冲区空闲
while FpPortBufState^.NowOprt<>opIdle do Sleep(5);
//设置缓冲区为写状态
//取缓冲区剩余空间大小
FpPortBufState^.NowOprt := opInWrite;
iBufFreeSize := PORT_BUFFER_SIZE - FpPortBufState^.Tail;
//读端口数据到缓冲区
// only try to read number of bytes in queue
ClearCommError( FhCom, dwErrorFlags, @ComStat ) ;
if (iBufFreeSize>ComStat.cbInQue) then
dwNOBR := ComStat.cbInQue
else
dwNOBR := iBufFreeSize;
ReadFile(FhCom,FPortBuf[FpPortBufState^.Tail],dwNOBR,dwNOBR,Fpo);
FpPortBufState^.Tail := FpPortBufState^.Tail + dwNOBR;
//恢复缓冲区为空闲
FpPortBufState^.NowOprt := opIdle;
end;
end;
end;
procedure TWaitComm.Execute;
begin
{ Place thread code here }
while True do
begin
case FState of
tsRun:
begin
ProcRun;
end;
tsPause:
begin
Sleep(10);
end;
tsEnd:
begin
Exit;
end;
end; // end case
end; // end while
end;
//=========================TWaitComm