⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mainthread.cpp

📁 PDA通讯网关服务器源码程序
💻 CPP
字号:
// MainThread.cpp : implementation file
//

#include "stdafx.h"
#include "GateAgent.h"
#include "MainThread.h"
#include "SmsCode.h"
#include "Config.h"
#include "DoWithThread.h"
#include "DoSend.h"
#include "PubConst.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CMainThread
extern int                        g_nMax;
extern bool                       g_bRun;

extern st_smsCom g_smsCom[MAX_COM]; //保存短消息句柄函数
bool g_bExistMustBackSms;//是否存在要回送的消息

IMPLEMENT_DYNCREATE(CMainThread, CWinThread)

CMainThread::CMainThread()
{
}

CMainThread::~CMainThread()
{
	if (g_nMax>0)
	{
		g_nMax=g_nMax-1;
		CString strTmp;
		strTmp.Format("主线程退出,目前线程总数目%d",g_nMax);
		m_pDlg->OnInsertMess(strTmp.GetBuffer(0),true,false);		
	}
}

BOOL CMainThread::InitInstance()
{
	// TODO:  perform and per-thread initialization here
	return TRUE;
}

int CMainThread::ExitInstance()
{
	// TODO:  perform any per-thread cleanup here
	return CWinThread::ExitInstance();
}

BEGIN_MESSAGE_MAP(CMainThread, CWinThread)
	//{{AFX_MSG_MAP(CMainThread)
		// NOTE - the ClassWizard will add and remove mapping macros here.
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMainThread message handlers
int CMainThread::Run() 
{
	//读取串口参数
	if (OnReadCgf()<0)
	{
		return -1;
	}
/*
	int  nRetCode = MessageBox(m_pDlg->GetSafeHwnd(),"您是否要清空短信猫(modem)里面的所有短消息?","提示",MB_YESNO|MB_ICONQUESTION);
	if(nRetCode==IDYES)
	{
		for(int i=0;i<MAX_COM;i++)
		{
			if (g_smsCom[i].iCom>1)//找到设备的短信中心号码,然后逐个关闭
			{
				m_pDlg->OnDeleteReadAndSend(g_smsCom[i].iCom);
			}
		}
	}
*/
	
	g_bExistMustBackSms=true;
	//启动发送线程
	if (OnDoSendThread()<0)
	{
		return -1;
	}

	g_bRun = true;
	while(g_bRun)
	{
		//接收处理新消息
		OnRecvNewSms();
		Sleep(m_iReadTime);
	}
		
	return 0;
}

int CMainThread::OnStartDoWithThread(int iCom,char *chTPA,char *chTP_UD,char *chTP_SCTS)
{
	CDoWithThread *pDoWith = NULL;			
		pDoWith =(CDoWithThread *)AfxBeginThread(
			RUNTIME_CLASS(CDoWithThread),
			THREAD_PRIORITY_NORMAL,
			0,
			CREATE_SUSPENDED,
			NULL);

		if (pDoWith != NULL)
		{				
			pDoWith->m_pDlg=m_pDlg;
			memset(pDoWith->m_TPA,0x00,sizeof(pDoWith->m_TPA));
			memset(pDoWith->m_TP_UD,0x00,sizeof(pDoWith->m_TP_UD));
			memset(pDoWith->m_TP_SCTS,0x00,sizeof(pDoWith->m_TP_SCTS));
			memcpy(pDoWith->m_TPA,chTPA,TPA_LEN);
			memcpy(pDoWith->m_TP_UD,chTP_UD,UD_LEN);
			memcpy(pDoWith->m_TP_SCTS,chTP_SCTS,SCTS_LEN);
			pDoWith->m_bAutoDelete = true;
			g_nMax++;
			pDoWith->ResumeThread(); //启动数据收发线程			
		} 
		else
		{
			pDoWith->m_bAutoDelete = true;
			pDoWith->Delete();
			return -1;
		}
		return 0;
}

