mobilemodule.cpp

来自「wince中 我自己的流驱动实现 注册表和bib文件 修改在QZSerialS」· C++ 代码 · 共 1,978 行 · 第 1/4 页

CPP
1,978
字号
	{
		if (m_hWriteThread = CreateThread( NULL, 0, WriteSerialThread, 0, 0,&dwWriteThreadID ) )
		{//创建一个从串口读取数据的线程
			CloseHandle(m_hWriteThread);
		}
		m_hWriteSerialEvent = CreateEvent(NULL,FALSE,FALSE,NULL) ;
		return TRUE ;
	}
	else
		return FALSE ;
}

void CMobileModule::CloseComm()
{
	OpeSerial.CloseSerial();
}

void CMobileModule::AtCommand()
{//空AT指令
	CString ATCommand("AT\r");
	OpeSerial.WriteSerial(ATCommand);
}

void CMobileModule::Answer()
{
	CString ATCommand("ATA\r");
	OpeSerial.WriteSerial(ATCommand);
}

void CMobileModule::Call(CString PhoneNumber)
{
	CString ATCommand("ATD");
	ATCommand += PhoneNumber ;
	ATCommand += ";\r";
	OpeSerial.WriteSerial(ATCommand);
}

void CMobileModule::ReCall()
{
	CString ATCommand("ATDL\r");
	OpeSerial.WriteSerial(ATCommand);
}

void CMobileModule::HandUp()
{
	CString ATCommand("ATH0\r");
	OpeSerial.WriteSerial(ATCommand);
}

void CMobileModule::ATECommand(int value)
{//This setting determines whether or not the TA echoes
 //characters received from TE during command state.
	CString ATCommand("ATE");
	char tmp[2];

	memset(tmp,0,2);
	sprintf(tmp, "%d", value);
	ATCommand += tmp;
	ATCommand += "\r" ;
	OpeSerial.WriteSerial(ATCommand);
}

void CMobileModule::ATLCommand(int value)
{//Set monitor speaker loudness
//0 low speaker volume 
//1 low speaker volume 
//2 medium speaker volume 
//3 high speaker volume
	CString ATCommand("ATL");
	char tmp[2];

	memset(tmp,0,2);
	sprintf(tmp, "%d", value);
	ATCommand += tmp ;//(CString)value ;
	ATCommand += "\r" ;
	OpeSerial.WriteSerial(ATCommand);
}

void CMobileModule::ATMCommand(int value)
{//Set monitor speaker mode
//0 speaker is always off 
//1 speaker on until TA inform TE that carrier has been detected 
//2 speaker is always on when TA is off-hook
	CString ATCommand("ATM");
	char tmp[2];

	memset(tmp,0,2);
	sprintf(tmp, "%d", value);
	ATCommand += tmp ;
//	ATCommand += (CString)value ;
	ATCommand += "\r" ;
	OpeSerial.WriteSerial(ATCommand);
}

void CMobileModule::ATSCommand(int value,int n)
{
	CString ATCommand("ATS");
	switch(value)
	{
	case 0: //set number of rings before automatically answering the call
	case 3://Set command line termination character
	case 4: //Set response formatting character
	case 5://Set command line editing character
	case 6://Set pause before blind dialing
	case 7://set number of seconds to wait for connection completion
	case 8://set number of second to wait for comma dial modifier
	case 10://Set disconnect delay after indicating the absence of data carries
//		ATCommand += (CString)value ;
		char tmp[2];
		
		memset(tmp,0,2);
		sprintf(tmp, "%d", value);
		ATCommand += tmp ;
		ATCommand += "=" ;
		memset(tmp,0,2) ;
		sprintf(tmp,"%d",n) ;
		ATCommand += tmp ;
		ATCommand += "\r" ;
		OpeSerial.WriteSerial(ATCommand);
		break ;
	default:
		break ;
	}
}

void CMobileModule::ATTCommand()
{//Select tone dialing
	CString ATCommand("ATT\r") ;
	OpeSerial.WriteSerial(ATCommand);
}

void CMobileModule::ATVCommand(int value)
{//Set result code format mode
//	0 Information response: <text><CR><LF> 
//Short result code format: <numeric code><CR> 
//1 Information response: <CR><LF><text><CR><LF>
//result code format: <CR><LF><verbose code><CR><LF>
	CString ATCommand("ATV");
	char tmp[2];

	memset(tmp,0,2);
	sprintf(tmp, "%d", value);
	ATCommand += tmp ;
//	ATCommand += CString(value);
	ATCommand += "\r" ;
	OpeSerial.WriteSerial(ATCommand);
}

