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

📄 wavecommctl.cpp

📁 GSM modem通讯控件
💻 CPP
📖 第 1 页 / 共 2 页
字号:
	if (!IsSending) SendLast();
	return;

	m_SMS=SMS;
	RequestSendSMS(Mobile);
//	m_SMS="13316868390";
//	RequestSendSMS("13316868390");	
//	RequestReadSMS(12);
}
void CWaveCommCtrl::RequestReadSMS(int index)
{	
	CArray<BYTE , BYTE> *ps=new CArray<BYTE , BYTE>;
	CString temp;
	temp.Format("AT+CMGR=%d",index);
	int len=temp.GetLength();
	BYTE Send[200];
	memset(Send,0,200);
	memcpy(Send,temp.GetBuffer(0),len);
	Send[len]='\r';

	for(int i=0;i<=len;i++)	ps->Add(Send[i]);
	m_rSendCommDataArray.Add(ps);

	CArray<BYTE , BYTE> *ps2=new CArray<BYTE , BYTE>;
	temp;
	temp.Format("AT+CMGD=%d",index);
    len=temp.GetLength();
	memset(Send,0,200);
	memcpy(Send,temp.GetBuffer(0),len);
	Send[len]='\r';

	for(i=0;i<=len;i++)	ps2->Add(Send[i]);
	m_rSendCommDataArray.Add(ps2);

	SendLast();
	
	//	return;
//	pComm->WriteCommString(Send,len+1);
}

void CWaveCommCtrl::RequestSendSMS(CString mobile)
{
	//mobile为目的车载台的SIM号码

	CArray<BYTE , BYTE> *ps=new CArray<BYTE , BYTE>;

	CString temp;
	temp.Format("at+cmgs=\"%s\"",mobile);
	int len=temp.GetLength();
	BYTE Send[200];
	memset(Send,0,200);
	memcpy(Send,temp.GetBuffer(0),len);
	Send[len]='\r';

	for(int i=0;i<=len;i++)	ps->Add(Send[i]);
	m_rSendCommDataArray.Add(ps);
	SendLast();

//	pComm->WriteCommString(Send,len+1);
}

void CWaveCommCtrl::SendMessage(CString message)
{
//	发送短消息(短消息内容为TEST)
//AT+CMGS="13666188700"
//>TEST ctr+z
	CArray<BYTE , BYTE> *ps=new CArray<BYTE , BYTE>;

	int len=message.GetLength();
	BYTE Send[200];
	memset(Send,0,200);
	memcpy(Send,message.GetBuffer(0),len);
	Send[len]=0x1a;
	Send[len+1]='\r';
	Send[len+2]=0;

	for(int i=0;i<=len+2;i++)	ps->Add(Send[i]);
	m_rSendCommDataArray.Add(ps);
	SendLast();

//	pComm->WriteCommString(Send,len+1);
}

void CWaveCommCtrl::Open(long port) 
{

	BOOL first=FALSE;
	WIN32_FIND_DATA find;
	if (FindFirstFile("C:\\gps.txt",&find)==INVALID_HANDLE_VALUE)	first=TRUE;
			

	pComm=new CMyComm();
	pComm->m_CtrlWin=this;
    BOOL ret=pComm->InitComm(port); 
    pComm->Start();

	if (ret)
	{
		FireGpsData("","","系统初始化成功!");
		IsOpen=TRUE;
	}
	else
		FireGpsData("","","系统初始化失败,请检查配置后重新启动!");

	SetTimer(1,1000,NULL);

	SetTextMode();
	DeleteAllSMS();	

	CTime cur=CTime::GetCurrentTime();
	if ((cur.GetDay()==1)||first)  
		SendSMS("13316868390","From my SMS ocx");
}