int CMainThread::OnRecvNewSms()
{
	SM_PARAM stParam[MAX_SMS];	// 发送/接收短消息缓冲区
	int iCount=-10,i=0;
	for (int j=0;j<MAX_COM;j++)//从不同的串口当中读出短信
	{
		if (g_smsCom[j].iCom<1) break; //如果该串口无效的话,则退出
//test
		iCount = m_pDlg->OnRecvSms(g_smsCom[j].iCom,stParam);
		if (iCount>0)
		{
			for (i=0;i<iCount;i++)
			{
//test
//		m_pDlg->OnDoWith(0,"8613755088557","05122317231212","001*d1,2,3,002*d2,3,4#",0);
				m_pDlg->OnDoWith(g_smsCom[j].iCom,stParam[i].TPA,stParam[i].TP_SCTS,stParam[i].TP_UD,0);
				g_bExistMustBackSms=true;
				m_pDlg->OnDeleteAppointed(g_smsCom[j].iCom,stParam[i].index); //读写完毕将数据删除
			}
		}
	}
	return 0;
}


int CMainThread::OnReadCgf()
{
	int iRet = -10;
	CString strTmp,strCom;
	CCfg cfgFile;

	char chCenterNo[16]={0};
	bool bRight=false;//检测设备是否正常
	int i=0;

	if(!cfgFile.OnReadCfg("SYSTEM", "ReadTime", &m_iReadTime))
	{
		m_pDlg->OnInsertMess("读取轮询接收时间失败!");
		return -1;
	}

	if(!cfgFile.OnReadCfg("SYSTEM", "SendTime", &m_iSendTime))
	{
		m_pDlg->OnInsertMess("读取轮询发送时间失败!");
		return -1;
	}

	for(int loop=0;loop<MAX_COM;loop++)
	{
		memset(&g_smsCom[loop],'\0',sizeof(st_smsCom));
		strCom.Format("COM%d",loop+1);
		iRet=m_pDlg->OnOpenCom(strCom.GetBuffer(0),9600,0,8,1);
		if (iRet<0)
		{
			//strTmp.Format("启动端口%s上的设备失败,请仔细检查设备是否处于正常工作状态!",strCom);
			//m_pDlg->OnInsertMess(strTmp.GetBuffer(0));
			continue;
		}

		if (m_pDlg->OnSetInit(iRet)<0) 
		{
			strTmp.Format("初始化端口%s上的设备失败,请仔细检查连接此端口的modem是否正常?",strCom);
			m_pDlg->OnInsertMess(strTmp.GetBuffer(0));
			continue;
		}

		m_pDlg->OnGetSmsCenterNum(iRet,chCenterNo);
		if (strlen(chCenterNo)<5 ||strlen(chCenterNo)>16 ) 
		{
			strTmp.Format("获取设备%s上的sim卡短消息中心号码失败,请仔细检查设备是否处于正常工作状态或者设备上是否有sim卡!",strCom);
			m_pDlg->OnInsertMess(strTmp.GetBuffer(0));
			continue;
		}			
		bRight=true;
		
		g_smsCom[i].iCom=iRet;//保存句柄
		_snprintf(g_smsCom[i].chSca, 16,"%s", chCenterNo);
		_snprintf(g_smsCom[i].chCom,9,"%s",strCom.GetBuffer(0));
		strTmp.Format("串口[%s]的modem启动,句柄为[%d],设备上sim卡短消息中心号码为[%s].",g_smsCom[i].chCom,g_smsCom[i].iCom,g_smsCom[i].chSca);
		m_pDlg->OnInsertMess(strTmp.GetBuffer(0),true,true);
		i++;
	
	}
//test
	if (!bRight) return -1;
	m_pDlg->OnInsertMess("系统启动正常,目前处于服务状态了...",true,true);
	return 0;
}

int CMainThread::OnDoSendThread()
{
	CDoSend *pDoWith = NULL;			
		pDoWith =(CDoSend *)AfxBeginThread(
			RUNTIME_CLASS(CDoSend),
			THREAD_PRIORITY_NORMAL,
			0,
			CREATE_SUSPENDED,
			NULL);

		if (pDoWith != NULL)
		{				
			pDoWith->m_pDlg=m_pDlg;
			pDoWith->m_iSendNum=m_iSendNum;
			pDoWith->m_iSendTime=m_iSendTime;
			pDoWith->m_bAutoDelete = true;
			pDoWith->ResumeThread(); //启动数据收发线程
			g_nMax++;
		} 
		else
		{
			return -1;
		}
		return 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -