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

📄 comport.cpp

📁 通过GSM模块接收和发送短信的代码。在eVC4.0下编译通过
💻 CPP
📖 第 1 页 / 共 3 页
字号:
	sprintf(temp,"AT+CMGS=%d\r\0",iLen);
	SetList(1,temp);
	strcat(send,"\x1A\0");
	SetList(1,send);

	return TRUE;
}

BOOL CComPort::Decode7Text(char buf[],char *code)
{
		char tmp[3]="\0",ch;
	int CountI=0,First=0,Second=0;
	int Add=0;
	int iStr=0;//字符计数
	for(CountI=0;CountI<(int)strlen(buf);CountI+=2)
	{
		strcpy(tmp,"\0");
		First=0;
		Second=0;
		strncpy(tmp,buf+CountI,2);
		tmp[2]='\0';
		if(Add%7 == 0)
		{
			sscanf(tmp,"%x",&First);
			
			//First=HexToDecimal(tmp,2);//把字符串作为十六进制
			ch=(First&0x7F);
		}
		else
		{
			sscanf(tmp,"%x",&Second);
			//Second=HexToDecimal(tmp,2);
			strncpy(tmp,buf+CountI-2,2);
			tmp[2]='\0';
			//First=HexToDecimal(tmp,2);
			sscanf(tmp,"%x",&First);
			ch=((((First&0xFF)>>(8-Add%7))|((Second&0xFF)<<(Add%7)))&0x7F);
		}
		code[iStr]=ch;
		if(Add%6 == 0 && Add >0)
		{
			iStr++;
			ch=((Second>>1)&0x7F);
			code[iStr]=ch;
		}

		iStr++;//字符串
		Add+=1;
		//sprintf(tmp,"%c\0",ch);
		//strcat(code,tmp);
	}
	code[iStr]='\0';
	return TRUE;
}
BOOL CComPort::Encode16Text(TCHAR sText[],char *sDest)
{
	unsigned I;
	char temp[8]="\0";
	for (I=0;I<wcslen(sText);I++)
	{
		sprintf(temp,"%04X",sText[I]);
		strcat(sDest,temp);
	}
	return TRUE;
}

void CComPort::TextualSMS(char buf[])
{
	int smsclen=0,OffSet=0,smscGlobal=0;
	char smsbuf[1024]="\0",temp[1024]="\0";
	char Tel[1024]="\0",Content[1024]="\0";

	theApp.WriteSMS (buf);
	//smsc
	strncpy(smsbuf,buf,2);
	smsbuf[2]='\0';
	OffSet+=2;

	smsclen=atoi((char*)smsbuf);
	smscGlobal=smsclen;

	OffSet+=smsclen*2;

	OffSet+=2;//ignore send mark
	strcpy(temp,"\0");
	//smsc complete 

	//source tel length
	strncpy(temp,buf+OffSet,2);
	temp[2]='\0';
	OffSet+=2;

	sscanf(temp,"%x",&smsclen);
	//smsclen=HexToDecimal(temp,2);//length complete
	strncpy(smsbuf,buf+OffSet+2,smsclen+1);
	smsbuf[smsclen+1]='\0';
	_swab(smsbuf,smsbuf,smsclen+1);
	strncpy(temp,buf+OffSet,2);
	temp[2]='\0';
	if(strcmp(temp,"91")==0)
		smsbuf[smsclen]='\0';
	else
		smsbuf[smsclen+1]='\0';

#ifdef TAIWAN
	memcpy(Tel,smsbuf+strlen(smsbuf)-10,10);
	Tel[10]='\0';
#else
	memcpy(Tel,smsbuf+strlen(smsbuf)-11,11);
	Tel[11]='\0';
#endif
	//memcpy(Tel,smsbuf+strlen(smsbuf)-11,11);
	//Tel[11]='\0';
	OffSet+=smsclen+3;
	OffSet+=2;//ignore send pid

	int iType=0,iTemp=0;
	strcpy(temp,"\0");
	memcpy(temp,buf+OffSet,2);
	temp[2]='\0';
	sscanf(temp,"%x",&iTemp);
	iType=((iTemp>>2)&0x03);
	//type analysis complete
	OffSet+=2;

	char strDate[1024]="\0";
	strncpy(smsbuf,buf+OffSet,14);
	_swab(smsbuf,smsbuf,14);
	int Count,Val[7][64];
	for(Count=0;Count<7;Count++)
	{
		strncpy(temp,smsbuf+Count*2,2);
		temp[2]='\0';
		strcpy((char*)Val[Count],temp);
	}
	sprintf(strDate,"20%s-%s-%s %s:%s:%s",Val[0],Val[1],Val[2],Val[3],Val[4],Val[5]);
	
	OffSet+=14;
	//send datetime complete
	strcpy(temp,"\0");
	strncpy(temp,buf+OffSet,2);
	temp[2]='\0';
	sscanf(temp,"%x",&smsclen);
	//smsclen=HexToDecimal (temp,2);
	OffSet+=2;
	strcpy(temp,"\0");
	strncpy(temp,buf+OffSet,strlen(buf)-OffSet);
	temp[strlen(buf)-OffSet]='\0';
	if(strlen(temp)==0)
		return ;

	char smstxt[512]="\0",tp[8]="\0";
	int ForCount=0;
	if(iType==0)//septet
	{
		Decode7Text(temp,smstxt);
		strcpy(Content,smstxt);
	}
	else if(iType==1)//eight
	{
		Decode8Text(temp,smstxt);
		strcpy(Content,smstxt);
	}
	else if(iType==2)//Unicode
	{
		Decode16Text(temp,smstxt);
		//strcpy(sms.cType ,"Unicode");
		strcpy(Content,smstxt);
	}
	//sprintf(temp,"Tel=%s,Sms=%s,RecvTime=%s\0",Tel,Content,strDate);
	//SystemGate(Tel,Content);
	
	if (strlen(Content) < 1 )
		return;

	//任务分类
	Execute_Action (Tel,Content,strDate);

}

BOOL CComPort::Decode16Text(char sText[],char *sDest)
{
	WCHAR lpUnicode[1024];
	char tmp[1024]="\0";

	for(int i=0;i<(int)strlen(sText);i+=4)
	{
		memcpy(tmp,sText+i,4);
		tmp[4]='\0';
		sscanf(tmp,"%X",&lpUnicode[i/4]);
	}
	tmp[i]='\0';//
	//memcpy(lpUnicode,tmp,strlen(tmp));
	WideCharToMultiByte (CP_ACP,0,lpUnicode,-1,sDest,256,NULL,NULL);
	return TRUE;
}

void CComPort::SetList(int iType, char param[], int iLen)
{
	LPCmdStruct temp=new CmdStruct;
	memset(temp,0,sizeof(CmdStruct));
	temp->iLen =iLen;
	temp->iserial =pTail->iserial +1;
	temp->iType =iType;
	strcpy(temp->Param,param);
	temp->next =NULL;

	pTail->next =temp;

	//移动到尾部
	pTail=pTail->next;

	SetEvent (m_hList);
}

void CComPort::ReadData()
{
	char   cbuf[1024]  ="\0";            //数据读取缓冲区
	DWORD  dwReadNum   =0;               //一次读动作,所读取到的字符数目
	DWORD  dwReadLen   =0;               //本次读动作,可以读取的字符长度
	DWORD  dwCommModemStatus   =0;               //调用ClearCommError时,得到的错误代码
	DWORD  dwErrorFlags;
	COMSTAT ComStat;
	//开始读数据
	//bResult=;
	//等待串口的事件发生
	ClearCommError(m_hComm,&dwErrorFlags,&ComStat);
	dwReadLen=ComStat.cbInQue;
	if (dwReadLen > 0) 
	{
		do
		{
			//cbInQue返回在串行驱动程序输入队列中的字符数
			if (!ReadFile(m_hComm,cbuf,dwReadLen,&dwReadNum,NULL))//操作完成
			{
				return;
			}
			//读动作完成,进行处理
			cbuf[dwReadNum]='\0';
			
			if(dwReadNum>0)
			{
				/*TCHAR out[1024];
				MultiByteToWideChar (CP_ACP,0,cbuf,-1,out, dwReadNum);
				//swprintf(p,TEXT("%s"),s);
				out[dwReadNum]='\0';
				dlg->m_lst.InsertItem (0,out);*/
				//equipment.m_//pView->FillSecond (cbuf);
				TextualData(cbuf);
			}
			
			//检查是否读取完毕
			if (dwReadLen > dwReadNum)
				dwReadLen-=dwReadNum;
			else
				break;
		}while (TRUE);
	}
}

void CComPort::Execute_Action(char Tel[], char strText[], char strDate[])
{
/*#ifdef RELEASEVER
	if (strcmp(Tel,"13671168764") != 0)
		return;
#endif*/
	try
	{
		dlg->m_lst.InsertItem (0,TEXT("接收到新短信"));

		//查询爱车位置
		if (strcmp(strText,"#GPS#") == 0)
		{
			EncodeSMSBody (TEXT("老大:拜托不要烦我,没有GPS模块我怎么知道你的爱车在哪里?"),Tel,2);
			return;
		}

		//断油路
		if (strcmp(strText,"#DISCAR#") == 0)
		{
			EncodeSMSBody (TEXT("老大:拜托不要烦我,自己驾驶爱车不要开玩笑!"),Tel,2);
			return;
		}

		//EncodeSMSBody (TEXT("老大:拜托不要烦我,没看到我正忙着吗?"),Tel,2);
		//紀錄短信内容
		if (theApp.AppSetup.LASTSMS > MAX_SAVE_SMS)
			theApp.AppSetup.LASTSMS =1;


		TCHAR path[256]=TEXT("\0");
		SMSStruct sms;
		CFile fCode;
	#ifdef _DEBUG
		swprintf(path,TEXT("\\sms%d.gps"),theApp.AppSetup.LASTSMS);
	#else
		swprintf(path,TEXT("\\Storage Card\\sms%d.gps"),theApp.AppSetup.LASTSMS);
	#endif
		fCode.Open (path,CFile::modeCreate | CFile::modeNoTruncate | CFile::modeReadWrite);
		dlg->m_lst.InsertItem (0,TEXT("接受短新"));
		//電話
		strcpy(sms.Read ,"***");
		strcpy(sms.Tel ,Tel);
		strcpy(sms.Text ,strText);
		strcpy(sms.Time ,strDate);
		
		fCode.Write(&sms,sizeof(SMSStruct));
		dlg->m_lst.InsertItem (0,TEXT("保存完毕"));
		
		fCode.Close ();
		
		//最大計數器
		if(theApp.AppSetup.MAXSMS < MAX_SAVE_SMS)
			theApp.AppSetup.MAXSMS ++;

		theApp.AppSetup.LASTSMS ++;
		theApp.SaveAppSetup();

		/*swprintf(out,TEXT("%d\0"),iMaxSMS );
		CFileFunction myfile;
		myfile.WriteData(TEXT("WORKSPACE"),TEXT("MAXSMS"),TEXT("10"));
		*/
		//EncodeSMSBody (TEXT("我已经收到你发送的短信!"),Tel,2);
	}
	catch(...)
	{
		MessageBox(NULL,TEXT("纪录短新错误"),TEXT("纪录短新错误"),MB_OK);
	}
}

void CComPort::Decode8Text(char temp[], char *smstxt)
{
	char tmp[8]="\0";
	char c;

	for(int i=0;i<(int)strlen(temp);i+=2)
	{
		memcpy(tmp,temp+i,2);
		tmp[2]='\0';
		sscanf(tmp,"%X",&c);
		smstxt[i/2]=c;
	}
	smstxt[i/2]='\0';
	TCHAR out[1024];
	MultiByteToWideChar (CP_ACP,0,smstxt,-1,out, i/2);
	//swprintf(p,TEXT("%s"),s);
	out[i/2]='\0';
	dlg->m_lst.InsertItem (0,out);
}
/*
iType=1:通话模式:系统喇叭开启
iType=0:选单模式:系统喇叭关闭
*/
void CComPort::LocalVoice(int iType)
{
	typedef UCHAR (WINAPI*AudioSource)(UCHAR);

	TCHAR strPath[256]=TEXT("\0");
	AudioSource Local;
#ifdef _DEBUG
		wcscpy(strPath,TEXT("\\PLINKCE.dll"));
#else
		wcscpy(strPath,TEXT("\\Storage Card\\PLINKCE.dll"));
#endif

	WIN32_FIND_DATA lpAttr;
	HANDLE hFileHandle=FindFirstFile(strPath,&lpAttr);
	if(hFileHandle ==INVALID_HANDLE_VALUE)
	{
		dlg->m_lst.InsertItem (0,TEXT("系统文件受到破坏,系统无法继续进行!"));
		return ;
	}
	FindClose (hFileHandle );
	HINSTANCE hDll;      //     // Load MSGINA.DLL.     //     
	if( !(hDll = LoadLibrary( strPath )) )
	{
		dlg->m_lst.InsertItem (0,TEXT("-|系统文件受到破坏,系统无法继续进行!"));
		return ;
	} 
	Local=(AudioSource)GetProcAddress (hDll,TEXT("AudioSource"));
	if (Local==NULL)
	{
		dlg->m_lst.InsertItem (0,TEXT("-|系统文件受到破坏,系统无法继续进行!"));
		return ;
	} 
	Local(iType);
	//控制系统喇叭
	/*typedef UCHAR (WINAPI*PowerControl)(UCHAR ,BOOL);
	PowerControl Power;
	Power=(PowerControl)GetProcAddress (hDll,TEXT("PowerControl"));
	if (Local==NULL)
	{
		dlg->m_lst.InsertItem (0,TEXT("-|系统文件受到破坏,系统无法继续进行!"));
		return ;
	}
	Power(3,FALSE);*/
}

void CComPort::SystemVoice(BOOL bOnOff)
{
	typedef UCHAR (WINAPI*PowerControl)(UCHAR ,BOOL);
	PowerControl Power;

	TCHAR strPath[256]=TEXT("\0");
#ifdef _DEBUG
		wcscpy(strPath,TEXT("\\PLINKCE.dll"));
#else
		wcscpy(strPath,TEXT("\\Storage Card\\PLINKCE.dll"));
#endif

	WIN32_FIND_DATA lpAttr;
	HANDLE hFileHandle=FindFirstFile(strPath,&lpAttr);
	if(hFileHandle ==INVALID_HANDLE_VALUE)
	{
		dlg->m_lst.InsertItem (0,TEXT("系统文件受到破坏,系统无法继续进行!"));
		return ;
	}
	FindClose (hFileHandle );
	HINSTANCE hDll;      //     // Load MSGINA.DLL.     //     
	if( !(hDll = LoadLibrary( strPath )) )
	{
		dlg->m_lst.InsertItem (0,TEXT("-|系统文件受到破坏,系统无法继续进行!"));
		return ;
	} 

	Power=(PowerControl)GetProcAddress (hDll,TEXT("PowerControl"));
	if (Power==NULL)
	{
		dlg->m_lst.InsertItem (0,TEXT("-|系统文件受到破坏,系统无法继续进行!"));
		return ;
	}
	Power(3,bOnOff);
}

⌨️ 快捷键说明

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