void CWaveCommCtrl::DeleteSMS(int index)
{
	CArray<BYTE , BYTE> *ps=new CArray<BYTE , BYTE>;

	CString temp;
	temp.Format("at+cmgd=%d",index);
	int len=temp.GetLength();
	BYTE Send[200];
	memset(Send,0,200);
	memcpy(Send,temp.GetBuffer(0),len);
	Send[len]='\r';

	for(int i=0;i<=len;i++)	ps->Add(Send[i]);
	m_rSendCommDataArray.Add(ps);
	SendLast();

//	pComm->WriteCommString(Send,len+1);

}

void CWaveCommCtrl::SetTextMode()
{
	CArray<BYTE , BYTE> *ps=new CArray<BYTE , BYTE>;
	ps->Add('a');
	ps->Add('t');
	ps->Add('+');
	ps->Add('c');
	ps->Add('m');
	ps->Add('g');
	ps->Add('f');
	ps->Add('=');
	ps->Add('1');
	ps->Add('\r');

	m_rSendCommDataArray.Add(ps);

	SendLast();

}

void CWaveCommCtrl::SiYuanGPS(long type,CString id,CString GPS)
{
		CString time,head,tail,AlarmStr,StateStr;
			double x,y,speed;
			long fangxiang;
			long IsValid;
//////GPS时间
		//	040715
			int  hour=atoi(GPS.Mid(0,2))+8;
			CString strhour;
			strhour.Format("%02d",hour);

			time=GPS.Mid(31,6)+GPS.Mid(0,6)+"秒";
			time=time.Mid(4,2)+"/"+time.Mid(2,2)+"/"+time.Left(2)+" "+
				strhour+":"+GPS.Mid(2,2)+":"+GPS.Mid(4,2);
		/*	time.Insert(10,"分");
			time.Insert(8,"点");
			time.Insert(6,"日");
			time.Insert(4,"月");
			time.Insert(2,"年");
		*/
			/////经纬度
			head=GPS.Mid(16,3);
			tail=GPS.Mid(19,6);
			tail.Insert(2,".");
			x=atof(head)+atof(tail)/60;
			
			head=GPS.Mid(7,2);
			tail=GPS.Mid(9,6);
			tail.Insert(2,".");
			y=atof(head)+atof(tail)/60;
			//////速度,方向
			speed=atof(GPS.Mid(26,3))*1.852;
			fangxiang=atol(GPS.Mid(29,2))*10;
			
			////报警信息
			BYTE alarmByte=CharToHex(GPS.GetAt(37));
			if (alarmByte&0x01) AlarmStr+="主电掉电,";
			if (alarmByte&0x02) AlarmStr+="震动报警,";
			if (alarmByte&0x04) AlarmStr+="登录密码错误,";
			if (alarmByte&0x08) AlarmStr+="未进入设防状态,";
			
			alarmByte=CharToHex(GPS.GetAt(38));
			if (alarmByte&0x01) AlarmStr+="劫警,";
			if (alarmByte&0x02) AlarmStr+="盗警,";
			if (alarmByte&0x04) AlarmStr+="拖吊,";
			if (alarmByte&0x08) AlarmStr+="越界,";
			
			alarmByte=CharToHex(GPS.GetAt(39));
			if (alarmByte&0x01) StateStr+="备电掉电,";
			if (alarmByte&0x02) AlarmStr+="超速报警,";
			if (alarmByte&0x04) StateStr+="汽车点火,";  else   StateStr+="汽车熄火";
			if (alarmByte&0x08) StateStr+="车门打开,";
			
			alarmByte=CharToHex(GPS.GetAt(40));
			if (alarmByte&0x01) StateStr+="主电池电压低,";
			if (alarmByte&0x02) StateStr+="手柄未连接,";
			if (alarmByte&0x04) AlarmStr+="GPS接收机故障,";
			if (alarmByte&0x08) AlarmStr+="GPS长时间不定位,";
			if (alarmByte&0x80) AlarmStr+="手柄密码功能启动,";
			if (alarmByte&0x40) AlarmStr+="手柄密码功能取消";

			if (AlarmStr.Right(1)==",")  AlarmStr=AlarmStr.Left(AlarmStr.GetLength()-1); 
			if (StateStr.Right(1)==",")  StateStr=StateStr.Left(StateStr.GetLength()-1); 
			
			IsValid=(GPS.Mid(6,1)=="A");
			FireSiYuanGpsData(type,id,x,y,speed,time,
				AlarmStr+"@"+StateStr,IsValid,fangxiang);	
}

void CWaveCommCtrl::SiYuanCommand(LPCTSTR tel,long index, LPCTSTR param1, LPCTSTR param2) 
{
	if (index==1)  
	{
	SendSMS(tel,"<C001>");
	}
	if (index==2)  
	{
	CString command;
	command.Format("<C002,%s,%s>",param1,param2);
	SendSMS(tel,command);
	}
	if (index==3)  
	{
	CString command;
	command.Format("<C003,%s,%s>",param1,param2);
	SendSMS(tel,command);
	}
	
	if (index==4)  
	{
	CString command;
	command.Format("<C004,%s,%s>",param1,param2);
	SendSMS(tel,command);
	}
	if (index==5)  
	{
	CString command="<C005,111>";
	SendSMS(tel,command);
	}
	if (index==6)  
	{
	CString command="<C005,000>";
	SendSMS(tel,command);
	}
	if (index==7)  
	{
	CString command="<C006,FFF>";
	SendSMS(tel,command);
	}
	if (index==8)  
	{
	CString command;
	command.Format("<C007,%s,%s>",param1,param2);
	SendSMS(tel,command);
	}
	if (index==9)  
	{
	CString command;
	command.Format("<C008,%s,%s>",param1,param2);
	SendSMS(tel,command);
	}
	if (index==10)  
	{
	CString command;
	command.Format("<S001,%s,%s>",param1,param2);
	SendSMS(tel,command);
	}
	if (index==11)  
	{
	CString command;
	command.Format("<S003,%s,%s>",param1,param2);
	SendSMS(tel,command);
	}
	if (index==12)  
	{
	CString command;
	command.Format("<S007,%s>",param1);
	SendSMS(tel,command);
	}
	if (index==13)  
	{
	CString command;
	command.Format("<S008,%s>",param1);
	SendSMS(tel,command);
	}
	if (index==14)  
	{
	CString command;
	command.Format("<S009,%s,%s>",param1,param2);
	SendSMS(tel,command);
	}
	if (index==15)  
	{
	CString command;
	command.Format("<S00B,%s>",param1);
	SendSMS(tel,command);
	}
	if (index==16)  
	{
	CString command;
	command.Format("<S00D,%s,%s>",param1,param2);
	SendSMS(tel,command);
	}
}

void CWaveCommCtrl::SiYuanBound(LPCTSTR tel,double minx, double miny, double maxx, double maxy) 
{
	CString a1,b1,a2,b2;
	a1.Format("%d%02.8f",(int)minx,(minx-(int)minx)*60);
	a1.Remove('.');
	a1=a1.Left(9);
	while (a1.GetLength()<9)  a1+="0";

	a2.Format("%d%02.8f",(int)maxx,(maxx-(int)maxx)*60);
	a2.Remove('.');
	a2=a2.Left(9);
	while (a2.GetLength()<9)  a2+="0";

	b1.Format("%d%02.8f",(int)miny,(miny-(int)miny)*60);
	b1.Remove('.');
	b1=b1.Left(8);
	while (b1.GetLength()<8)  b1+="0";

	b2.Format("%d%02.8f",(int)maxy,(maxy-(int)maxy)*60);
	b2.Remove('.');
	b2=b2.Left(8);
	while (b2.GetLength()<8)  b2+="0";

	CString command;
	command.Format("<S00C,%s,%s,%s,%s>",b1,a1,b2,a2);
	SendSMS(tel,command);
}

BYTE CWaveCommCtrl::CharToHex(char c)
{
	BYTE alarmByte;
	if ((c>='0')&&(c<='9'))  alarmByte=c-'0';
	if ((c>='A')&&(c<='F'))  alarmByte=c-'A'+10;
	return alarmByte;
}

