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

📄 mobmsgdlg.cpp

📁 一个完整的短讯网关示例,支持短讯收发,支持UDP及Text格式.
💻 CPP
📖 第 1 页 / 共 5 页
字号:

			
				if (strlen(MsgInfo)<1)      //|| MsgMobNo.Length()<11
					continue;

				CString sSendMsg = "";
				sSendMsg += "(";
				sSendMsg += MsgMobNo;
				sSendMsg += ")";
				sSendMsg += MsgInfo;
				SendSystemInfo(sSendMsg,1);

				memset(MobNo,0,20);
				memcpy(MobNo,MsgMobNo,strlen(MsgMobNo));
				//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@一条短信息分析完毕
				

				//////###################################################################
				////以下代码为根据所接收的短信息进行再次分析,完成以下四个工作:
				////1、用户短信息于机顶盒上显示;(接收到字符"M" or "m")
				////2、用户要求绑定电话			 (接收到字符"R" or "r")
				////3、用户点播节目     短信息中只有节目编号
				////4、用户要求取消绑定电话		 (接收到字符"C" or "c")
				////5、用户确认点播				 (接收到字符"D" or "d")
				////说明:如果接收到的短信息长度>1时表示为1或2或3,因为执行1或2或3时应加入卡号或节目编号
				////否则就存在两种情况:
				////			a.如果内容是1-9之间的数则默认为节目编号,则执行3(用户点播节目);
				////			b.否则为4或5,
				/////###################################################################
				CString sTmpMesssage = MsgInfo;
				CString sTempString = sTmpMesssage.Mid(0,1);
				if ((sTempString == "S") ||(sTempString == "s")) //用户短信息于机顶盒上显示
				{
						if (strlen(MsgInfo)<13)
							SendMsgInfo(StrSmsCenterCode,MsgMobNo,"显示短讯的内容不可为空!");
						else
						{
							memset(sSendToReceiveMsg,0,100);
							memcpy(sSendToReceiveMsg,MsgInfo + 1 + 11 + 1,strlen(MsgInfo) - 1 - 11 - 1);
							sTempString = sTmpMesssage.Mid(11,1);
							if (sTempString == "@")
								SendMsgInfo(StrSmsCenterCode,MsgMobNo,"显示短讯格式为:M+IC卡号+@+显示的信息内容!");
							else
							{
								if (strlen(sSendToReceiveMsg)>110)//所要显示的短信息长度不可超过120个汉字
									SendMsgInfo(StrSmsCenterCode,MsgMobNo,"用于显示的短信息不可超过55个汉字!");
								else
									SendShortMsgToReceive();
							}	
						}
				}
				else if ((sTempString == "R") ||(sTempString == "r")) //如果内容为用户请求绑定
						UserRequestService();
 				else if ((sTempString == "C") ||(sTempString == "c")) //内容为用户取消绑定
						UserCancleService(); 
				else if ((sTempString == "A") ||(sTempString == "a")) //用户确认点播	
						UserTvodMovieValidate();
				else if ((sTempString == "X") ||(sTempString == "x")) //用户取消点播节目	
						UserRemoveTvodMovie();
				else if ((sTempString == "M") ||(sTempString == "m")) //用户按节目名称点播节目
						UserTvodMovieName();
				else 
				{
					char IsNUmber[1];
					memset(IsNUmber,0,1);
					memcpy(IsNUmber,sTmpMesssage,1);
					int bcur =IsNUmber[0];
					if (bcur >48  && bcur <= 57)//@@@@@@@@@@@@@@@@@@@@@@@用户按节目编号点播节目  介于0--9之间的数
						UserTvodMovieNo();
					else
						SendMsgInfo(StrSmsCenterCode,MsgMobNo,"无法识别的短信或短信内容过长(显示的信息内容不可超过55个汉字)!");//不可识别的短信
				}

				////////////////////////////////////
				/////////以下为删除当前所处理的短讯/////
				///////////////////////////////////
				//////////@@@@@@@@@@@@@判断一下是否还存在短信息
				pdest = strstr(cLoopSMSMsg, "+CMGL:");
				nPos = pdest - cLoopSMSMsg;
				if (nPos >= 0)
				{
					itmpisFound = 1;
					memset(TmpSmsCenter,0,8192);
					memcpy(TmpSmsCenter,cLoopSMSMsg,strlen(cLoopSMSMsg));
				}
				else
					itmpisFound = 0;
				char cTmp[10];
				memset(cTmp,0,10);
				itoa(iSmsPosIndex,cTmp,10);
				memset(StructionString,0,1024);
				memcpy(StructionString,"at+cmgd=",8);
				memcpy(StructionString + strlen(StructionString),cTmp,strlen(cTmp));
				memcpy(StructionString + strlen(StructionString),"\r",2);

				::Sleep(5000);
				int deleflag = WriteFile(Mobile, StructionString, strlen(StructionString), &num, NULL);
				
				if (deleflag) 
					SendSystemInfo("短信删除成功!",1);
				
			}
		}
		catch(...)
		{
			SendSystemInfo("读到错误短信息!",3);
			char cTmp[10];
			memset(cTmp,0,10);
			itoa(iSmsPosIndex,cTmp,10);
			memset(StructionString,0,1024);
			memcpy(StructionString,"at+cmgd=",8);
			memcpy(StructionString + strlen(StructionString),cTmp,strlen(cTmp));
			memcpy(StructionString + strlen(StructionString),"\r",2);

			::Sleep(2000);
			int deleflag = WriteFile(Mobile, StructionString, strlen(StructionString), &num, NULL);
			
			if (deleflag) 
				SendSystemInfo("短信删除成功!",1);
			SendMsgInfo(StrSmsCenterCode,MsgMobNo,"无法识别的短信或短信内容过长(显示的信息内容不可超过55个汉字)!");//不可识别的短信
			//*/
			continue;
		}
	}
	return 1;
}

