?? gsmport.cpp
字號:
// GSMPort.cpp: implementation of the CGSMPort class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "GSMPort.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// CGSMDataFrame Construction/Destruction
//////////////////////////////////////////////////////////////////////
IMPLEMENT_DYNAMIC(CGSMDataFrame, CObject)
CGSMDataFrame::CGSMDataFrame()
{
m_arrNumOfMsg.RemoveAll();
m_lpMsgData = NULL;
m_nMsgDataLength = 0;
}
CGSMDataFrame::~CGSMDataFrame()
{
m_arrNumOfMsg.RemoveAll();
if (m_lpMsgData/*!=NULL*/)
delete[] m_lpMsgData;
}
BOOL CGSMDataFrame::CopyMsg(const BYTE* lpMsgData, const UINT& nMsgDataLength)
{
Detach();
if (nMsgDataLength < 2)
return FALSE;
m_nMsgDataLength = nMsgDataLength;
if (nMsgDataLength <= 0)
m_lpMsgData = NULL;
else if (nMsgDataLength > 0 && nMsgDataLength < 4)
m_lpMsgData = new BYTE[4];
else
m_lpMsgData = new BYTE[nMsgDataLength];
if (!m_lpMsgData)
return FALSE;
::memcpy((void*)m_lpMsgData, (const void*)lpMsgData, nMsgDataLength);
AnalyseMsg();
return TRUE;
}
void CGSMDataFrame::Detach(void)
{
m_arrNumOfMsg.RemoveAll();
m_nMsgDataLength = 0;
if (m_lpMsgData/*!=NULL*/)
delete[] m_lpMsgData;
m_lpMsgData = NULL;
}
void CGSMDataFrame::AnalyseMsg(void)
{
for (UINT i = 0; i < m_nMsgDataLength; i++)
if (m_lpMsgData[i] == 0xAA) {
m_arrNumOfMsg.Add(i);
}
}
BOOL CGSMDataFrame::NewEmptyFrame(const UINT& nLength)
{
Detach();
if (nLength < 2)
return FALSE;
if (nLength < 4)
m_lpMsgData = new BYTE[4];
else
m_lpMsgData = new BYTE[nLength];
if (!m_lpMsgData)
return FALSE;
m_nMsgDataLength = nLength;
return TRUE;
}
BOOL CGSMDataFrame::SetCommand(const USHORT& Command)
{
if (!m_lpMsgData)
return FALSE;
m_nMsgDataLength = COMMAND_LENGTH;
::memcpy((void*)m_lpMsgData, (const void*)&Command, sizeof(USHORT));
return TRUE;
}
USHORT CGSMDataFrame::GetCommand(const UINT& nIndex) const
{
ASSERT(nIndex >= 0 && nIndex < GetNumOfMsg());
if (m_lpMsgData)
return *((USHORT*)(m_lpMsgData + m_arrNumOfMsg[nIndex]));
return NULL;
}
BOOL CGSMDataFrame::SetDataFrame(const CString& strData)
{
ASSERT(m_lpMsgData != NULL);
if (m_nMsgDataLength != 2 && strData.GetLength() > 142 && strData.GetLength() < 0)
return FALSE;
m_nMsgDataLength += strData.GetLength();
::memcpy((void*)(m_lpMsgData + 2), (const void*)(LPCTSTR)strData, strData.GetLength());
return TRUE;
}
BYTE* CGSMDataFrame::GetDataFrame(const UINT& nIndex, UINT& pMsgLength)
{
ASSERT(m_lpMsgData != NULL);
ASSERT(nIndex >= 0 && nIndex < GetNumOfMsg());
pMsgLength = 0;
if (m_nMsgDataLength <= 2)
return NULL;
else if (nIndex == GetNumOfMsg() - 1)
pMsgLength = m_nMsgDataLength - m_arrNumOfMsg[GetNumOfMsg() - 1];
else
pMsgLength = m_arrNumOfMsg[nIndex + 1] - m_arrNumOfMsg[nIndex];
pMsgLength -= 2;
if (pMsgLength == 0)
return NULL;
return m_lpMsgData + m_arrNumOfMsg[nIndex] + 2;
}
//////////////////////////////////////////////////////////////////////
// CGSMPort Construction/Destruction
//////////////////////////////////////////////////////////////////////
IMPLEMENT_DYNAMIC(CGSMPort, CSerialPort)
CGSMPort::CGSMPort()
{
}
CGSMPort::~CGSMPort()
{
}
BOOL CGSMPort::HandleReadedEvent(LPVOID pParam, const DWORD& dwReadCount)
{
m_DataFrame.CopyMsg((const BYTE*)pParam, dwReadCount);
return CSerialPort::HandleReadedEvent((LPVOID)&m_DataFrame, dwReadCount);
}
BOOL CGSMPort::HandleWriteEvent(void)
{
try {
DWORD dwWriteCount = 0;
Write((LPVOID)(BYTE*)m_DataFrame, m_DataFrame.GetDataFrameLength(), m_WriteOverlapped, NULL);
GetOverlappedResult(m_WriteOverlapped, dwWriteCount, TRUE);
} catch(CSerialException* pEx) {
pEx->Delete();
}
return CSerialPort::HandleWriteEvent();
}
void CGSMPort::SendGsmMsg(const USHORT& Command, const CString& strData)
{
m_DataFrame.NewEmptyFrame();
m_DataFrame.SetCommand(Command);
m_DataFrame.SetDataFrame(strData);
SetWriteEvent();
}
CString CGSMPort::Process_00H(const BYTE* lpMsgData, const UINT& nMsgLength)
{
CString strInfo = _T("");
for (UINT i = 0; i < nMsgLength; i++)
strInfo += (char)lpMsgData[i];
if (strInfo.Left(13) == _00H_MODEMINFO)
strInfo.Format(_T("設備名稱:%s\n設備版本:%s"), _00H_MODEMINFO, strInfo.Mid(14, 16));
else if (strInfo.Left(20) == _00H_MODEMINITIAL)
strInfo = _T("GSM MODEM 初始化完成,啟動正常");
else if (strInfo.Left(20) == _00H_MODEMENGINE)
strInfo = _T("GSM MODEM 啟動過程中沒有檢測到GSM引擎,可能原因是GSM引擎損壞");
else if (strInfo.Left(16) == _00H_MODEMSIMERROR)
strInfo = _T("GSM MODEM 中的SIM卡已損壞,請更換SIM卡");
else if (strInfo.Left(19) == _00H_MODEMNOTSIM)
strInfo = _T("GSM MODEM 中未安裝SIM卡,可能的原因是沒有安裝SIM卡或SIM卡安裝不正確");
else if (strInfo.Left(15) == _00H_MODEMANTENNA)
strInfo = _T("GSM MODEM信號很弱,可能是天線連接異常或網絡信號弱");
else if (strInfo.Left(11) == _00H_MODEMOTHER)
strInfo = _T("用戶通信數據幀錯誤或短信數據超過140字節");
else
strInfo = _T("錯誤通信數據幀");
return strInfo;
}
CString CGSMPort::Process_01H(const BYTE* lpMsgData, const UINT& nMsgLength)
{
CString strInfo = _T("");
for (UINT i = 0;i < nMsgLength; i++)
strInfo += (char)lpMsgData[i];
if (strInfo.Left(5) == _01H_ERROR)
strInfo = _T("短信未送出:可能的原因是SIM卡故障、存款不足或未登陸網絡!");
if (strInfo.Left(2) == _01H_OK)
strInfo = _T("短信已送出,但未到達短信中心,可能是信號質量差或天線連接不好!");
if (strInfo.Left(7) == _01H_SENDOK)
strInfo = _T("接收方已經接收到短信");
if (strInfo.Left(7) == _01H_SUCCESS)
strInfo = _T("短信已經成功送達用戶");
if (strInfo.Left(7) == _01H_FAILURE)
strInfo = _T("未能將短信送達接收方或接收方已經收到短信,但其回應短信沒有接收到!");
return strInfo;
}
BYTE CGSMPort::Process_14H(const BYTE* lpMsgData, const UINT& nMsgLength)
{
if (nMsgLength != 1)
return 0;
return *lpMsgData;
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -