📄 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 + -