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

📄 mainfrm.cpp

📁 使用VC编写的GPRS通讯代码,可用于GPRS MODEM收发数据
💻 CPP
📖 第 1 页 / 共 2 页
字号:
	{
		sprintf(mess,"数据中心服务启动失败!\r\n");
		this->AddTextToEditView(mess,false);
	}
}

void CMainFrame::OnMistopservice() 
{
	// TODO: Add your command handler code here
	char mess[512];

	if (m_option.m_bSysAutoPoll)
		this->KillTimer(1);

	//this->AddTextToEditView(mess);
	if(!DSStopService())
	{
		DSGetLastError(mess,sizeof(mess));
	}
	else
	{
		sprintf(mess,"停止数据中心服务成功!\r\n");
	}
	
	this->AddTextToEditView(mess,false);

	StopRecvThread();

	this->m_bServerRunning=false;

	this->GetRightPane()->ClearAllItem();
	this->GetLeftPane()->ClearAllItem();
}

void CMainFrame::OnQuit() 
{
	// TODO: Add your command handler code here
	this->OnClose();	
}

void CMainFrame::OnClose() 
{
	// TODO: Add your message handler code here and/or call default
	if (IDYES==MessageBox("确定要退出吗?","退出",MB_YESNO | MB_ICONINFORMATION))
	{
		if (m_bServerRunning)
		{
			this->OnMistopservice();
		}

		CFrameWnd::OnClose();
	}
}

UINT CMainFrame::ProcessRvData(LPVOID lparam)
{
	CMainFrame* pMainFrm=(CMainFrame*)lparam;
	ModemDataStruct modemdata;
	static char buf[2048];
	while(1)
	{
		if (DSGetNextData(&modemdata,0)!=FALSE)
		{
			if (modemdata.m_data_len>0)
			{
				//whether show head
				if (pMainFrm->m_option.m_bShowHead)
				{
					struct tm when;
					when = *localtime((time_t*)&modemdata.m_recv_time);
					
					//					sprintf(buf,"\r\nuserid:%s,time:%s,len:%d",
					//						modemdata.m_modemId,asctime(&when),modemdata.m_data_len);
					sprintf(buf,"\r\nuserid:%8.8x,time:%s,len:%d",
						modemdata.m_modemId,asctime(&when),modemdata.m_data_len);
					pMainFrm->AddTextToEditView(buf);
				}
				
				//Hex or not to show data
				modemdata.m_data_buf[modemdata.m_data_len]=0;
				if (pMainFrm->m_option.m_bHexShow)
				{
					pMainFrm->PrintData((char*)modemdata.m_data_buf);
				}
				else
				{
					pMainFrm->AddTextToEditView((char*)modemdata.m_data_buf);
				}
			}
		}
		else
		{
			break;
		}
	}	//endof while(1)

	return 1;
}

void CMainFrame::AddTextToEditView(char * szContent,bool newline)
{	
	if (szContent)
	{
	    CString text;
	    CEdit &ce=GetBottompane()->GetEditCtrl();

	    if (ce.GetLineCount()>100)
		    ce.SetWindowText("");

		ce.GetWindowText(text);
		if (newline)
			ce.SetWindowText(text+"\r\n"+szContent);
		else
			ce.SetWindowText(text+szContent);

		ce.LineScroll(ce.GetLineCount());
	}	
}

void CMainFrame::AddTextToEditView(int iContent,bool newline)
{
	CString text;
	char buf[32];

	sprintf(buf,"%d",iContent);
    CEdit &ce=this->GetBottompane()->GetEditCtrl();

	if (ce.GetLineCount()>100)
		ce.SetWindowText("");

	ce.GetWindowText(text);
	if (newline)
		ce.SetWindowText(text+"\r\n"+buf);
	else
		ce.SetWindowText(text+buf);

	ce.LineScroll(ce.GetLineCount());
}

void CMainFrame::RefreshUserTable(int flag)
//flag=1 means refresh usertable , 0 means not
//flag==1:called by timer
//flag==0:called by manual
{
	int i,iMaxDTUAmount;
	ModemInfoStruct mi;
	bool   bShow;
	time_t t_now,t_update;

	this->GetRightPane()->ClearAllItem();
	this->GetLeftPane()->ClearAllItem();

	iMaxDTUAmount=DSGetModemCount();
	TRACE("TIMER:MAX MODEM COUNT = %d.\n",iMaxDTUAmount);
	for (i=0;i<iMaxDTUAmount;i++)
	{
		DSGetModemByPosition(i,&mi);
		bShow=true;

		if ((m_option.m_bSysAutoPoll) && (1==flag))
		{
				/* if type of update time is ulong then use */
				/* the following code                       */

				t_now=time(NULL);
				t_update=mi.m_refresh_time;
				if ((t_now-t_update)>=m_option.m_iOffLineTime)
				{
					bShow=false;
				}                
		}

		if (bShow)
		{
				//add to ListView of right top
				this->GetRightPane()->InsertUserItem(
					&mi);

				//add to TreeView of left
				CString str;
				str.Format("%8.8x",mi.m_modemId);
				GetLeftPane()->InsertUserItem(str.GetBuffer(1));
		}
	}

	this->GetLeftPane()->GetTreeCtrl().Expand(
		this->GetLeftPane()->GetTreeCtrl().GetFirstVisibleItem(),TVE_EXPAND);
}

void CMainFrame::OnMianswer() 
{
    m_bAnswer=!m_bAnswer;	
}

void CMainFrame::OnMiserversetting() 
{
	this->GetPara(0);
}

void CMainFrame::OnMishowdata() 
{
	this->GetPara(1);
}

void CMainFrame::OnShowmenu() 
{
    ShowControlBar(&m_wndMenuBar,!m_wndMenuBar.IsWindowVisible(),FALSE);
	m_wndToolBar.SetButtonStyle(5,TBSTYLE_BUTTON);
}

void CMainFrame::OnHelpcontent() 
{
	MessageBox("厦门桑荣科技技术部");
}

void CMainFrame::SetStatusBarStyle()
{
	m_wndStatusBar.SetPaneInfo(0,ID_SEPARATOR,SBPS_STRETCH,100);  
	m_wndStatusBar.SetPaneText(1,"",TRUE);		
	m_wndStatusBar.SetPaneText(2,"数据中心软件DEMO软件",TRUE);
}

void CMainFrame::OnTimer(UINT nIDEvent) 
{
	// TODO: Add your message handler code here and/or call default
	if (1==nIDEvent)
		this->RefreshUserTable(1);

	if (3==nIDEvent)
		ProcessRvData(this);

	CFrameWnd::OnTimer(nIDEvent);
}

void CMainFrame::PrintData(char *szData)
{
	char buf[128];

	int i,j,len,totallen;

	totallen=strlen(szData);
	for (i=0;i<totallen;i+=16)
	{
		len=(totallen-i)>16?16:(totallen-i);
		sprintf(buf,"%04X  :  ",i);
		for (j=0;j<len;j++)
		{
			sprintf(buf+9+j*3,"%02X ",szData[i+j]&0xFF);
			buf[64+j]=szData[i+j];
		}
        memset(buf+len*3+9,' ',64-len*3-9);
		buf[64+len]='\0';

		this->AddTextToEditView(buf);
	}
}

void CMainFrame::OnUpdateMistartservice(CCmdUI* pCmdUI) 
{
	// TODO: Add your command update UI handler code here
	pCmdUI->Enable(!m_bServerRunning);
}

void CMainFrame::OnUpdateMistopservice(CCmdUI* pCmdUI) 
{
	// TODO: Add your command update UI handler code here
	pCmdUI->Enable(m_bServerRunning);
}

void CMainFrame::OnUpdateMianswer(CCmdUI* pCmdUI) 
{
	// TODO: Add your command update UI handler code here
	pCmdUI->SetCheck(m_bAnswer);	
}

void CMainFrame::init_option(LPOPTION pOption)
{
	pOption->m_bSaveToLog=FALSE;
	pOption->m_iLogItemCount=200;
	pOption->m_bShowDataHead=TRUE;
	pOption->m_iServerPort=5001;
	pOption->m_bSysAutoPoll=TRUE;
	pOption->m_iPollTimeInterval=5;
	pOption->m_iOffLineTime=120;
	pOption->m_bShowHead=TRUE;
	pOption->m_bHexShow=TRUE;
}

void CMainFrame::GetPara(int tabindex)
{
	CSettingDlg sdlg(NULL,tabindex,!m_bServerRunning);

	sdlg.m_bSaveToLog=m_option.m_bSaveToLog;
	sdlg.m_iLogItemCount.Format("%d",m_option.m_iLogItemCount);
	sdlg.m_iOffLineTime.Format("%d",m_option.m_iOffLineTime);
	sdlg.m_iPollTime.Format("%d",m_option.m_iPollTimeInterval);
	sdlg.m_iServerPort.Format("%d",m_option.m_iServerPort);
	sdlg.m_bSysAutoPoll=m_option.m_bSysAutoPoll;
	sdlg.m_bShowHead=m_option.m_bShowHead;
	sdlg.m_bHexShow=m_option.m_bHexShow;

	if (IDOK==sdlg.DoModal())
	{
		m_option.m_bSaveToLog=sdlg.m_bSaveToLog;
		m_option.m_iLogItemCount=atoi(sdlg.m_iLogItemCount.GetBuffer(0));
		m_option.m_iOffLineTime=atoi(sdlg.m_iOffLineTime.GetBuffer(0));
		m_option.m_iPollTimeInterval=atoi(sdlg.m_iPollTime.GetBuffer(0));
		m_option.m_iServerPort=atoi(sdlg.m_iServerPort.GetBuffer(0));
		m_option.m_bSysAutoPoll=sdlg.m_bSysAutoPoll;
	    m_option.m_bShowHead=sdlg.m_bShowHead;
	    m_option.m_bHexShow=sdlg.m_bHexShow;
	}
}

void CMainFrame::SetUserId(char *szUserId)
{
	strncpy(this->m_userid,szUserId,11);
	this->m_userid[11]='\0';
}

void CMainFrame::OnUpdateShowmenu(CCmdUI* pCmdUI) 
{
    pCmdUI->SetCheck(m_wndMenuBar.IsWindowVisible());
}


void CMainFrame::OnMiofflineall() 
{
/*
    do_close_all_user(NULL);
	this->RefreshUserTable();
*/
}


void CMainFrame::OnMiviewword() 
{
	// TODO: Add your command handler code here
    if (!IsWindowVisible()) 
		return;

	CRect rect; 

	m_viewword=!m_viewword;
    m_wndToolBar.SetShowWord(m_viewword);
    m_wndToolBar.GetItemRect(0,&rect); 
	if (m_viewword)
	{
	    m_wndToolBar.SetSizes(CSize(SHOWTEXTLEN,rect.Size().cy),CSize(32,32));
	}
	else
	{
        m_wndToolBar.SetSizes(CSize(NOTEXTLEN,rect.Size().cy),CSize(32,32));
	}
	ShowControlBar(&m_wndToolBar,TRUE,FALSE);
}

void CMainFrame::OnUpdateMiviewword(CCmdUI* pCmdUI) 
{
	pCmdUI->Enable(m_wndToolBar.IsWindowVisible());
    pCmdUI->SetCheck(m_viewword);
}

BOOL CMainFrame::IsShowToolBarWord()
{
	return m_viewword;
}

int CMainFrame::CompareDateTimeAndNow(char * szDateTime)
{
    int nYear,nMonth,nDay,nHour,nMin,nSec;
	char temp[20];

    memset(temp,0,20);
	strncpy(temp,szDateTime,4);
	nYear = atoi(temp);

	memset(temp,0,20);
	strncpy(temp,&(szDateTime[5]),2);
	nMonth = atoi(temp);

	memset(temp,0,20);
	strncpy(temp,&(szDateTime[8]),2);
	nDay = atoi(temp);

	memset(temp,0,20);
	strncpy(temp,&(szDateTime[11]),2);
	nHour = atoi(temp);

	memset(temp,0,20);
	strncpy(temp,&(szDateTime[14]),2);
	nMin = atoi(temp);

	memset(temp,0,20);
	strncpy(temp,&(szDateTime[17]),4);
	nSec = atoi(temp);

	CTime updatetime(nYear,nMonth,nDay,nHour,nMin,nSec);
	time_t stUpdatetime = updatetime.GetTime();
	CTime currenttime = CTime::GetCurrentTime();

    return (currenttime.GetTime()-stUpdatetime);
}

BOOL CMainFrame::StartRecv(void)
{
	SetTimer(3,100,NULL);		//500ms every time.
	return TRUE;

}

void CMainFrame::StopRecvThread()
{
	KillTimer(3);
}

⌨️ 快捷键说明

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