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

📄 procsm.cpp

📁 用VC写的监控中心后台程序。 代码全
💻 CPP
📖 第 1 页 / 共 3 页
字号:
// ProcSM.cpp: implementation of the CProcSM class.
//
///////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "GpsSC.h"
#include "ProcSM.h"
#include "Unicode.h"
#include "Afxcoll.h"
#include "Afxmt.h"
#include "SysIniteDlg.h"
#include "TreeCtrlEx.h"
#include "VehicleMessageSet.h"
#include "ClientinfoSet.h"


#include "MainFrm.h"

#include "DiverNumberDlg.h"
#include "UserTypeDlg.h"
#include "MonitorDlg.h"
#include "ChooseTimeDlg.h"
#include "ChangePassWordDlg.h"
#include "ComUserDlg.h"
#include "GpsScView.h"
#include "SMdata.h"


#include "ctype.h"


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

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////


//the globale data
CString				strToBeWriteToComm;		//最终将要发送到串行口的字符串
CGpsSCDoc*			m_pDoc;					//the ducument pointor for send message
CStringList			gStringList;
CCriticalSection	m_cMessageDealer;		//the critical varaible
CString				strToBeJudge("");		//the string to be process

//the thread fuction for write somthing to the comm
UINT WriteThread(LPVOID lparam)
{
	CProcSM * m_pProcsm = (CProcSM *) lparam;
	while(1) {	
		if(strToBeWriteToComm.IsEmpty()) Sleep(10);	
		if(m_pDoc->m_bConnected == FALSE) continue;

		if((strToBeWriteToComm.GetLength() != 0) && (m_pDoc != NULL)) {
			m_pProcsm->WriteComPort(strToBeWriteToComm,m_pDoc);
			strToBeWriteToComm.Empty();		
		}	
	}

	return 1;
}

//THE process thread
UINT ProcessThread(LPVOID lparam)
{
	CProcSM		*pProcsm = (CProcSM*)lparam;
	while(1) {
		Sleep(1000);

		if(gStringList.GetCount() != 0) {
			strToBeJudge.Empty();
			for(int k = 0; k < gStringList.GetCount(); k++) {
				strToBeJudge += gStringList.GetAt(gStringList.FindIndex(k));
			}
		}
		if(!strToBeJudge.IsEmpty()) {
			pProcsm->JudgeReceivedMsg(strToBeJudge);
		}			
		
		m_cMessageDealer.Lock();

		if(!strToBeJudge.IsEmpty()) strToBeJudge.Empty();

		m_cMessageDealer.Unlock();
	}
}

CProcSM::CProcSM()
{	
	strToBeWriteToComm		= "";            //最终将要发送到串行口的字符串
	m_sVehicleNumber        = "13751709724";
	m_strDriverNumber		= "13751709724";

	strCommonUserPassWord	= "123456";
	m_bWaveComInite			= TRUE;
	m_nUserType				= 0;
	m_bSysTemInit			= FALSE;

	m_pView					= NULL;

	m_sCurrMonitorCar.Empty();
	m_sGetPositionCar.Empty();
}


CProcSM::~CProcSM()
{

}

CString CProcSM::ConstructSendstr(CString strOrder,CString m_sVehicleNumber,CString strSMConttent)
{
	CString 	str2Send;

	str2Send.Empty();
	str2Send += strOrder;
	str2Send += "\"";
	str2Send += m_sVehicleNumber;
	str2Send += "\"\r";
	str2Send += strSMConttent;
	str2Send += "\x1a\r";

	return	str2Send;
}

/********************************************************************/
//a reload function
//the software send to the wavecom chip,all the order is command
//so the mSleepTime only depend on the order not depend on the 
//conttent of the smconttetn ,because the scconttent is the order 
/********************************************************************/
void CProcSM::SetStr2Send(CString sSendStr)
{
	if(!m_bWaveComInite) {
		AfxMessageBox("电话系统没有初始化\n,请初始化电话系统!");
		return;
	}
	
	if(!sSendStr.IsEmpty())	strToBeWriteToComm = sSendStr;
}

