J
Junior
Unregistered / Unconfirmed
GUEST, unregistred user!
小可最近在使用delphi编写一个串口通讯程序时遇到一些问题,百思不得其解,
特向各位大虾求教。
在delphi中调用api函数createfile,writefile,readfile等api函数进行串口通讯,
经常遇到在一端发出的数据另一端收不到的情况。跟踪程序显示问题多出在
getoverlappedresult函数返回false,小可怀疑问题可能处在overlapped的i/o上,这个
createfile函数中指明的overlapped读写方式到底是什么意思?在使用overlapped的
读写方式时有什么要注意的地方吗?我的程序结构大概是这样的,有没有什么问题?
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;
望各位大虾多多指教,小可这厢有礼了。
特向各位大虾求教。
在delphi中调用api函数createfile,writefile,readfile等api函数进行串口通讯,
经常遇到在一端发出的数据另一端收不到的情况。跟踪程序显示问题多出在
getoverlappedresult函数返回false,小可怀疑问题可能处在overlapped的i/o上,这个
createfile函数中指明的overlapped读写方式到底是什么意思?在使用overlapped的
读写方式时有什么要注意的地方吗?我的程序结构大概是这样的,有没有什么问题?
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;
望各位大虾多多指教,小可这厢有礼了。