void CMobileModule::ATXCommand(int value)
{//Set CONNECT result code
//0 CONNECT result code only returned, dial tone and busy detection are both disabled 
//1 CONNECT<text> result code only returned, dial tone and busy detection are both disabled 
//2 CONNECT<text> result code returned, dial tone detection is enabled, busy detection is disabled 
//3 CONNECT<text> result code returned, dial tone detection is disabled, busy detection is enabled 
//4 CONNECT<text> result code returned, dial tone and busy detection are both enabled
	CString ATCommand("ATX");
	char tmp[2];

	memset(tmp,0,2);
	sprintf(tmp, "%d", value);
	ATCommand += tmp ;
//	ATCommand += (CString)value ;
	ATCommand += "\r" ;
	OpeSerial.WriteSerial(ATCommand);
}

void CMobileModule::ATFCommand()
{//Set all current parameters to manufacturer defaults
	CString ATCommand("AT&F\r") ;
	OpeSerial.WriteSerial(ATCommand);
}

void CMobileModule::ATWCommand()
{//Store current parameter to user defined profile
	CString ATCommand("AT&W\r");
	OpeSerial.WriteSerial(ATCommand);
}

void CMobileModule::GSNCommand()
{//Request TA serial number identification (IMEI)
	CString ATCommand("AT+GSN\r") ;
	OpeSerial.WriteSerial(ATCommand);
}

void CMobileModule::IPRCommand(int value)
{//Set TE-TA fixed local rate
	CString ATCommand("AT+IPR=") ;
	char tmp[10];

	memset(tmp,0,10);
	sprintf(tmp, "%d", value);
	ATCommand += tmp ;
//	ATCommand += (CString)value ;
	ATCommand += "\r" ;
	OpeSerial.WriteSerial(ATCommand);
}

void CMobileModule::CMGDCommand(int Index)
{//Delete SMS Message
	CString ATCommand("AT+CMGD=");
	char tmp[10];

	memset(tmp,0,2);
	sprintf(tmp, "%d", Index);
	ATCommand += tmp ;
	ATCommand += "\r" ;
	OpeSerial.WriteSerial(ATCommand);
}

void CMobileModule::CMGFCommand(CString Index)
{//Select SMS Message Format
 //0 PDU mode
 //1 Text mode
	CString ATCommand("AT+CMGF=");
	ATCommand += Index ;
	ATCommand += "\r" ;
	OpeSerial.WriteSerial(ATCommand);
}

void CMobileModule::CMGLCommand(int value)
{//1) If text mode: <value> 
//"REC UNREAD" Received unread messages (default) 
//"REC READ" Received read messages 
//"STO UNSENT" Stored unsent messages 
//"STO SENT" Stored sent messages 
//"ALL" All messages 
//2) If PDU mode: <value> 
//0 Received unread messages (default) 
//1 Received read messages 
//2 Stored unsent messages 
//3 Stored sent messages 
//4 All messages
	CString ATCommand("AT+CMGL=") ;
	char tmp[2] ;
	memset(tmp,0,2) ;
	sprintf(tmp,"%d",value) ;
	ATCommand += tmp ;
	ATCommand += "\r" ;
	OpeSerial.WriteSerial(ATCommand);
}

void CMobileModule::CMGRCommand(int value)
{//Read SMS Message
//<value> integer type; value in the range of location numbers supported 
//by the associated memory
	CString ATCommand("AT+CMGR=") ;
	char tmp[2] ;
	memset(tmp ,0 ,2) ;
	sprintf(tmp,"%d",value) ;
	ATCommand += tmp ;
	ATCommand += "\r" ;
	OpeSerial.WriteSerial(ATCommand);
}

void CMobileModule::CMGSCommand(char* SmsCenter, char* DesAddress, char* SmsContent)
{//Send sms
	//pdu format

	char CTRL_Z = 0x1A;///
	int smslen = 0 ;
	char *pDestSmsContent = new char[1024] ;
	memset(pDestSmsContent,0,1024) ;
	int ret = TextToSms(SmsCenter,DesAddress,SmsContent,0,0,&smslen,pDestSmsContent) ;


	int SmsLength = strlen(pDestSmsContent) ;
	strcat(pDestSmsContent,"\x01a") ;

	unsigned char nSmscLength ;
	gsmString2Bytes(pDestSmsContent,&nSmscLength,2) ;
	nSmscLength++ ;
	CString Code("") ;
	char char_temp[50] ;
	memset(char_temp,0,50) ;
	sprintf(char_temp, "AT+CMGS=%d\r", SmsLength / 2 - nSmscLength);
	Code += char_temp ;
	printf("SmsCenter=%s\n",SmsCenter);
	printf("DesAddress=%s\n",DesAddress);
	printf("SmsContent=%s\n",SmsContent);
	printf("test 1\n");
	OpeSerial.WriteSerial(Code);
	printf("test 2\n");
	Sleep(50);  //bing
	OpeSerial.WriteSerial(pDestSmsContent);
	delete [] pDestSmsContent ;
}

