www.pudn.com > gprs_sms.zip > Sms.cpp


/* ************************************************************************ 
	 *	 CSms.cpp:  Impliment file  of cmd Socket receive server  * 
	 *	 Version :  0.07                                          * 
	 *	 Date    :  30th,Nov 2002                                 *  
	 *	 Author  :  Caihy                           		  * 
	 *	 Email   :  cai_hai_ying2002@163.com                      * 
 *************************************************************************/ 
 
#include "stdafx.h" 
#include "Sms.h" 
#include "math.h" 
#include "SmsDlg.h" 
 
#ifdef _DEBUG 
#define new DEBUG_NEW 
#undef THIS_FILE 
static char THIS_FILE[] = __FILE__; 
#endif 
 
// 
//	Note! 
// 
//		If this DLL is dynamically linked against the MFC 
//		DLLs, any functions exported from this DLL which 
//		call into MFC must have the AFX_MANAGE_STATE macro 
//		added at the very beginning of the function. 
// 
//		For example: 
// 
//		extern "C" BOOL PASCAL EXPORT ExportedFunction() 
//		{ 
//			AFX_MANAGE_STATE(AfxGetStaticModuleState()); 
//			// normal function body here 
//		} 
// 
//		It is very important that this macro appear in each 
//		function, prior to any calls into MFC.  This means that 
//		it must appear as the first statement within the  
//		function, even before any object variable declarations 
//		as their constructors may generate calls into the MFC 
//		DLL. 
// 
//		Please see MFC Technical Notes 33 and 58 for additional 
//		details. 
// 
 
///////////////////////////////////////////////////////////////////////////// 
// CSmsApp 
 
BEGIN_MESSAGE_MAP(CSmsApp, CWinApp) 
	//{{AFX_MSG_MAP(CSmsApp) 
		// NOTE - the ClassWizard will add and remove mapping macros here. 
		//    DO NOT EDIT what you see in these blocks of generated code! 
	//}}AFX_MSG_MAP 
END_MESSAGE_MAP() 
 
///////////////////////////////////////////////////////////////////////////// 
// CSmsApp construction 
 
CSmsApp::CSmsApp() 
{ 
 
} 
 
///////////////////////////////////////////////////////////////////////////// 
// The one and only CSmsApp object 
 
CSmsApp theApp; 
char* m_Message;      //Message 
char* m_Number;      //the called number  
int ComNum;           //port number, now not used 2002-11-25 
 
 
extern "C" __declspec(dllexport) int GetStatus() 
{ 
    AFX_MANAGE_STATE(AfxGetStaticModuleState()); 
 return 0;  //ready 
} 
 
extern "C" __declspec(dllexport) int ReadSms() 
{ 
    AFX_MANAGE_STATE(AfxGetStaticModuleState()); 
//varialbe declaration 
	CSmsDlg dlg;       //declare an dlg instance 
	int i,j,k,InBufferCount,nowInBufferCount; //receive buffer variables 
    VARIANT vResponse; 
	char *str; 
	BYTE Data[400];//设置BYTE数组 An 8-bit integer that is not signed. 
 
	dlg.Create(IDD_MODEMCONNET_DIALOG,NULL); 
 	dlg.m_Com.SetCommPort(4); //选择COM1 
	dlg.m_Com.SetInBufferSize(1024); //设置输入缓冲区的大小,Bytes 
	dlg.m_Com.SetOutBufferSize(512); //设置输入缓冲区的大小,Bytes 
  
	if(dlg.m_Com.GetPortOpen()) //打开串口   
	{ 
	//	if(dlg) dlg.DestroyWindow(); can't in for dlg is only -- 2002-11-29 
		return 1;//no use 
	} 
	else 
	{ 
		try 
		{dlg.m_Com.SetPortOpen(TRUE);} 
		catch(...)  // catch an exception 如果其它进程独占端口,则打开端口失败,返回busy 
		{ 
			if(dlg) dlg.DestroyWindow(); 
			return 1; 
		} 
		//if(m_Com.GetPortOpen())	MessageBox("端口打开!"); 
	} 
	dlg.m_Com.SetInputMode(1); //设置输入方式为二进制方式,default is 0 文本方式接收数据, more trouble 
	dlg.m_Com.SetRThreshold(1); //为1表示有一个字符引发一个事件     
	dlg.m_Com.SetSettings("57600,n,8,1"); 
	//m_Com.SetHandshaking(0); 
	dlg.m_Com.SetInputLen(0);//接收缓冲区的全部内容 
//	m_Com.SetOutput(COleVariant(m_strArray));//发送数据 
	dlg.m_Com.GetInput();   	//先预读缓冲区以清除残留数据 
 
   	dlg.SendString("AT\r\n");    //测试连接是否成功 
   	dlg.SendString("ATE0\r\n");  //设置modem无回显,即返回数据中不在包括发送指令 
  	dlg.SendString("AT\r\n");    //测试连接是否成功 
   	dlg.SendString("AT+CMGF=0\r\n");    //测试连接是否成功 
	Sleep(500); 
	dlg.m_Com.GetInput(); 
 
	dlg.SendString("AT+CMGR=7\r\n"); 
 
// query inbuffer data 
	for(i=1;i<2;i++) 
	{ 
		Sleep(1000); 
		InBufferCount = dlg.m_Com.GetInBufferCount(); //接收到的字符数目 
		vResponse = dlg.m_Com.GetInput();   //读缓冲区 
		nowInBufferCount=dlg.m_Com.GetInBufferCount(); 
		str=(char*)(unsigned char*)vResponse.parray->pvData; 
		for(k=0;k<=InBufferCount;k++) Data[k] = *(str+k); 
		if(Data[10] != '+') 
		{ 
			if(dlg.m_Com.GetPortOpen()) dlg.m_Com.SetPortOpen(FALSE); 
			if(dlg) dlg.DestroyWindow(); 
			return -1; //if not 'ok', return 1 busy 
		} 
	} 
 
	return 100; 
} 
 
extern "C" __declspec(dllexport) int SendShortMessage(char *Number, int NumberLen, char *Message, int MessageLen) 
{ 
    AFX_MANAGE_STATE(AfxGetStaticModuleState()); 
// variable definition 
	CString strMessage;  //send string 
	CString strNumber;  //send string 
	CSmsDlg dlg;       //declare an dlg instance 
	 
	int i,j,k,InBufferCount,nowInBufferCount; //receive buffer variables 
    VARIANT vResponse; 
	char *str; 
	BYTE Data[200];//设置BYTE数组 An 8-bit integer that is not signed. 
 
	m_Message = Message; 
	m_Number = Number; 
	strMessage=CString(Message); 
	strNumber=CString(Number); 
 
	int m_NumberLen = strlen(Number); 
	int m_MessageLen = strlen(Message); 
 
	if(m_NumberLen!=11) return 3;  // illegal number 
 
	CString strTemp;	 
 
 
// AT+CMGF=1  Test Mode  
//	dlg.SendString("AT+CMGF=1"); 
//	strTemp = "AT+CMGS=""+strMessage+""\r\n";  
// AT+CMGF=0  PDU  mode 
//	dlg.SendString("AT\r\n"); 
//	dlg.SendString("AT+CMGF=0\r\n"); 
 
	dlg.Create(IDD_MODEMCONNET_DIALOG,NULL); 
 	dlg.m_Com.SetCommPort(4); //选择COM1 
	dlg.m_Com.SetInBufferSize(1024); //设置输入缓冲区的大小,Bytes 
	dlg.m_Com.SetOutBufferSize(512); //设置输入缓冲区的大小,Bytes 
  
	if(dlg.m_Com.GetPortOpen()) //打开串口   
	{ 
	//	if(dlg) dlg.DestroyWindow(); can't in for dlg is only -- 2002-11-29 
		return 1;//no use 
	} 
	else 
	{ 
		try 
		{dlg.m_Com.SetPortOpen(TRUE);} 
		catch(...)  // catch an exception 如果其它进程独占端口,则打开端口失败,返回busy 
		{ 
			if(dlg) dlg.DestroyWindow(); 
			return 1; 
		} 
		//if(m_Com.GetPortOpen())	MessageBox("端口打开!"); 
	} 
	dlg.m_Com.SetInputMode(1); //设置输入方式为二进制方式,default is 0 文本方式接收数据, more trouble 
	dlg.m_Com.SetRThreshold(1); //为1表示有一个字符引发一个事件     
	dlg.m_Com.SetSettings("57600,n,8,1"); 
	//m_Com.SetHandshaking(0); 
	dlg.m_Com.SetInputLen(0);//接收缓冲区的全部内容 
//	m_Com.SetOutput(COleVariant(m_strArray));//发送数据 
	dlg.m_Com.GetInput();   	//先预读缓冲区以清除残留数据 
 
   	dlg.SendString("AT\r\n");    //测试连接是否成功 
   	dlg.SendString("ATE0\r\n");  //设置modem无回显,即返回数据中不在包括发送指令 
  	dlg.SendString("AT\r\n");    //测试连接是否成功 
   	dlg.SendString("AT+CMGF=0\r\n");    //测试连接是否成功 
	Sleep(500); 
	dlg.m_Com.GetInput(); 
 
	dlg.SendString("AT+CSCA?\r\n"); 
 
// query inbuffer data 
	for(i=1;i<2;i++) 
	{ 
		Sleep(1000); 
		InBufferCount = dlg.m_Com.GetInBufferCount(); //接收到的字符数目 
		vResponse = dlg.m_Com.GetInput();   //读缓冲区 
		nowInBufferCount=dlg.m_Com.GetInBufferCount(); 
		str=(char*)(unsigned char*)vResponse.parray->pvData; 
		for(k=0;k<=InBufferCount;k++) Data[k] = *(str+k); 
		if(Data[10] != '+') 
		{ 
			if(dlg.m_Com.GetPortOpen()) dlg.m_Com.SetPortOpen(FALSE); 
			if(dlg) dlg.DestroyWindow(); 
			return -1; //if not 'ok', return 1 busy 
		} 
		 
		else  
		{ 
		 
			dlg.SCA.Empty(); 
			char MidSCA[13]; 
			for(i=0;i<9;i=i+2) 
			{ 
				MidSCA[i] = Data[i+14]; 
				MidSCA[i+1] = Data[i+13]; 
			} 
			MidSCA[11] = Data[23]; 
			MidSCA[10] = 'F'; 
			for(i=0;i<12;i++) dlg.SCA=dlg.SCA+CString(MidSCA[i]); 
			continue; 
		} 
	} 
// if message too long, then close the port and dlg, and return 2 
	int len = dlg.SmsConvertPDU(); 
	if(len>=140)  
	{ 
		if(dlg.m_Com.GetPortOpen()) dlg.m_Com.SetPortOpen(FALSE); 
		if(dlg) dlg.DestroyWindow(); 
		return 2;// message too long 
	} 
 
   	dlg.SendString("AT\r\n");    //测试连接是否成功 
// query inbuffer data 
	for(i=1;i<2;i++) 
	{ 
		Sleep(500); 
		InBufferCount = dlg.m_Com.GetInBufferCount(); //接收到的字符数目 
		vResponse = dlg.m_Com.GetInput();   //读缓冲区 
		nowInBufferCount=dlg.m_Com.GetInBufferCount(); 
		str=(char*)(unsigned char*)vResponse.parray->pvData; 
		for(k=0;k<=InBufferCount;k++) Data[k] = *(str+k); 
		if(Data[2] != 'o' && Data[2] != 'O') 
		{ 
			if(dlg.m_Com.GetPortOpen()) dlg.m_Com.SetPortOpen(FALSE); 
			if(dlg) dlg.DestroyWindow(); 
			return -1; //if not 'ok', return 1 busy 
		} 
		 
		else continue; 
	} 
	 
// prepare for sms 
	CString TotalLength; 
	TotalLength.Format("%d",len); 
	strTemp = "AT+CMGS="+TotalLength+"\r\n";  
	dlg.SendString(strTemp); 
 
// query inbuffer data 
	for(i=1;i<5;i++) 
	{ 
		Sleep(1000); 
		InBufferCount = dlg.m_Com.GetInBufferCount(); //接收到的字符数目 
		vResponse = dlg.m_Com.GetInput();   //读缓冲区 
		nowInBufferCount=dlg.m_Com.GetInBufferCount(); 
		str=(char*)(unsigned char*)vResponse.parray->pvData; 
		for(k=0;k<=InBufferCount;k++) Data[k] = *(str+k); 
		if(Data[2]=='>') 
		{ 
			dlg.SmsPDU(); 
			for(j=1;j<5;j++) 
			{ 
				Sleep(1000); 
				InBufferCount = dlg.m_Com.GetInBufferCount(); //接收到的字符数目 
				vResponse = dlg.m_Com.GetInput();   //读缓冲区 
				nowInBufferCount=dlg.m_Com.GetInBufferCount(); 
				str=(char*)(unsigned char*)vResponse.parray->pvData; 
				for(k=0;k<=InBufferCount;k++) Data[k] = *(str+k); 
				if(Data[2]=='+')  
				{ 
					if(dlg.m_Com.GetPortOpen()) dlg.m_Com.SetPortOpen(FALSE); 
					dlg.DestroyWindow(); 
					return 0; //sms send return ok 
				} 
			} 
		} 
 
	} 
 
	if(dlg.m_Com.GetPortOpen()) dlg.m_Com.SetPortOpen(FALSE); 
	if(dlg) dlg.DestroyWindow(); 
	return -1; //not send 
} 
 
 
BOOL CSmsApp::InitInstance()  
{ 
	CoInitialize(NULL);  
	AfxEnableControlContainer();  
 
	//此程序只能运行一次,用互斥量来判断程序是否已运行 
//    HANDLE m_hMutex=CreateMutex(NULL,TRUE, m_pszAppName);  
//  if(GetLastError()==ERROR_ALREADY_EXISTS) { return FALSE; } 
//	dlg.DoModal();  //this function can be replaced by the following codes 
 
   //Check if new succeeded and we got a valid pointer to a dialog object 
//	dlg.ShowWindow(SW_SHOW); 
 
/* 
   if(dlg!=NULL) 
   { 
      BOOL ret = dlg.Create(IDD_MODEMCONNET_DIALOG,this); 
      if(!ret)   //Create failed. 
         AfxMessageBox("Error creating Dialog"); 
      dlg->ShowWindow(SW_SHOW); 
   } 
*/ 
	return CWinApp::InitInstance(); 
}