void CProcSM::JudgeReceivedMsg(CString strReceived)
{
	if(strReceived.IsEmpty())	return;
	strReceived.Replace(" ","");

	CString		SM_Num,ShortMessage;
	int			nFind;

	SM_Num	= NewSMIndication(strReceived);
	if(SM_Num != "") {
		nFind	= strReceived.Find("CMGR");
		if(nFind == -1) {
			m_cMessageDealer.Lock();
			strToBeWriteToComm	= READONESHORTMESSAGE;
			strToBeWriteToComm.Replace("XXX",SM_Num);
			m_cMessageDealer.Unlock();
			return ;
		}
	}

	//读完一条短信息并处理后,立即删除刚读的那一条短信息
	SM_Num = GetNewSMData(strReceived,ShortMessage);
	if(!SM_Num.IsEmpty()) {
		m_cMessageDealer.Lock();
		ProcShortMessage(ShortMessage);
		strToBeWriteToComm  = DELONESHORTMESSAGE;
		strToBeWriteToComm.Replace("XXX",SM_Num);
		m_cMessageDealer.Unlock();
	}

	SendMessageResult(strReceived);

	ClearStrList();

	DealSystemMsg(strReceived);
}

void CProcSM::ClearStrList()
{
	m_cMessageDealer.Lock();

	if(!gStringList.IsEmpty())	gStringList.RemoveAll();

	m_cMessageDealer.Unlock();
}

void CProcSM::ClearVehicleList()
{
	m_lCurrMonitorVehicle.RemoveAll();
}

void CProcSM::SendMessageResult(CString strReceived)
{
	if(strReceived.IsEmpty())	return ;

	int 		nFind1, nFind2;

	nFind1	= strReceived.Find("+CMGS");
	nFind2	= strReceived.Find("OK");
	if((nFind1 != -1) && (nFind2 != -1)) {
//		AfxMessageBox("短信发送成功!");
	}


	nFind1	= strReceived.Find("+CMGS");
	nFind2	= strReceived.Find("ERROR");
	if((nFind1 != -1) && (nFind2 != -1)) {
//		AfxMessageBox("短信发送失败!");
	}
}

CString CProcSM::GetNewSMData(CString strReceived,CString &sMessage)
{
	if(strReceived.IsEmpty())	return "";

	CString		SM_Num(""),Data;
	int 		nFind1, nFind2, i;
	char		chr;

	nFind1	= strReceived.Find("AT+CMGR=");
	if(nFind1 != -1) {
		for(i = nFind1 + 8; i < strReceived.GetLength(); i++) {
			chr = strReceived.GetAt(i);
			if(isalnum(chr))	SM_Num += chr;
			else				break;
			if((chr == '\r') || (chr == '\n'))	break;
		}
	}

	nFind1	= strReceived.Find("READ");
	nFind2	= strReceived.Find("OK");
	if((nFind1 != -1) && (nFind2 != -1)) {
		sMessage = GetSpecialStr(strReceived,nFind1 + 6,nFind2);
	}

	return SM_Num;
}

void CProcSM::ProcShortMessage(CString sMessage)
{
	CVehicleMessageSet		messageSet;
	CClientinfoSet			clientinfoSet;
	CMainFrame				*pMainFrame = NULL;
	CString					sTel,sDate,sUnico,sAnsi;
	CString					sID,sState,sPos,sGx,sGy,sSpeed,sDriver;
	int						nFind1,nFind2,Len;

	sTel.Empty();	sDate.Empty();	sUnico.Empty();	sAnsi.Empty();	sID.Empty();
	sState.Empty();	sPos.Empty();	sGx.Empty();	sGy.Empty();	sSpeed.Empty();	sDriver.Empty();

	sMessage.Replace("\r","");
	sMessage.Replace("\n","");
	sMessage.Replace(" ","");

//	WriteToFile("Message.txt",sMessage);

	Len		= sMessage.GetLength();

	nFind1	= sMessage.Find("\"");
	nFind2	= sMessage.Find("\"",nFind1 + 1);
	sTel	= GetSpecialStr(sMessage,nFind1 + 1, nFind2);
	sTel	= sTel.Right(11);

	nFind1	= sMessage.Find("\"",nFind2 + 1);
	nFind2	= sMessage.Find("\"",nFind1 + 1);
	sDate	= GetSpecialStr(sMessage,nFind1 + 1, nFind2);

	sUnico	= GetSpecialStr(sMessage,nFind2 + 1,Len);

	sAnsi	= UnicodeStr2AnsiStr(sUnico);

	if(sAnsi.Find("指令已经执行") != -1) {				//判断是否自动监控
		AfxMessageBox("自动监控指令已经执行!");
		return;
	}
	if(sAnsi.Find("密码错误") != -1) {
		return;
	}

	nFind1	= sAnsi.Find('\r');
	if(nFind1 != -1) {
		sID		= GetSpecialStr(sAnsi,0,nFind1);
		sAnsi	= sAnsi.Mid(nFind1 + 1);
	}
	nFind1	= sID.Find(":");
	if(nFind1 != -1)	sID	= sID.Mid(nFind1 + 1);

	nFind1	= sAnsi.Find('\r');
	if(nFind1 != -1) {
		sState	= GetSpecialStr(sAnsi,0,nFind1);
		sAnsi	= sAnsi.Mid(nFind1 + 1);
	}
	nFind1	= sState.Find(":");
	if(nFind1 != -1)	sState = sState.Mid(nFind1 + 1);

	nFind1	= sAnsi.Find('\r');
	if(nFind1 != -1) {
		sPos	= GetSpecialStr(sAnsi,0,nFind1);
		sAnsi	= sAnsi.Mid(nFind1 + 1);
	}
	nFind1	= sPos.Find(":");
	if(nFind1 != -1) sPos = sPos.Mid(nFind1 + 1);

	nFind1	= sAnsi.Find('\r');
	if(nFind1 != -1) {
		sGx		= GetSpecialStr(sAnsi,0,nFind1);
		sAnsi	= sAnsi.Mid(nFind1 + 1);
	}
	nFind1	= sGx.Find(":");
	if(nFind1 != -1) sGx = sGx.Mid(nFind1 + 1);

	nFind1	= sAnsi.Find('\r');
	if(nFind1 != -1) {
		sGy		= GetSpecialStr(sAnsi,0,nFind1);
		sAnsi	= sAnsi.Mid(nFind1 + 1);
	}
	nFind1	= sGy.Find(":");
	if(nFind1 != -1) sGy = sGy.Mid(nFind1 + 1);

//	nFind1	= sAnsi.Find('\r');
//	if(nFind1 != -1) {
//		sSpeed	= GetSpecialStr(sAnsi,0,nFind1);
//	}
	sSpeed	= sAnsi;
	nFind1	= sSpeed.Find(":");
	if(nFind1 != -1) sSpeed = sSpeed.Mid(nFind1 + 1);

	if(sGx.IsEmpty() || sGy.IsEmpty())	return ;

	clientinfoSet.FindClientinfo(sTel,sDriver,sID);

	ChangLGAndLt(sGx,sGy,sID,sTel);

	messageSet.AddMessage(sTel,sDate,sState,sPos,sGx,sGy,sSpeed);

	pMainFrame = (CMainFrame*)AfxGetApp()->m_pMainWnd;
	if(pMainFrame != NULL) {
		pMainFrame->Insert2ListCtrl(sID,sDriver,sDate,sSpeed,sGx,sGy,sState,sTel,sPos);
	}
}

CString CProcSM::NewSMIndication(CString strReceived)
{
	if(strReceived.IsEmpty())	return "";

	int 		nFind1, nFind2, i;

	nFind1	= strReceived.Find("CMTI");
	nFind2	= strReceived.Find("SM");

	if((nFind1 != -1) && (nFind2 != -1)) {
		strReceived	= strReceived.Mid(nFind2 + 4);
		CString			Str("");
		char			chr;

		for(i = 0; i < strReceived.GetLength(); i++) {
			chr = strReceived.GetAt(i);
			if(isalnum(chr))	Str += chr;
			else				break;
			if((chr == '\r') || (chr == '\n'))	break;
		}

		return Str;
	}

	return "";
}

CString CProcSM::GetLenStr(CString DstStr,BYTE pos,BYTE	Len)
{
	CString		RetStr("");
	BYTE		i;

	for(i = pos; i < pos + Len; i++) {
		RetStr += DstStr.GetAt(i);
	}

	return RetStr;
}

CString CProcSM::GetSpecialStr(CString sSrcStr,long pos1,long pos2)
{
	CString		RetStr("");
	long		i;

	for(i = pos1; i < pos2; i++) {
		RetStr += sSrcStr.GetAt(i);
	}

	return RetStr; 
}

