2,640
社区成员
发帖
与我相关
我的任务
分享
CStringA strA(m_strSend);
if (WriteFile(hCom, strA, strA.GetLength(), &factdata, &wOverLaped))//开始发送数据
{
}
else
{
if(GetLastError() == ERROR_IO_INCOMPLETE)
{
GetOverlappedResult(hCom, &wOverLaped, &factdata, TRUE);
}
}
///////////////////////////////////////////////////////////
int CDload::WriteCommBlock(BYTE *pBlock,int nBlockLen)
{ //return 0 if error
DWORD dwBytesWritten;
DWORD dwModemStat;
int WaitErr;
BOOL fWriteStat;
if (!m_bConnected) return 0;
dwBytesWritten=0;
fWriteStat=WriteFile(m_idComDev,pBlock,nBlockLen,&dwBytesWritten,&m_osWrite);
if(!fWriteStat)
{ // Writting wait
if (GetLastError()==ERROR_IO_PENDING)
{ // if writting processing
//??? ResetEvent(m_osWrite.hEvent);//???
if (WaitErr=WaitForSingleObject(m_osWrite.hEvent,60000)) // 1 minute
{ // WaitErr!=0
if(WaitErr==WAIT_TIMEOUT)
{ // time out
KillTimer(1);
// if device error!
GetCommModemStatus(m_idComDev,&dwModemStat);
if(dwModemStat &= MS_CTS_ON) // if (CTS_ON && TIME_OUT) then COMM Hardware Errors.
{ // 16 "GREEN"
AfxMessageBox("COMM Device hardware errors.",MB_ICONSTOP);// don't move
m_bLoadStop=1;
dwBytesWritten=0;
}
else
{ // 0 "RED"
AfxMessageBox("CTS timer 60 seconds out!",MB_ICONSTOP);
m_bLoadStop=1;
dwBytesWritten=0;
}
}
// if(WaitErr==WAIT_FAILED) =-1
// else other waiterr
}
else
{ // WaitErr=0.if you SetEvent(m_osWrite.hEvent) anywhere else
GetOverlappedResult(m_idComDev,&m_osWrite,&dwBytesWritten,FALSE);
m_osWrite.Offset +=dwBytesWritten;
}
}
// else // not IO_PENDING
}
return (int)dwBytesWritten;
}