3,245
社区成员
发帖
与我相关
我的任务
分享
BOOL CHMI_manApp::SendCommand2Modem(char *command,char *ecko)
{
return FALSE;
//
// ready to ask
if(!m_pPpiManager) return FALSE;
//
ULONG ulBytesRead;
ULONG ulBytesWritten;
OVERLAPPED oReadOverlapped,oWriteOverlapped;
DWORD dwWaitReadResult,dwWaitWriteResult;
//
memset(&oReadOverlapped,0,sizeof(OVERLAPPED));
oReadOverlapped.hEvent=m_PPIhReadEvent;
//
memset(&oWriteOverlapped,0,sizeof(OVERLAPPED));
oWriteOverlapped.hEvent=m_PPIhWriteEvent;
// reset Events
ResetEvent(m_PPIhReadEvent);
ResetEvent(m_PPIhWriteEvent);
BOOL bResult=0;
//
PurgeComm (m_PPIhComPort,PURGE_TXABORT|PURGE_RXABORT|PURGE_TXCLEAR|PURGE_RXCLEAR);
// write "+++" make modem state changed from data mode to command mode
bResult=WriteFile(m_PPIhComPort,// handle to file
command, //
strlen(command),// number of bytes to write
&ulBytesWritten, // number of bytes written
&oWriteOverlapped);// overlapped buffer
if (!bResult)
{// io_pending
if (GetLastError()!=ERROR_IO_PENDING)
{
AfxMessageBox(IDS_COMTR_IO_ERR);
return FALSE;
}
}
//write command
dwWaitWriteResult=WaitForSingleObject(m_PPIhWriteEvent,100);
switch (dwWaitWriteResult)
{
case WAIT_FAILED://-1
AfxMessageBox(IDS_COMTR_IO_ERR);
return FALSE;
break;
case WAIT_TIMEOUT://0x0102
AfxMessageBox(IDS_COMTR_IO_ERR);
return FALSE;
break;
}
//get ulBytesWritten
// bResult=GetOverlappedResult(m_PPIhComPort,&oWriteOverlapped,&ulBytesWritten,FALSE);
Sleep(100);// wait answer chars
//read answer
char StateOK[MAX_PATH]={0};
int jj;
for (jj=0;jj<sizeof(StateOK);jj++)
{// we don't know how many chars will be received !
bResult=ReadFile(m_PPIhComPort,&StateOK[jj],1,&ulBytesRead,&oReadOverlapped);
if (!bResult)
{// io_pending
if (GetLastError()!=ERROR_IO_PENDING)
{
AfxMessageBox(IDS_COMTR_IO_ERR);
return FALSE;
}
}
//
dwWaitReadResult=WaitForSingleObject(m_PPIhReadEvent,10);
switch (dwWaitReadResult)
{
case WAIT_FAILED://-1
AfxMessageBox(IDS_COMTR_IO_ERR);
return FALSE;
break;
case WAIT_OBJECT_0:
bResult=GetOverlappedResult(m_PPIhComPort,&oReadOverlapped,&ulBytesRead,FALSE);
if (bResult==0) return FALSE;
if(ulBytesRead !=0 )
{//"ATZ 0x0d 0x0d 0x0a OK 0x0d 0x0a" len=10
oReadOverlapped.Offset+=ulBytesRead;
// for next
}
else
{
AfxMessageBox(IDS_COMTR_IO_ERR);
return FALSE;
}
break;
case WAIT_TIMEOUT://0x0102
if(oReadOverlapped.Offset!=0)
{
if(ecko) strcpy(ecko,StateOK);
return TRUE;
}
else
{
//AfxMessageBox("Time out");
return FALSE;
}
break;
}//end switch (dwWaitReadResult)
}// end for
if(jj==sizeof(StateOK)) return FALSE;// string too long
//
return TRUE;
}
SendCommand2Modem(char *command,char *ecko)