/**********************************************/
/*  read the serial port ,the function
/*  return the numbers of the date
/***********************************************/
long CProcSM::ReadComPort(WPARAM wParam,CGpsSCDoc *pDoc)
{
	if(!pDoc->m_bConnected || (wParam&EV_RXCHAR) != EV_RXCHAR) {
		SetEvent(pDoc->m_hPostMsgEvent);
		return 0L;
	}

	CString 		strMessage;	
	char			buff[512];
	long			nLength;

	nLength = pDoc->ReadComm(buff,512);	

	buff[nLength] = 0;
	strMessage = buff;

//	WriteToFile("ReadMsg.txt",strMessage);

	if(nLength != 0) {
		gStringList.AddTail(strMessage);
	}

	SetEvent(pDoc->m_hPostMsgEvent); //允许发送下个 WM_COOMMNOTIFY 消息

	return 0L;
}

/****************************************/
/*    write date to the serial port
/****************************************/
long CProcSM::WriteComPort(CString str2Write,CGpsSCDoc *pDoc)
{
	if(str2Write.IsEmpty())		return 0;

	if(!(pDoc->m_bConnected)) {
		AfxMessageBox("串行口没有成功打开,请检查串行口!");
		return 0;
	}

//	WriteToFile("WriteMsg.txt",str2Write);

	COMSTAT			ComStat;
	DWORD			dwErrorFlags;
	BYTE			Buf[512];
	BOOL			fState;
	DWORD			length = 0,i;


	length = str2Write.GetLength();
	if(length > 512)	length = 512;
	for(i = 0; i < length; i++)	Buf[i] = str2Write.GetAt(i);
	Buf[i] = 0;

	ClearCommError(pDoc->m_hCom,&dwErrorFlags,&ComStat);
	fState = WriteFile(pDoc->m_hCom,Buf,length,&length,&(pDoc->m_osWrite));
	
	if(!fState)	{
		if(GetLastError()==ERROR_IO_PENDING) {
			GetOverlappedResult(pDoc->m_hCom,&(pDoc->m_osWrite),&length,TRUE); 
		}		
		else {
			length = 0;
		}
	}

	return length;
}

/*************************************************/
//function to deal systemstr
/*************************************************/
void CProcSM::DealSystemMsg(CString strReceived)
{
//	WriteToFile("System.txt",strReceived);

	int		Ask;

	strReceived.Replace("\r","");
	strReceived.Replace("\n","");
	strReceived.Replace(" ","");

	if(strReceived.Find(WAVECOMINITETEXTSUCCESS) != -1) {			//Text方式
	  AfxMessageBox("监控中心电话初始化成功!");
	}
	else if (strReceived.Find(WAVECOMINITETEXTFAILE) != -1)	{		//Text方式
		Ask = AfxMessageBox("监控中心电话初始化失败,\n是否重新初始化监控中心电话?",MB_YESNO);
		if(Ask == IDYES) SetStr2Send(WAVECOMINITETEXT);
	}
	else if (strReceived .Find(WAVECOMINITEPDUSUCCESS) !=-1) {		//PDU方式
		AfxMessageBox("监控中心电话初始化成功!");
	}
    else if (strReceived.Find(WAVECOMINITEPDUFAILE) != -1) {		//PDU方式
		Ask = AfxMessageBox("监控中心电话初始化失败,\n是否重新初始化监控中心电话?",MB_YESNO);
		if(Ask == IDYES) SetStr2Send(WAVECOMINITEPDU);
	}
}

//get gprmc data
BOOL CProcSM::GetSubData(char * ReceData,CString *strLongitude,CString *strLatitude)
{
	CString		TmpStr1,TmpStr2;
	int			DataPos = 0;
	INT			strlength = strlen(ReceData);

	if (strlength >= 60)
	{
		TmpStr1 = GetString(ReceData,&DataPos);	//用于去掉 "$GPRMC,"等字符
	}	
	else
	{
		return false;
	}

	if(TmpStr1 == "$GPRMC")
	{		
		TmpStr1 = GetString(ReceData,&DataPos);	//获取GPS接收时间
		
		if(!TmpStr1.IsEmpty())
		{
			;		
		}
		else 

⌨️ 快捷键说明

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