?? gprs.cpp
字號:
返 回: TRUE: 成功 FALSE: 失敗
********************************************************************************************/
BOOL CGPRS::GPRS_TakeTheCall(void)
{
GPRS_SendATCmd("ATA");
return (GPRS_WaitResponseOK());
}
/*******************************************************************************************
函數(shù)名稱: GPRS_TakeTheCall
描 述: 掛斷電話(有電話打進(jìn)來時, 掛斷電話)
輸入?yún)?shù): 無
輸出參數(shù): 無
返 回: TRUE: 成功 FALSE: 失敗
********************************************************************************************/
BOOL CGPRS::GPRS_HangUpTheCall(void)
{
GPRS_SendATCmd("AT+CHUP");
return (GPRS_WaitResponseOK());
}
/*******************************************************************************************
函數(shù)名稱: GPRS_SetShortMSGCenterTel
描 述: 設(shè)置短信中心號碼
輸入?yún)?shù): CString strTelNum: 短信中心號碼
輸出參數(shù): 無
返 回: TRUE: 成功 FALSE: 失敗
********************************************************************************************/
BOOL CGPRS::GPRS_SetShortMSGCenterTel(CString strTelNum)
{
CString strTmp;
strTmp = strTelNum + ",145";
GPRS_SendATCmd(strTelNum);
return (GPRS_WaitResponseOK());
}
/*******************************************************************************************
函數(shù)名稱: FindMsgPos
描 述: 設(shè)置短信中心號碼
輸入?yún)?shù): CString strTelNum: 短信中心號碼
輸出參數(shù): 無
返 回: TRUE: 成功 FALSE: 失敗
********************************************************************************************/
int CGPRS::FindMsgPos(int *posnum, int *numlen, int *posmsg, int *msglen)
{
// 短信開始位置
int posStart = m_strRespBuf.Find(_T("+CMGR:"), 0);
if (posStart < 0)
return -1;
// 電話號碼開始位置
*posnum = m_strRespBuf.Find(_T("\"REC READ\",\""), posStart);
if (*posnum < 0)
return -1;
*posnum = *posnum + wcslen(_T("\"REC READ\",\""));
// 電話號碼結(jié)束位置
int posnumend = m_strRespBuf.Find(_T("\""), *posnum + 1);
if (posnumend < 0)
return -1;
// 電話號碼長度
*numlen = posnumend - *posnum;
// 短信開始位置
*posmsg = m_strRespBuf.Find(_T("\r\n"), posStart);
if (*posmsg < 0)
return -1;
*posmsg = *posmsg + wcslen(_T("\r\n"));
// 短信結(jié)束位置
int posmsgend = m_strRespBuf.Find(_T("\r\n\r\nOK"), *posmsg);
*msglen = posmsgend - *posmsg;
return 1;
}
/*******************************************************************************************
函數(shù)名稱: GPRS_ReadShortMessage
描 述: 讀取短信內(nèi)容
輸入?yún)?shù): DWORD num : 短信序號
輸出參數(shù): CString *Msg : 短信內(nèi)容
返 回: TRUE: 成功 FALSE: 失敗
********************************************************************************************/
BOOL CGPRS::GPRS_ReadShortMessage(DWORD num, CString *strTelCode, CString *Msg)
{
CString strReadNum;
BOOL ret, retvalue;
strReadNum.Format(_T("AT+CMGR=%d"), num);
bSendATCmd = TRUE;
GPRS_SendATCmd(strReadNum); /* 命令: 讀第 num 條短信 */
while(1)
{
ret = WaitForSingleObject(m_ATCmdRespEvent, 2000);
if (ret == WAIT_OBJECT_0)
{
if (m_strRespBuf.Find(_T("ERROR\r\n"), 0) > 0) /* 響應(yīng)為 ERROR */
{
//AfxMessageBox(_T("讀取短信發(fā)現(xiàn)錯誤"));
retvalue = FALSE;
break;
}
int posnum, numlen, posmsg, msglen;
int pos = FindMsgPos(&posnum, &numlen, &posmsg, &msglen); /* 尋找短信位置 */
if (pos > 0)
{
//char buffer[1024];
//for (int i = 0; i < m_strRespBuf.GetLength(); i++)
// buffer[i] = (char)m_strRespBuf.GetAt(i);
*strTelCode = m_strRespBuf.Mid(posnum, numlen);
*Msg = m_strRespBuf.Mid(posmsg, msglen); /* 取出短信內(nèi)容 */
retvalue = TRUE;
break;
}
ResetEvent(m_ATCmdRespEvent);
}//end of if (ret == WAIT_OBJECT_0)
else
{
//AfxMessageBox(_T("讀取短信超時"));
retvalue = FALSE;
break;
}
}
ResetGlobalVarial();
if (retvalue == FALSE)
return FALSE;
return TRUE;
}
/*******************************************************************************************
函數(shù)名稱: GPRS_SendShortMessage
描 述: 發(fā)送短信
輸入?yún)?shù): CString strTelNum : 對方電話號碼
輸出參數(shù): CString *Msg : 短信內(nèi)容
返 回: TRUE: 成功 FALSE: 失敗
********************************************************************************************/
BOOL CGPRS::GPRS_SendShortMessage(CString strTelNum, CString Msg)
{
BOOL ret, retvalue;
int pos;
/*
* 1. 發(fā)送電話號碼
*/
CString strNum;
strNum.Format(_T("AT+CMGS=\"%s\""), strTelNum);
bSendATCmd = TRUE;
GPRS_SendATCmd(strNum); /* 發(fā)送電話號碼 */
while(1)
{
ret = WaitForSingleObject(m_ATCmdRespEvent, 2000);
if (ret == WAIT_OBJECT_0)
{
pos = m_strRespBuf.Find(_T('>'), 0);
if (pos > 0)
{ /* 收到提示符, 現(xiàn)在可以往模塊寫入短信內(nèi)容了 */
retvalue = TRUE;
break;
}
pos = m_strRespBuf.Find(_T("ERROR\r\n"), 0);
if (pos > 0)
{
retvalue = FALSE; /* GPRS 響應(yīng)字符中包含有"ERROR" */
break;
}
}
else
{
retvalue = FALSE;
break;
}
}
ResetGlobalVarial();
if (retvalue == FALSE) return FALSE;
/*
* 2. 等待 '>' 提示符, 發(fā)送短信內(nèi)容
*/
bSendATCmd = TRUE;
GPRS_WriteMsg(Msg);
while(1)
{
ret = WaitForSingleObject(m_ATCmdRespEvent,500);
if (ret == WAIT_OBJECT_0)
{
int pos = m_strRespBuf.Find(_T("ERROR\r\n"), 0);
if (pos > 0)
{
retvalue = FALSE;
break;
}
pos = m_strRespBuf.Find(_T("CMGS"), 0);
if (pos > 0)
{ /* 響應(yīng)正確, 發(fā)送成功 */
retvalue = TRUE;
break;
}
}
}
ResetGlobalVarial();
if (retvalue == FALSE)
return FALSE;
return TRUE;
}
/*******************************************************************************************
函數(shù)名稱: CheckMsgNum
描 述: 檢查短信數(shù)量: SIN卡中有效短信數(shù)量及可納容的最大短信數(shù)量(private函數(shù))
輸入?yún)?shù): CString str : GPRS 響應(yīng)字符
輸出參數(shù): CString *strNum : 有效短信數(shù)量
CString *strTotal: 可納容的最大短信數(shù)量
返 回: TRUE: 成功 FALSE: 失敗
********************************************************************************************/
BOOL CheckMsgNum(CString str, CString *strNum, CString *strTotal)
{
int pos1 = str.Find(_T("\"SM\","), 0); /* 標(biāo)識字符 */
if (pos1 < 0)
return FALSE;
int pos2 = str.Find(_T(','), pos1 + 5); /* 分隔逗號 */
if (pos2 < 0)
return FALSE;
*strNum = str.Mid(pos1 + 5, pos2 - (pos1 + 5)); /* 現(xiàn)有短信數(shù)量 */
int pos3 = str.Find(_T(","), pos2 + 1);
if (pos3 < 0)
return FALSE;
*strTotal = str.Mid(pos2 + 1, pos3 - pos2 - 1); /* 可納容的最大短信數(shù)量 */
return TRUE;
}
/*******************************************************************************************
函數(shù)名稱: GPRS_CheckMsgNum
描 述: 檢查短信數(shù)量: SIN卡中有效短信數(shù)量及可納容的最大短信數(shù)量(public函數(shù))
輸入?yún)?shù): 無
輸出參數(shù): CString *strNum : 有效短信數(shù)量
CString *strTotal: 可納容的最大短信數(shù)量
返 回: TRUE: 成功 FALSE: 失敗
********************************************************************************************/
BOOL CGPRS::GPRS_CheckMsgNum(CString *strNum, CString *strTotal)
{
BOOL ret, retvalue;
CString strTmp;
bSendATCmd = TRUE;
GPRS_SendATCmd(_T("AT+CPMS?")); /* 查詢命令 */
while(1)
{
ret = WaitForSingleObject(m_ATCmdRespEvent, 500);
if (ret == WAIT_OBJECT_0)
{
int pos = m_strRespBuf.Find(_T("ERROR\r\n"), 0);
if (pos > 0)
{
retvalue = FALSE;
break;
}
if (CheckMsgNum(m_strRespBuf, strNum, strTotal) == TRUE)
{ /* 查詢成功 */
retvalue = TRUE;
break;
}
ResetEvent(m_ATCmdRespEvent);
}
else
{
retvalue = FALSE;
break;
}
}
ResetGlobalVarial();
if (retvalue == FALSE)
return FALSE;
return TRUE;
}
/*******************************************************************************************
函數(shù)名稱: GPRS_DeleteShortMsg
描 述: 刪除短信
輸入?yún)?shù): 無
輸出參數(shù): DWORD num : 短信序號
返 回: TRUE: 成功 FALSE: 失敗
********************************************************************************************/
BOOL CGPRS::GPRS_DeleteShortMsg(DWORD num)
{
CString strCmd;
BOOL ret;
strCmd.Format(_T("AT+CMGD=%d"), num);
GPRS_SendATCmd(strCmd);
ret = GPRS_WaitResponseOK();
if (ret == FALSE)
return FALSE;
return TRUE;
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -