// GsmModem.cpp: implementation of the CGsmModem class.
//
//////////////////////////////////////////////////////////////////////
#include "..\stdafx.h"
#include "GsmModem.h"
//#include "configFile.h"
#include "Sms.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
#define M_GSMTYPE 2 //1为TC35 2为MC45
#ifdef _DEBUG
#undef M_GSMTYPE
#define M_GSMTYPE 1
#endif
#if(M_GSMTYPE==2)
extern "C" void gprs_on();
extern "C" void mic_on(BOOL nIsOn);
#endif
extern char g_strPhoneNum[32];
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////
//构造函数
//////////////////////////////////////////////////////////////////////
CGsmModem::CGsmModem()
{
//设串口为无效值
m_hComm=INVALID_HANDLE_VALUE;
//为解析到的短消息分配空间及初始化
m_pShortMsgInfo=new SHORTMSGINFO;
m_pShortMsgInfo->nMsgType=0;
m_pShortMsgInfo->nMsgLen=0;
m_pShortMsgInfo->pSrcPhoneNum[0]=0;
m_pShortMsgInfo->pTime[0]=0;
m_pShortMsgInfo->MsgData[0]=0;
m_pShortMsgInfo->nRecInfo=0;
//GSM关闭
m_bIsGsmOn=0;
m_bIsSendOver=1;
m_nTransNum=1;
m_nMsgCount = 0;
//初始化变量
for (int i = 0; i < M_MAXMSGNUMBER;i++)
{
m_SaveMsg[i].nCount = -1;
m_SaveMsg[i].nFlag = -1;
for(int j = 0;j < 10; j++)
{
m_SaveMsg[i].bAccess[j] = FALSE;
m_SaveMsg[i].nLength[j] = 0;
m_SaveMsg[i].msg[j]=NULL;
}
}
}
///////////////////////////////////////////////////////////////
//析构函数
///////////////////////////////////////////////////////////////
CGsmModem::~CGsmModem()
{
delete m_pShortMsgInfo;
ClosePort();
}
//////////////////////////////////////////////////////////////////////////////////
//功能:初始化串口
//history name data remark
// wanfangjie 2003.03.05 create
// wanfangjie 2003.04.04 添加错误处理
//参数说明:int iPort 串口的端口号(1~2)
//返回值:1 创建成功 0 创建失败
//////////////////////////////////////////////////////////////////////////////////
int CGsmModem::InitPort(int iPort)
{
COMMTIMEOUTS sCommTimeouts;
DCB sDcb;
char aTempPool[64];
//如果串口开打,关闭该串口
if(m_hComm!=INVALID_HANDLE_VALUE)
{
CloseHandle(m_hComm);
m_hComm=INVALID_HANDLE_VALUE;
}
//创建串口,获取串口句柄
m_hComm = CreateFile(_T("COM1:"),
GENERIC_READ | GENERIC_WRITE, // read/write types
0, // comm devices must be opened with exclusive access
NULL, // no security attributes
OPEN_EXISTING, // comm devices must use OPEN_EXISTING
0, // sync I/O
NULL); // template must be 0 for comm devices
if (m_hComm == INVALID_HANDLE_VALUE)
{
strcpy(aTempPool,"为GSM创建串口错");
return 0;
}
//设串口读写缓冲区
SetupComm(m_hComm,1024,1024);
// 设置时间超时值
// readfile function using "MAXDWORD" can return fleetly.
sCommTimeouts.ReadIntervalTimeout = MAXDWORD;
sCommTimeouts.ReadTotalTimeoutMultiplier = 10;
sCommTimeouts.ReadTotalTimeoutConstant = 1000;
sCommTimeouts.WriteTotalTimeoutMultiplier = 10;
sCommTimeouts.WriteTotalTimeoutConstant = 1000;
//设置时间超时
if(SetCommTimeouts(m_hComm, &sCommTimeouts)==NULL)
{
strcpy(aTempPool,"为GSM设置时间超时错");
return 0;
}
//串口配置
GetCommState(m_hComm, &sDcb);
//set dcb structure
sDcb.fRtsControl = RTS_CONTROL_ENABLE; // set RTS bit high!
sDcb.DCBlength = sizeof( DCB ) ;
sDcb.BaudRate = 9600;
sDcb.ByteSize = 8;
sDcb.fParity = 0;
//在同步方式下,停止位必须为0
sDcb.StopBits = 0;
if(!SetCommState(m_hComm, &sDcb))
{
strcpy(aTempPool,"为GSM设置串口配置错");
return 0;
}
// 清除串口
PurgeComm(m_hComm, PURGE_RXCLEAR | PURGE_TXCLEAR | PURGE_RXABORT | PURGE_TXABORT);
return 1;
}
///////////////////////////////////////////////////////////////////////
//功能:清串口缓冲区
//history name data remark
// wanfangjie 2002.09.23 create
//参数说明 int itype 0 清读缓冲区
// 1 清写缓冲区
// 2 清读写缓冲区
///////////////////////////////////////////////////////////////////////
void CGsmModem::ClearCommBuf(int iType)
{
switch(iType)
{
case 0:
PurgeComm(m_hComm,PURGE_RXCLEAR);
break;
case 1:
PurgeComm(m_hComm,PURGE_TXCLEAR);
break;
case 2:
PurgeComm(m_hComm,PURGE_TXCLEAR|PURGE_RXCLEAR);
break;
default:
PurgeComm(m_hComm,PURGE_TXABORT|PURGE_RXABORT|PURGE_TXCLEAR|PURGE_RXCLEAR);
}
}
///////////////////////////////////////////////////////////////////////
//功能:写串口
//history name data remark
// wanfangjie 2002.09.18 create
//返回值:1 成功 0 失败
///////////////////////////////////////////////////////////////////////
int CGsmModem::WriteDataToPort(const char *pWritePool, long nWriteLen)
{
BOOL nReturn=0;
DWORD nRealLen=0;
if (m_hComm == INVALID_HANDLE_VALUE)
{
return 0;
}
////清除写缓冲区
ClearCommBuf(1);
nReturn=WriteFile(m_hComm,pWritePool,nWriteLen,&nRealLen,NULL);
return nReturn;
}
///////////////////////////////////////////////////////////////////////
//功能:读串口
//history name data remark
// wanfangjie 2002.09.18 create
//参数说明:char *pReadPool 返回串口中的内容
// long nReadLen 要读的长度
// BOOL bIsClearBuf 是否清除读的缓存
//返回值:非0实际读的长度 0 失败
///////////////////////////////////////////////////////////////////////
int CGsmModem::ReadDataFromPort(char *pReadPool, long nReadLen,BOOL bIsClearBuf)
{
BOOL nReturn=0;
DWORD nRealLen=0;
if (m_hComm == INVALID_HANDLE_VALUE)
{
return 0;
}
nReturn=ReadFile(m_hComm,pReadPool,nReadLen,&nRealLen,NULL);
//读成功
if(nRealLen)
{
pReadPool[nRealLen]=0;
}
if(bIsClearBuf)
{
ClearCommBuf(0);//清除读缓冲区
}
return nRealLen;
}
///////////////////////////////////////////////////////////////////////
//功能:等待串口的事件
//history name data remark
// wanfangjie 2002.09.20 create
// wanfangjie 2002.10.10 把返回值由布尔类型改为整型
//参数说明 iEventType 0表示不需要等待事件,立即判断事件是否出现
// 1表示等待事件,直到事件的出现
//返回值:非0 表示事件出现 0 事件没出现
///////////////////////////////////////////////////////////////////////
int CGsmModem::GetCommEvent(int iEventType)
{
int bResult=0;
COMSTAT comStat;
DWORD dEvtMask;
DWORD dwError=0;
if (m_hComm == INVALID_HANDLE_VALUE)
{
return 0;
}
switch(iEventType)
{
case 0:
ClearCommError(m_hComm, &dwError, &comStat);
if(comStat.cbInQue!=0)
{
bResult=comStat.cbInQue;
}
break;
case 1:
//设置的事件为接受缓冲区出现字符
SetCommMask(m_hComm,EV_RXCHAR);
bResult =WaitCommEvent (m_hComm, &dEvtMask, 0);
break;
default:
break;
}
return bResult;
}
//////////////////////////////////////////////////////////////////////////////////////
//功能:初始化GSM
//history name data remark
// wanfangjie 2002.09.20 create
// wanfangjie 2003.04.04 添加错误处理及信号的强弱
//返回值:0,初始化失败,1初始化成功
///////////////////////////////////////////////////////////////////////
int CGsmModem::InitGsm()
{
int nBufLen;
m_bIsGsmOn=0;
//打开GSM
#if(M_GSMTYPE==2)
// gprs_on();
#endif
//判断串口是否打开
if(m_hComm==INVALID_HANDLE_VALUE)
{
//调用打开串口函数
int n = InitPort(1);
if(n==0)
{
int iii = 0;//AfxMessageBox(_T("初始化串口错"));
}
}
//测试模块是否正确与连接
ClearCommBuf(0);
SendATCmd("AT\r");
nBufLen=GetCommEvent(0);
if(nBufLen>0)
{
ReadDataFromPort(m_pReadPool,nBufLen,1);
if((m_pReadPool[0]==M_ERROR)&&strstr(m_pReadPool,"OK")==NULL)
{
// AfxMessageBox(_T("通讯模块坏"));
return 0;
}
}
else
{
// AfxMessageBox(_T("通讯模块未开机"));
return 0;
}
//挂机
SendATCmd("ATH\r");
//取消命令回显
SendATCmd("ATE0\r"
评论0