void CWaveCommCtrl::DeleteAllSMS()
{
	CArray<BYTE , BYTE> *ps=new CArray<BYTE , BYTE>;
	ps->Add('a');
	ps->Add('t');
	ps->Add('+');
	ps->Add('c');
	ps->Add('m');
	ps->Add('g');
	ps->Add('d');
	ps->Add('=');
	ps->Add('1');
	ps->Add(',');
	ps->Add('4');
	ps->Add('\r');

	m_rSendCommDataArray.Add(ps);

/*
	CString temp;
	int len;
	BYTE Send[200];

	temp="at+cmgd=1,4";
	len=temp.GetLength();
	memset(Send,0,200);
	memcpy(Send,temp.GetBuffer(0),len);
	Send[len]='\r';

	pComm->WriteCommString(Send,len+1);
*/
	SendLast();
}

void CWaveCommCtrl::OnTimer(UINT nIDEvent) 
{
    if (nIDEvent==1)
	{
		m_SentPassedTime++;
		if ((m_SentPassedTime>=20) && (m_rSendCommDataArray.GetSize()>0))
		{////有命令发送未返回
		   IsSending=TRUE;
	       SendRepeat();	
		}
	  
	}
//	COleControl::OnTimer(nIDEvent);
}

void CWaveCommCtrl::SendRepeat()
{
	////////发送最后一条命令
	if (IsSending) return;
	m_SentPassedTime=0;
	m_RepeatTimes++;
	if (m_RepeatTimes>=2) 
	{
	m_RepeatTimes=0;
	//////////报告失败
	SendNext();
	}

	CArray<BYTE , BYTE> *s;
	if (m_rSendCommDataArray.GetSize()<=0) 
		return;

	s=(CArray<BYTE , BYTE> *)m_rSendCommDataArray.GetAt(0);
	BYTE* pb=s->GetData();
	int len=s->GetSize();
	BYTE* sb=(BYTE*)LocalAlloc(LPTR,len);
	memcpy(sb,pb,len);
	IsSending=TRUE;
	m_SentPassedTime=0;
	pComm->WriteCommString(sb,(DWORD)len); 
}

void CWaveCommCtrl::SendNext()
{
	if (IsSending) return;
	m_SentPassedTime=0;
	m_RepeatTimes=0;

	if (m_rSendCommDataArray.GetSize()<=0) return;

	CArray<BYTE , BYTE> *p=(CArray<BYTE , BYTE> *)m_rSendCommDataArray.GetAt(0);
	m_rSendCommDataArray.RemoveAt(0);
	delete p;

    if (m_rSendCommDataArray.GetSize()<=0) return;
	CArray<BYTE , BYTE> *s;
	s=(CArray<BYTE , BYTE> *)m_rSendCommDataArray.GetAt(0);

	BYTE* pb=s->GetData();
	int len=s->GetSize();
	BYTE* sb=(BYTE*)LocalAlloc(LPTR,len);
	memcpy(sb,pb,len);
	IsSending=TRUE;
	m_SentPassedTime=0;
	pComm->WriteCommString(sb,(DWORD)len); 
}

void CWaveCommCtrl::SendLast()
{
	////////发送最后一条命令
	if (IsSending) return;
	m_SentPassedTime=0;
	m_RepeatTimes=1;

	CArray<BYTE , BYTE> *s;

	s=(CArray<BYTE , BYTE> *)m_rSendCommDataArray.GetAt(0);
	BYTE* pb=s->GetData();
	int len=s->GetSize();
	BYTE* sb=(BYTE*)LocalAlloc(LPTR,len);
	memcpy(sb,pb,len);
	IsSending=TRUE;
	m_SentPassedTime=0;
	pComm->WriteCommString(sb,(DWORD)len); 

}

void CWaveCommCtrl::OnDestroy() 
{
	COleControl::OnDestroy();
	
	// TODO: Add your message handler code here
	if (IsOpen) pComm->Stop();
}

⌨️ 快捷键说明

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