先给你一段例子,供参考:
write方法
procedure WriteToComm( BufferToWrite: TCommBuffer; NumberToWrite: DWORD );
var
dwNumberOfWritten, dwLastError: DWORD;
begin
lpOverlapped^.Offset := 0;
dwNumberOfWritten := 0;
repeat
if not WriteFile( hCommFile, BufferToWrite,
NumberToWrite, dwNumberOfWritten,
lpOverlapped ) then
begin
dwLastError := GetLastError;
if dwLastError <> ERROR_IO_PENDING then
begin
Raise ECommError.Create( 'Error write serial port' );
Exit;
end;
if not GetOverlappedResult( hCommFile, lpOverlapped^,
dwNumberOfWritten, True ) then
begin
Raise ECommError.Create( 'Error write serial port' );
Exit;
end;
end;
Inc( lpOverlapped^.Offset, dwNumberOfWritten );
Dec( NumberToWrite, dwNumberOfWritten );
until (NumberToWrite <= 0);
end;
read方法
procedure ReadFromComm( var BufferToRead: TCommBuffer;
var NumberOfRead: DWORD );
var
dwLastError, dwEvtMask: DWORD;
CommStat: TComStat;
bolOverlappedResult: Boolean;
begin
lpOverlapped^.Offset := 0;
dwEvtMask := 0;
if not ClearCommError( hCommFile, dwLastError, @CommStat ) then
begin
Raise ECommError.Create( 'Error read serial port' );
Exit;
end;
if not WaitCommEvent( hCommFile, dwEvtMask, lpOverlapped ) then
begin
dwLastError := GetLastError;
if dwLastError = ERROR_IO_PENDING then
begin
bolOverlappedResult := GetOverlappedResult( hCommFile,
lpOverlapped^, NumberOfRead, False );
if not bolOverlappedResult then
begin
Exit;
end;
end
else
begin
Exit;
end;
end;
if not ReadFile( hCommFile, BufferToRead,
CommStat.cbInQue, NumberOfRead,
lpOverlapped ) then
begin
Raise ECommError.Create( 'Error read serial port' );
Exit;
end;
if not GetOverlappedResult( hCommFile, lpOverlapped^,
NumberOfRead, True ) then
begin
Raise ECommError.Create( 'Error read serial port' );
Exit;
end;
end;
如果要资料可以给我来信。