//---------------------------------------------------------------------------

void CMobMsgDlg::OnStart() 
{
//////////Clear CListBox 
	while (Lst->GetCount() > 0)
			Lst->DeleteString(0);

	iTryConnCount = 0;
	::Sleep(1000);
	if (Mobile != NULL)
		CloseHandle(Mobile);
	if (!MobileConnect())
	{
		Mobile = NULL;
		return;
	}
	SendSystemInfo("------------------------------------------------------------------------------\n",3);
	//////////////////////////////////////读取手机厂商信息
	ReadMobilePriver:	
	char Miinstruction[1000];
	memset(Miinstruction,0,1000);
	memcpy(Miinstruction,"at+cgmi\r",9);
	Sleep(1000);
	///取出手机供应商信息 西门子
	boolTmp = WriteFile(Mobile, Miinstruction, 9 , &num, NULL);
	if (boolTmp == 0)
	{
		SendSystemInfo("手机连接失败!",3);//传入参数为3说明当前为刚启动但还未完全启动服务
		return;
	}
	Sleep(1000);
	char *MobilePriver;
	MobilePriver = ReceiveMobileModelPrevie();
	char TmpPriver[1024];
	int pos;
	if (MobilePriver == NULL )
	{
		iTryConnCount ++;
		if (iTryConnCount > 1)
		{
			SendSystemInfo("手机连接失败!",3);
			return;
		}
		else
			goto ReadMobilePriver;
	}
	MobilePriver = strstr(MobilePriver,"\r\r\n");
	if (MobilePriver == NULL)
	{
		iTryConnCount ++ ;
		if (iTryConnCount > 1)
		{
			SendSystemInfo("手机连接失败!",3);
			return;
		}
		else
			goto ReadMobilePriver;
	}
	pos=strcspn(MobilePriver, "OK" );
	
	memset(TmpPriver,0,1024);
	memcpy(TmpPriver,MobilePriver,pos);//TmpPriver取得了手机供应商信息

	//以下为取得手机型号信息
	ReadMobileModel:
	char *MobileMode;
	memset(Miinstruction,0,1000);
	memcpy(Miinstruction,"at+cgmm\r",9);
	Sleep(1000);
	///取出手机型号信息  s3815i
	boolTmp = WriteFile(Mobile, Miinstruction, 9 , &num, NULL);
	if (!boolTmp)
	{
		iTryConnCount ++ ;
		if (iTryConnCount > 1)
		{
			SendSystemInfo("手机连接失败!",3);
			return;
		}
		else
			goto ReadMobileModel;
	}
	Sleep(100);
	MobileMode = ReceiveMobileModelPrevie();// "at+cgmi\r\r\nSIEMENS\r\n\r\nOK\r\n";
	char TmpMode[1024];
	if (MobileMode == NULL)
	{
		iTryConnCount ++ ;
		if (iTryConnCount > 1)
		{
			SendSystemInfo("手机连接失败!",3);
			return;
		}
		else
			goto ReadMobileModel;
	}
	MobileMode = strstr(MobileMode,"\r\r\n");
	if (MobileMode == NULL)
	{
		iTryConnCount ++ ;
		if (iTryConnCount > 1)
		{
			SendSystemInfo("手机连接失败!",3);
			return;
		}
		else
			goto ReadMobileModel;
	}
	pos = strcspn(MobileMode, "OK" );
	
	memset(TmpMode,0,1024);
	memcpy(TmpMode,MobileMode,pos);//TmpPriver取得了手机供应商信息*/
	
	char TempString[256];
	memset(TempString,0,256);
	memcpy(TempString,TmpPriver,strlen(TmpPriver));
	memcpy(TempString + strlen(TempString),"--",2);
	memcpy(TempString + strlen(TempString),TmpMode,strlen(TmpMode));

	BtnStop->EnableWindow(true);
	BtnExit->EnableWindow(false);
	BtnStart->EnableWindow(false);
	Lst->InsertString(0,TempString);
	SendSystemInfo("手机连接成功!",1);
	SendSystemInfo("------------------------------------------------------------------------------\n",3);
	//开始线程
	iThreadFlag = 0;
	iTryConnFlag = 1;
	hReadMobileInfo = ::CreateThread(NULL, 0, tReadMobileInfo, NULL, 0, NULL);
	//event=CreateEvent(NULL, TRUE, FALSE, NULL);
	//SetEvent(event);
}