void CMobileModule::CNMICommand(int mode, int mt, int bm, int ds, int bfr)
{//New SMS message indications
	CString ATCommand("AT+CNMI="), code("");
	char temp[50];

	memset(temp, 0, 50);
	sprintf(temp, "%s%d,%d,%d,%d,%d\r", ATCommand, mode, mt, bm, ds, bfr);
	code = temp;

	OpeSerial.WriteSerial(ATCommand);
}

void CMobileModule::CSCASetCommand(char* CenterNumber)
{//Set sms center number
	CString m_CenterNumber = CenterNumber ;
	CString ATCommand("AT+CSCA=\"") ;
	ATCommand += m_CenterNumber ;
	ATCommand += "\"\r" ;
	OpeSerial.WriteSerial(ATCommand);
}

void CMobileModule::CSCAGetCommand()
{//Get sms center number
	CString ATCommand("AT+CSCA?\r") ;
	OpeSerial.WriteSerial(ATCommand);
}

//Next At command is done for PhoneBook Set
void CMobileModule::CPBSCommand(CString Storage)
{//Select phone book memory storage
	//"MC" ME missed (unanswered) calls list 
	//"RC" ME received calls list 
	//"DC" ME dialed calls list(+CPBW may not be applicable or this storage)(same as LD) 
	//"LA" Last Number All list (LND/LNM/LNR) 
	//"ME" ME phonebook 
	//"BN" SIM barred dialed number 
	//"SD" SIM service dial number 
	//"VM" SIM voice mailbox 
	//"FD" SIM fix dialing-phone book 
	//"LD" SIM last-dialing-phone book 
	//"ON" SIM (or ME) own numbers (MSISDNs) list 
	//"SM" SIM phonebook
	CString ATCommand("AT+CPBS=\"") ;
	ATCommand += Storage ;
	ATCommand += "\"\r" ;
	OpeSerial.WriteSerial(ATCommand);
}

void CMobileModule::CPBSCommand()
{//Get Phone Book Record Number
	CString ATCommand("AT+CPBS?\r") ;
	OpeSerial.WriteSerial(ATCommand);
}

void CMobileModule::GetPhoneBookRecordNumber()
{
	CString ATCommand("AT+CPBS?\r") ;
	OpeSerial.WriteSerial(ATCommand);
}

void CMobileModule::CPBWCommand(int Index, CString PhoneNumber, CString RecordName)
{//Save Phone Book Record
//Before this operation , please set scsc at ucs2 format
	char buf[2], srcName[40];
	unsigned char PhoneBook_RecordName[40];
	char DestName[40] ;
	memset(buf,0,2) ;
	memset(srcName,0,40);
	memset(PhoneBook_RecordName,0,40) ;
	memset(DestName,0,40) ;
	CString ATCommand("AT+CPBW=") ;
	_itoa(Index,buf,10) ;
	ATCommand += buf ;
	ATCommand += ",\"" ;
	ATCommand += PhoneNumber ;
	ATCommand += ",129,\"" ;
	//Encode Record Name
	LPWSTR pName = RecordName.GetBuffer(RecordName.GetLength()) ;
	int name_length = wcslen(pName) ;
	int len = WideCharToMultiByte(CP_ACP,0,pName,name_length,srcName,name_length,NULL,NULL) ;
	srcName[++len] = '\0' ;
	int nDstLength = gsmEncodeUcs2(srcName,PhoneBook_RecordName,len) ;
	gsmBytes2String(PhoneBook_RecordName,DestName,nDstLength) ;

	ATCommand += DestName ;
	ATCommand += "\"\r" ;
	OpeSerial.WriteSerial(ATCommand);
}

void CMobileModule::CPBRCommand(int Index)
{//Get Current Phone Book Entry
	char temp[2] ;
	memset(temp,0,2) ;
	CString ATCommand("AT+CPBR=") ;
	sprintf(temp,"%d",Index) ;
	ATCommand += temp ;
	ATCommand += "\r" ;

	//bing 2007.09.06
	//::PostMessage(HWND_BROADCAST,WM_COPYPHONEBOOK, (UINT)(Index), 0) ;

	OpeSerial.WriteSerial(ATCommand);
}

void CMobileModule::CPBFCommand(CString FindText)
{//Find phone book entries
	CString ATCommand("AT+CPBF=\"") ;
	char srcFindText[40] ;
	char DestFindText[40] ;
	unsigned char PhoneBook_FindText[40] ;
	memset(srcFindText,0,40) ;
	memset(DestFindText,0,40) ;
	memset(PhoneBook_FindText,0,40) ;
	//Encode Record Name
	LPWSTR pFindText = FindText.GetBuffer(FindText.GetLength()) ;
	int FindText_length = wcslen(pFindText) ;
	int len = WideCharToMultiByte(CP_ACP,0,pFindText,FindText_length,srcFindText,FindText_length,NULL,NULL) ;
	srcFindText[++len] = '\0' ;
	int nDstLength = gsmEncodeUcs2(srcFindText,PhoneBook_FindText,len) ;
	gsmBytes2String(PhoneBook_FindText,DestFindText,nDstLength) ;

	ATCommand += DestFindText ;
	ATCommand += "\"\r" ;
	OpeSerial.WriteSerial(ATCommand);
}

#if 0  //bing 2007.09.07
void CMobileModule::SmsToText(CString RetInfo, char* SendNum, char* SendTime, char* SmsContent)
{
	//csca restore sms center number
	//sendnum restore the number whitch sent sms to me
	//sendtime restore the time this sms sent
	//smsnr retore the content of sms
	CString tmp("") ;
	char char_tmp[1024] ;
	char csca[20] ;
	memset(char_tmp,0,1024) ;
	memset(csca,0,20) ;
	tmp = RetInfo.Right(RetInfo.GetLength()-RetInfo.Find(_T("+CMGR:"))) ;

	tmp = tmp.Right(tmp.GetLength()-tmp.Find(_T("\r"))) ;
	tmp = tmp.Left(tmp.Find(_T("OK")));
	tmp.Remove('\r') ;
	tmp.Remove('\n') ;
	//Check if received sms is empty
	if(!tmp.Compare(L""))
		return ;
	//check if received sms including "000A"(just return charactor)
	int Find_Int = tmp.Find(_T("000A")) ;
	if(Find_Int != -1)
	{
		int retchar_numbers = 0 ;
		CString tmp_Right = tmp ;
		CString tmp_Left("") ;
		do{
			tmp_Left = tmp_Right.Left(Find_Int) + "000D000A" ;
			retchar_numbers++ ;
			tmp_Right = tmp_Right.Right(tmp_Right.GetLength() - Find_Int - 4) ;
			Find_Int = tmp_Right.Find(_T("000A")) ;
		}while(Find_Int != -1);
		//Get number of charactors from received sms and replace char number of new sms return char
		CString str_Number = tmp_Left.Left(60) ;
		str_Number = str_Number.Right(2) ;
		int char_num = HexToInteger(str_Number) ;
		char_num += 2 * retchar_numbers ;
		str_Number = IntegerToHex(char_num) ;
		CString temp_left = tmp_Left.Left(58) ;
		CString temp_right = tmp_Left.Right(tmp_Left.GetLength()-60) ;
		tmp_Left = temp_left + str_Number + temp_right ;
		tmp = tmp_Left + tmp_Right  ;
	}

	LPWSTR ptemp = tmp.GetBuffer(tmp.GetLength()) ;
	int len = WideCharToMultiByte(CP_ACP,NULL,ptemp,wcslen(ptemp),char_tmp,wcslen(ptemp),NULL,NULL) ;
	char_tmp[len] = '\0' ;
	gsmDecodePdu(char_tmp,csca,SendNum,SendTime,SmsContent) ;
}
#endif //bing-
void CMobileModule::SmsToText(CString RetInfo, char* SendNum, char* SendTime, char* SmsContent)
{
	//csca restore sms center number
	//sendnum restore the number whitch sent sms to me
	//sendtime restore the time this sms sent
	//smsnr retore the content of sms
	CString tmp("") ;
	char char_tmp[1024] ;
	char csca[20] ;
	memset(char_tmp,0,1024) ;
	memset(csca,0,20) ;
	tmp = RetInfo.Right(RetInfo.GetLength()-RetInfo.Find(_T("+CMGR:"))) ;

	tmp = tmp.Right(tmp.GetLength()-tmp.Find(_T("\r"))) ;
	tmp = tmp.Left(tmp.Find(_T("OK")));
	tmp.Remove('\r') ;
	tmp.Remove('\n') ;
	//Check if received sms is empty

//bing 
	if(!tmp.Compare(L""))
		return ;

	int returntimes = 0 ;
	int Find_Int = tmp.Find(_T("000A")) ;
	if(Find_Int != -1)
	{
		CString tmp_Right("") ;
		CString tmp_Left("") ;
		tmp_Left += tmp.Left(Find_Int) + "000D000A" ;

⌨️ 快捷键说明

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