关于readfile(...)的问题.

ran 2000-07-27 09:22:00
大家好:
我在程序中用readfile(...)去读串口数据,在95和98下读取的数据正常,但是到了
NT环境下运行,读出的数据却不正确,是否在NT下使用这个函数有什么特殊要求?
哪位高手知道原因快来救我.
谢谢啦!
...全文
230 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
xiaozuo 2000-11-06
  • 打赏
  • 举报
回复
我编过许多的串行口通讯程序,好像各位朋友说的都不对,其实这是WIndows中通讯口驱动程序的一个Bug,这在MSDN中也有说明,我也碰到过此类问题,主要是和硬件有关。
NT中完全可以不使用读事件(不用ovlaped)读取串行口的数据。
在很少情况下会出现这种错误,主要是在发送数据结束时监测数据的发送状态时出现。原因是操作系统认为发送已经完成,但是实际上硬件还没有完成所有数据的发送,新的检测将破坏未发送完的数据。
经过我的试验,在WIn9x中确实不会发生这种错误,具体原因不知,在NT中在数据发送之后和接受之前插入适当的延时可以解决这个问题。
dingsg 2000-11-06
  • 打赏
  • 举报
回复
BOOL ReadFile(
HANDLE hFile, // handle to file
LPVOID lpBuffer, // data buffer
DWORD nNumberOfBytesToRead, // number of bytes to read
LPDWORD lpNumberOfBytesRead, // number of bytes read
LPOVERLAPPED lpOverlapped // overlapped buffer
);
Parameters
hFile
[in] Handle to the file to be read. The file handle must have been created with GENERIC_READ access to the file.
Windows NT/2000: For asynchronous read operations, hFile can be any handle opened with the FILE_FLAG_OVERLAPPED flag by the CreateFile function, or a socket handle returned by the socket or accept function.

Windows 95/98: For asynchronous read operations, hFile can be a communications resource opened with the FILE_FLAG_OVERLAPPED flag by CreateFile, or a socket handle returned by socket or accept. You cannot perform asynchronous read operations on mailslots, named pipes, or disk files.

lpBuffer
[out] Pointer to the buffer that receives the data read from the file.
nNumberOfBytesToRead
[in] Specifies the number of bytes to be read from the file.
lpNumberOfBytesRead
[out] Pointer to the variable that receives the number of bytes read. ReadFile sets this value to zero before doing any work or error checking. If this parameter is zero when ReadFile returns TRUE on a named pipe, the other end of the message-mode pipe called the WriteFile function with nNumberOfBytesToWrite set to zero.
Windows NT/2000: If lpOverlapped is NULL, lpNumberOfBytesRead cannot be NULL. If lpOverlapped is not NULL, lpNumberOfBytesRead can be NULL. If this is an overlapped read operation, you can get the number of bytes read by calling GetOverlappedResult. If hFile is associated with an I/O completion port, you can get the number of bytes read by calling GetQueuedCompletionStatus.

Windows 95/98: This parameter cannot be NULL.

lpOverlapped
[in] Pointer to an OVERLAPPED structure. This structure is required if hFile was created with FILE_FLAG_OVERLAPPED.
If hFile was opened with FILE_FLAG_OVERLAPPED, the lpOverlapped parameter must not be NULL. It must point to a valid OVERLAPPED structure. If hFile was created with FILE_FLAG_OVERLAPPED and lpOverlapped is NULL, the function can incorrectly report that the read operation is complete.

If hFile was opened with FILE_FLAG_OVERLAPPED and lpOverlapped is not NULL, the read operation starts at the offset specified in the OVERLAPPED structure and ReadFile may return before the read operation has been completed. In this case, ReadFile returns FALSE and the GetLastError function returns ERROR_IO_PENDING. This allows the calling process to continue while the read operation finishes. The event specified in the OVERLAPPED structure is set to the signaled state upon completion of the read operation.

If hFile was not opened with FILE_FLAG_OVERLAPPED and lpOverlapped is NULL, the read operation starts at the current file position and ReadFile does not return until the operation has been completed.

Windows NT/2000: If hFile is not opened with FILE_FLAG_OVERLAPPED and lpOverlapped is not NULL, the read operation starts at the offset specified in the OVERLAPPED structure. ReadFile does not return until the read operation has been completed.

Windows 95/98: For operations on files, disks, pipes, or mailslots, this parameter must be NULL; a pointer to an OVERLAPPED structure causes the call to fail. However, Windows 95/98 supports overlapped I/O on serial and parallel ports.

overit 2000-11-06
  • 打赏
  • 举报
回复
方法不对。请问你用的是NT,还是WINDOWS 9X?在NT下,必须使用读事件,否则返回错误。具体的方法,请看下面的读串口的原码:

COMSTAT ComStat ;
DWORD dwErrorFlags;
DWORD dwLength;
OVERLAPPED os_read;

os_read.hEvent = CreateEvent( NULL, // no security
TRUE, // explicit reset req
FALSE, // initial event reset
NULL ) ; // no name
if (os_read.hEvent == NULL) return -1;

// only try to read number of bytes in queue
ClearCommError( hCom, &dwErrorFlags, &ComStat ) ;

if(!ReadFile( hCom, lpszBlock, nMaxLength, &dwLength, &os_read)) {
if (GetLastError() == ERROR_IO_PENDING) {
// We have to wait for read to complete.
// This function will timeout according to the
// CommTimeOuts.ReadTotalTimeoutConstant variable
// Every time it times out, check for port errors
while(!GetOverlappedResult( hCom, &os_read, &dwLength, TRUE )) {
if(GetLastError() != ERROR_IO_INCOMPLETE) {
ClearCommError( hCom, &dwErrorFlags, &ComStat ) ;
break;
}
}
}
else {
// some other error occurred
dwLength = -1 ;
ClearCommError( hCom, &dwErrorFlags, &ComStat ) ;
}
}
CloseHandle(os_read.hEvent);
return ( dwLength ) ;
seaskyfox 2000-07-27
  • 打赏
  • 举报
回复
NT:如果参数5为NULL,参数4不能为NULL;如果参数5不为NULL,参数4可以为NULL。
95/98:参数4不能为NULL。
U皮特U 2000-07-27
  • 打赏
  • 举报
回复
在NT下和winxx下确实有所差别,将你的相关代码贴出来看看。

16,471

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