在网上看到这么一段代码,我关于参数的理解,请高手分析对不对。
请高手指教。int CSerial::ReadData( void *buffer, int limit )
{
if( !m_bOpened || m_hIDComDev == NULL ) return( 0 );
BOOL bReadStatus;
DWORD dwBytesRead, dwErrorFlags;
COMSTAT ComStat;
ClearCommError( m_hIDComDev, &dwErrorFlags, &ComStat );
if( !ComStat.cbInQue ) return( 0 ); dwBytesRead = (DWORD) ComStat.cbInQue;//缓冲区里面字符数传给dwBytesRead,比如100个 bReadStatus = ReadFile( m_hIDComDev,
buffer,
dwBytesRead,//一次读取的字符数,100个
&dwBytesRead,//已经读取的字符数,比如可能是90个,80个
//在这里dwBytesRead的值讲被替换成90或者80吗?
&m_OverlappedRead ); if( !bReadStatus ){
if( GetLastError() == ERROR_IO_PENDING ){
WaitForSingleObject( m_OverlappedRead.hEvent, 2000 );
return( (int) dwBytesRead );
}
return( 0 );
} return( (int) dwBytesRead );//这是值是多少呢,是90或者80吗?而不是最初的100了是吧?}
请高手指教。int CSerial::ReadData( void *buffer, int limit )
{
if( !m_bOpened || m_hIDComDev == NULL ) return( 0 );
BOOL bReadStatus;
DWORD dwBytesRead, dwErrorFlags;
COMSTAT ComStat;
ClearCommError( m_hIDComDev, &dwErrorFlags, &ComStat );
if( !ComStat.cbInQue ) return( 0 ); dwBytesRead = (DWORD) ComStat.cbInQue;//缓冲区里面字符数传给dwBytesRead,比如100个 bReadStatus = ReadFile( m_hIDComDev,
buffer,
dwBytesRead,//一次读取的字符数,100个
&dwBytesRead,//已经读取的字符数,比如可能是90个,80个
//在这里dwBytesRead的值讲被替换成90或者80吗?
&m_OverlappedRead ); if( !bReadStatus ){
if( GetLastError() == ERROR_IO_PENDING ){
WaitForSingleObject( m_OverlappedRead.hEvent, 2000 );
return( (int) dwBytesRead );
}
return( 0 );
} return( (int) dwBytesRead );//这是值是多少呢,是90或者80吗?而不是最初的100了是吧?}
解决方案 »
- 求助:怎么把缓冲里的数据保存为 .png 格式的图片,谢谢!
- 关于几个代理在注册表的疑问。。。
- 关于openssl连接https服务器以后自动登录 急急急!!!
- 请问打包的问题,我用的是wise iInstallMaster 8.1为什么我在编译的时候会出现错误: “目标路径必须用一个变量开头”
- 请问,哪有C++和VC60简明类库参考手册下载.
- 关于 读取配置数据丢失的问题
- 能不能暂停Windows中所有用户应用程序的执行?
- 问一个很弱的问题
- 请问:如何用数组来保存鼠标移动时的上一次的point和当前的point?
- 一个基础问题,怎样抓住计算过程的溢出
- DLL调试的问题。
- flash控件的问题
所以后续操作依赖的实际字符数应该是返回的结果dwBytesRead。
__in HANDLE hFile,
__out LPVOID lpBuffer,
__in DWORD nNumberOfBytesToRead,
__out_opt LPDWORD lpNumberOfBytesRead,
__inout_opt LPOVERLAPPED lpOverlapped
);lpNumberOfBytesRead
A pointer to the variable that receives the number of bytes read.ReadFile sets this value to zero (0) before doing any work or error checking. If this parameter is zero (0) when ReadFile returns TRUE on a named pipe, the other end of the message mode pipe calls the WriteFile function with nNumberOfBytesToWrite set to zero (0).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.If I/O completion ports are used and you are using a callback routine to free the memory that is allocated to the OVERLAPPED structure pointed to by the lpOverlapped parameter, specify NULL as the value of this parameter to avoid a memory corruption problem during the deallocation. This memory corruption problem causes an invalid number of bytes to be returned in this parameter.
dwBytesRead = min(limit, ComStat.cbInQue);
我是想用第四个参数来判断,数据是否接受完整。
下面的代码成立吗?我运行的时候不理想。do{
sleep(150);
bReadStatus = ReadFile( m_hIDComDev,
buffer,
dwBytesRead,//一次读取的字符数,100个
&dwBytesRead,//已经读取的字符数,比如可能是90个,80个
//在这里dwBytesRead的值讲被替换成90或者80吗?
&m_OverlappedRead );
}while(dwBytesRead!=(DWORD) ComStat.cbInQue)
我是想用第四个参数来判断,数据是否接受完整。
下面的代码成立吗?我运行的时候不理想。
[code=C/C++]do{
sleep(150);
bReadStatus = ReadFile( m_hIDComDev,
buffer,
dwBytesRead,//一次读取的字符数,100个
&dwBytesRead,//已经读取的字符数,比如可能是90个,80个
//在这里dwBytesRead的值讲被替换成90或者80吗?
&m_OverlappedRead );
}while(dwBytesRead!=(DWORD) ComStat.cbInQue)
[code]