void CMobMsgDlg::OnStop() 
{
	iThreadFlag = 1;
	//SuspendThread(hReadMobileInfo);  //挂起线程
	BtnStart->EnableWindow(true);
	BtnExit->EnableWindow(true);
	BtnStop->EnableWindow(false);
	SendSystemInfo("服务停止!",0);	
}

void CMobMsgDlg::OnCExit() 
{
	Mobile = NULL;
	exit(0);	
}

bool CMobMsgDlg::MobileConnect()
{
	char IniFile[1000];	
	memset(IniFile,0,1000);
	GetCurrentDirectory(1000,IniFile);
	strcat(IniFile,"\\ShortMsg.ini");
	int nFileIndex = open(IniFile,_O_RDONLY);
	if (nFileIndex > 0)
	{
		memset(StrPort,0,20);
		memset(StrBaudrates,0,20);
		memset(StrStopBit,0,20);
		memset(StrParity,0,20);
		memset(StrByteSize,0,20);
		memset(StrSmsCenterCode,0,20);
		memset(ServerIP,0,20);
		memset(ServerPort,0,20);
		memset(MobilChannel,0,20);
		
		GetPrivateProfileString("ServerInfo","ServerIp","127.0.0.1",ServerIP,20,IniFile);
		if (sizeof(ServerIP) < 1)
			WritePrivateProfileString("ServerInfo","ServerIp","127.0.0.1",IniFile);

		GetPrivateProfileString("ServerInfo","ServerPort","6876",ServerPort,20,IniFile);
		if (sizeof(ServerPort) < 1)
			WritePrivateProfileString("ServerInfo","ServerPort","6876",IniFile);
		iServerPort =atoi(ServerPort);

		GetPrivateProfileString("MobilChannel","MobilChannel","111",MobilChannel,20,IniFile);
		if (sizeof(MobilChannel) < 1)
			WritePrivateProfileString("MobilChannel","MobilChannel","111",IniFile);


		GetPrivateProfileString("ShortMsg","Port","COM1",StrPort,20,IniFile);
		if (sizeof(StrPort) < 1)
			WritePrivateProfileString("ShortMsg","Port","COM1",IniFile);

		GetPrivateProfileString("ShortMsg","BAUDRATE","19200",StrBaudrates,20,IniFile);
		if (sizeof(StrBaudrates) < 1)
			WritePrivateProfileString("ShortMsg","BAUDRATE","19200",IniFile);

		GetPrivateProfileString("ShortMsg","STOPBITS","2",StrStopBit,20,IniFile);
		if (sizeof(StrStopBit) < 2)
			WritePrivateProfileString("ShortMsg","STOPBITS","2",IniFile);

		GetPrivateProfileString("ShortMsg","PARITY","0",StrParity,20,IniFile);
		if (sizeof(StrParity) < 1)
			WritePrivateProfileString("ShortMsg","PARITY","0",IniFile);

		GetPrivateProfileString("ShortMsg","BYTESIZE","8",StrByteSize,20,IniFile);
		if (sizeof(StrByteSize) < 1)
			WritePrivateProfileString("ShortMsg","BYTESIZE","8",IniFile);

		GetPrivateProfileString("ShortMsg","SmsCenterCode","8613800755500",StrSmsCenterCode,20,IniFile);
		if (sizeof(StrSmsCenterCode) < 1)
			WritePrivateProfileString("ShortMsg","SmsCenterCode","8613800755500",IniFile);
		close(nFileIndex);
	}
	else
	{
		WritePrivateProfileString("ServerInfo","ServerIP","127.0.0.1",IniFile);
		WritePrivateProfileString("ServerInfo","ServerPort","6876",IniFile);
		WritePrivateProfileString("MobChannel","MobChannel","111",IniFile);
		WritePrivateProfileString("ShortMsg","Port","COM1",IniFile);
		WritePrivateProfileString("ShortMsg","BAUDRATE","19200",IniFile);
		WritePrivateProfileString("ShortMsg","STOPBITS","2",IniFile);
		WritePrivateProfileString("ShortMsg","PARITY","0",IniFile);
		WritePrivateProfileString("ShortMsg","BYTESIZE","8",IniFile);
		WritePrivateProfileString("ShortMsg","SmsCenterCode","8613800755500",IniFile);
	}
	_fmode = _O_BINARY;
	//////////////////开始打开串口通讯
	DCB lpDCB;
    Mobile = NULL;
	Mobile = CreateFile(StrPort,GENERIC_READ |GENERIC_WRITE,0,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);
	if (Mobile == INVALID_HANDLE_VALUE)
	{
		SendSystemInfo("手机连接失败,请检查您的COM端口!",3);
		return false;	
	}

	//PurgeComm(m_hComm, PURGE_RXCLEAR | PURGE_TXCLEAR | PURGE_RXABORT | PURGE_TXABORT); file://清缓冲方法
	/////以下为初始化串口  //设置串口参数
	SetupComm(Mobile,4096,1024);
	//SetupComm(Mobile,10000,10000);
	GetCommState(Mobile, &lpDCB);

	DWORD StopBit,ByteSize,Parity,Baudrates;
	StopBit = atoi(StrStopBit);
	ByteSize = atoi(StrByteSize);
	Parity = atoi(StrParity);
	Baudrates = atoi(StrBaudrates);

	lpDCB.StopBits = atoi(StrStopBit);
    lpDCB.ByteSize = atoi(StrByteSize);
    lpDCB.Parity = atoi(StrParity);
    lpDCB.BaudRate = atoi(StrBaudrates);
    SetCommState(Mobile, &lpDCB);

		//设置超时参数
	COMMTIMEOUTS commTimeOuts;
	commTimeOuts.ReadIntervalTimeout = MAXDWORD;
	commTimeOuts.ReadTotalTimeoutConstant = 0;
	commTimeOuts.ReadTotalTimeoutMultiplier = 0;
	commTimeOuts.WriteTotalTimeoutConstant = 1000;
	commTimeOuts.WriteTotalTimeoutMultiplier = 0;
	SetCommTimeouts(Mobile, &commTimeOuts);
	return true;
}
//---------------------------------------------------------

void SendSystemInfo(CString TmpStr, int Flag)
{
	/*if (Flag==1)//表示手机连接成功
	{
		
		iTryConnFlag =1;
		SuspendThread(hTryConnectMob);  //挂起手机连接线程
		if (iThreadFlag == 1)
		{
			SuspendThread(hReadMobileInfo);  //挂起读线程
			hReadMobileInfo =::CreateThread(NULL, 0, tReadMobileInfo, NULL, 0, NULL);
		}
		iThreadFlag = 0 ; //如果手机连接成功的话则充许读手机信息
	}
	else if (Flag==0)//表示手机连接成功
	{
		iTryConnFlag =0;
		SuspendThread(hReadMobileInfo);  //挂起读线程
		if (iThreadFlag==1) //如果服务是正常启动时而连接不上的话就不需要再次尝试手机连接
			hTryConnectMob =::CreateThread(NULL, 0, tTryConnectMob, NULL, 0, NULL);
		iThreadFlag = 1 ;//如果手机连接失败的话则中止读手机信息
	}//*/
	CTime t = CTime::GetCurrentTime();
	szTemp.Format("%02d:%02d:%02d",t.GetHour(),t.GetMinute(),t.GetSecond());
	TmpStr = TmpStr + "("+ szTemp + ")" ;
	Lst->InsertString(0,TmpStr);
}

⌨️ 快捷键说明

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