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

📄 testdlg.cpp

📁 一个canbus通讯软件源代码
💻 CPP
📖 第 1 页 / 共 3 页
字号:
{
	
	// TODO: Add your control notification handler code here
	DWORD wordtemp=m_EditSendFrmID;
	UpdateData(true);
	if (m_EditSendFrmID>2000)
	{
		m_EditSendFrmID=wordtemp;
		UpdateData(false);
	}
}



BOOL CTestDlg::DestroyWindow() 
{
	// TODO: Add your specialized code here and/or call the base class
	KillTimer(1);
	UpdateData(true);
    CStringArray   strarray;   
    CFile file;
    CFileException fe;   
    CString      filepath;
	char   szBuffer[MAX_PATH];
    int    itemp;
    GetModuleFileName(AfxGetInstanceHandle(), szBuffer, MAX_PATH); 
	filepath=szBuffer;
	itemp=filepath.ReverseFind('\\');
    filepath.Format("%s\\config.set",filepath.Left(itemp));
	   //以读方式打开文件
   if(!file.Open(filepath,CFile::modeWrite|CFile::modeCreate,&fe))
        {
                fe.ReportError();
                return ClxDialog::DestroyWindow();
        }
        
        //构建CArchive 对象
   CArchive ar(&file,CArchive::store);



        Serialize(ar);
        ar.Flush();
        //写完毕,关闭文件流
        ar.Close();
        file.Close();



	m_DBCnt->Close;
   
   // m_DBCnt->Release();
	return ClxDialog::DestroyWindow();
}




void CTestDlg::OnButtonSetplus() 
{
	// TODO: Add your control notification handler code here


		unsigned char cInterval[8]={0,0};
		
		cInterval[0]=2;
		cInterval[1]=31-m_ctrReaderPlus.GetCurSel();
		
    if(m_connect==0)
    	return;
	VCI_CAN_OBJ frameinfo;
	UpdateData(true);
	memcpy(&frameinfo.Data,cInterval,8);
	frameinfo.DataLen=8;
	frameinfo.RemoteFlag=0;
	frameinfo.ExternFlag=1;
	DWORD dwordFormat=2;

	frameinfo.ID=(m_EditSendFrmID<<18)+((DWORD)dwordFormat);
	
	
	frameinfo.SendType=m_ComboSendType.GetCurSel();
    CString strtemp;

	
	if(VCI_Transmit(m_devtype,0,m_cannum,&frameinfo,1)==1)
	{
	
	
	
			strtemp.Format("请求设置:%d增益为%d.....发送成功",m_EditSendFrmID,m_ctrReaderPlus.GetCurSel());
		
		
		ShowInfo(strtemp,0);		
	}
	else
	{
		
			strtemp.Format("请求设置:%d增益为%d.....发送失败",m_EditSendFrmID,m_ctrReaderPlus.GetCurSel());
	
		
		ShowInfo(strtemp,0);	
	}
}

void CTestDlg::OnButtonGetplus() 
{
	// TODO: Add your control notification handler code here
	unsigned char cInterval[8]={0};
		
		cInterval[0]=3;
		
		
    if(m_connect==0)
    	return;
	VCI_CAN_OBJ frameinfo;
	UpdateData(true);
	memcpy(&frameinfo.Data,cInterval,8);
	frameinfo.DataLen=8;
	frameinfo.RemoteFlag=0;
	frameinfo.ExternFlag=1;
	DWORD dwordFormat=2;

	frameinfo.ID=(m_EditSendFrmID<<18)+((DWORD)dwordFormat);
	
	
	frameinfo.SendType=m_ComboSendType.GetCurSel();
    CString strtemp;
	
	if(VCI_Transmit(m_devtype,0,m_cannum,&frameinfo,1)==1)
	{
	
	
	
			strtemp.Format("读取:%d增益.....发送成功",m_EditSendFrmID);
		
		
		ShowInfo(strtemp,0);		
	}
	else
	{
		
			strtemp.Format("读取:%d增益.....发送失败",m_EditSendFrmID);
	
		
		ShowInfo(strtemp,0);	
	}	

}


void CTestDlg::OnButtonGetmode() 
{
	// TODO: Add your control notification handler code here
	unsigned char cInterval[8]={0};
		
		cInterval[0]=1;
		
		
    if(m_connect==0)
    	return;
	VCI_CAN_OBJ frameinfo;
	UpdateData(true);
	memcpy(&frameinfo.Data,cInterval,8);
	frameinfo.DataLen=8;
	frameinfo.RemoteFlag=0;
	frameinfo.ExternFlag=1;
	DWORD dwordFormat=2;

	frameinfo.ID=(m_EditSendFrmID<<18)+((DWORD)dwordFormat);
	
	
	frameinfo.SendType=m_ComboSendType.GetCurSel();
    CString strtemp;

	if(VCI_Transmit(m_devtype,0,m_cannum,&frameinfo,1)==1)
	{
	
	
	
			strtemp.Format("读取:%d工作模式请求.....发送成功",m_EditSendFrmID);
		
		
		ShowInfo(strtemp,0);		
	}
	else
	{
		
			strtemp.Format("读取:%d工作模式请求.....发送失败",m_EditSendFrmID);
	
		
		ShowInfo(strtemp,0);	
	}	
}



DWORD CTestDlg::ReverseDword(DWORD cstwordID)
{

	DWORD tempword=0,wordChang;//目标变量 
 	DWORD sign=1; 
	CString tempstr;
    int tempint=0;
 	for (int i=0;i<=31;i++) 
 	{ 
 		tempword+=((cstwordID&(sign<<i))>>i)<<(31-i); 
 	} 
	
    for (i=1;i<32;i++)
    {
		wordChang=tempword & 1;
		if (wordChang==1)
		{
			break;
		}
		tempword=tempword>>1;
    }
 	return tempword;
}

void CTestDlg::OnButtoncls() 
{
	// TODO: Add your control notification handler code here
KillTimer(1);
	critical_section.Lock();
	m_ReceiveInfo.RemoveAll();
	critical_section.Unlock();
	m_ctrTagShow.DeleteAllItems();
	SetTimer(1,1000,NULL);	
	
		
}


HBRUSH CTestDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor) 
{
	HBRUSH hbr = ClxDialog::OnCtlColor(pDC, pWnd, nCtlColor);
	
	// TODO: Change any attributes of the DC here
	
	// TODO: Return a different brush if the default is not desired

 if(pWnd->GetDlgCtrlID()==IDC_STATIC_LOGAL)//我假设你的文本控件ID为IDC_EDIT1
     pDC->SetTextColor(RGB(215,0,0));
	return hbr;
}


void CTestDlg::TranslateReceiveInfo()//把接收到的数据转化为数据里面。
{
   bool blAddInfo,blAddInfoSQL;
   CReceiveInfo receiveinfotemp;
   CReceiveSQLDate receiveSQLdatetemp;
   //VCI_CAN_OBJ tempobj;
   for (int i=0;i<m_ReceiveInfoThread.GetSize();i++)
   {
			 blAddInfoSQL=true;
	          blAddInfo=true;
				for (int ia=0;ia<m_ReceiveInfo.GetSize();ia++)
				{
                     
						if (m_ReceiveInfo[ia].FindAndChange(m_ReceiveInfoThread[i]))// 查找是不是已经在数据内部
						{
						
												
							blAddInfo=false;
							break;
						}
                    
					
				}

				if (blAddInfo) //如果不存在则添加在收到信息内
				{
              
			   
					receiveinfotemp.SetInfo(m_ReceiveInfoThread[i]);
					m_ReceiveInfo.Add(receiveinfotemp);

				}
				receiveinfotemp.SetInfo(m_ReceiveInfoThread[i]);
				for (int ib=0;ib<m_receiveSQLDate.GetSize();ib++)
				{
					
					if (m_receiveSQLDate[ib].m_strTagID.Compare(receiveinfotemp.m_strTagID)==0)
					{
						m_receiveSQLDate[ib].AddTheReaderID(receiveinfotemp.m_dwordReaderID,receiveinfotemp.m_nstate);
						
						 blAddInfoSQL=false;
						break;
					}
				}
				if (blAddInfoSQL)
				{
				
					receiveSQLdatetemp.m_strTagID=receiveinfotemp.m_strTagID;
					receiveSQLdatetemp.m_time_last=CTime::GetCurrentTime();
					receiveSQLdatetemp.m_time_first=receiveSQLdatetemp.m_time_last;
					receiveSQLdatetemp.m_time_first_last=receiveSQLdatetemp.m_time_last;
					receiveSQLdatetemp.m_boolIsChange=true;
					receiveSQLdatetemp.m_boolIsRecieve=true;
					receiveSQLdatetemp.m_boolfistadd=true;
					receiveSQLdatetemp.m_dwordReaderID_first=receiveinfotemp.m_dwordReaderID;
					receiveSQLdatetemp.m_dwordReaderID_last=receiveinfotemp.m_dwordReaderID;
					m_receiveSQLDate.Add(receiveSQLdatetemp);
					
				}
			



   }
   m_ReceiveInfoThread.RemoveAll();
}

void CTestDlg::OnColumnclickListTagshow(NMHDR* pNMHDR, LRESULT* pResult) 
{
	NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)pNMHDR;
	CReceiveInfo *tempReceiveinfo=m_ReceiveInfo.GetData();
	// TODO: Add your control notification handler code here
	if (pNMListView->iSubItem==2)
	{
		
		qsort(tempReceiveinfo,m_ReceiveInfo.GetSize(),sizeof(m_ReceiveInfo[0]),sort_function);
		 m_ctrTagShow.DeleteAllItems();
		 m_blNewTag=true;
		 SetTimer(1,100,NULL);
		 SetTimer(2,500,NULL);
	}
    else if(pNMListView->iSubItem==1)
	{
		
		qsort(tempReceiveinfo,m_ReceiveInfo.GetSize(),sizeof(m_ReceiveInfo[0]),sort_functionID);
		 m_ctrTagShow.DeleteAllItems();
		 m_blNewTag=true;
		 SetTimer(1,100,NULL);
		 SetTimer(2,500,NULL);
	}
			
				
				//	if (i+1<m_ctrTagShow.GetItemCount())
				
	
	*pResult = 0;
}

void CTestDlg::OnItemclickListTagshow(NMHDR* pNMHDR, LRESULT* pResult) 
{
	HD_NOTIFY *phdn = (HD_NOTIFY *) pNMHDR;
	// TODO: Add your control notification handler code here
	
	*pResult = 0;
}

void CTestDlg::OnKeydownListTagshow(NMHDR* pNMHDR, LRESULT* pResult) 
{
	LV_KEYDOWN* pLVKeyDow = (LV_KEYDOWN*)pNMHDR;
	// TODO: Add your control notification handler code here
	
	*pResult = 0;
}
void CTestDlg::WriteErroLog(CString conststrerro)
{
		CStdioFile   file;
		CString      filepath;
		CString   filestr;
		CString  strtemp;
		char   szBuffer[MAX_PATH];
		int    itemp;
         CTime  TimeTemp;
		 TimeTemp=CTime::GetCurrentTime();
		 GetModuleFileName(AfxGetInstanceHandle(), szBuffer, MAX_PATH); 
		filepath=szBuffer;
		itemp=filepath.ReverseFind('\\');
		filepath.Format("%s\\Erro.txt",filepath.Left(itemp));
		if(   !file.Open(filepath,CFile::modeReadWrite) )  
		{   
			if (!file.Open(filepath,CFile::modeCreate|CFile::modeWrite))
			{
				AfxMessageBox("can   not   open   file!");   
			 return; 
			}
			   
		}   
		strtemp=TimeTemp.Format( "%c" );

        filestr.Format("%sDay:%s\n\n\n",conststrerro,strtemp);
	    file.SeekToEnd();
		file.WriteString(filestr);   
  
		 file.Close();

}




void CTestDlg::OnButIdset() 
{
	// TODO: Add your control notification handler code here
   
	CDlgPollingMod dlgpollingtemp;
	dlgpollingtemp.m_wordInterval=m_wordInterval;
	
	if (dlgpollingtemp.DoModal()==IDOK)
	{
		m_wordInterval=dlgpollingtemp.m_wordInterval;
	}
	



}

void CTestDlg::OnButStartPolling() 
{
	// TODO: Add your control notification handler code here
	UpdateData(true);
	CButton * pbutton=(CButton*)GetDlgItem(IDC_BUT_START_POLLING);
	CString  strbutID;
	pbutton->GetWindowText(strbutID);
	if (0==strbutID.Compare("开  始"))
	{
		SetTimer(3,m_wordInterval,NULL);
		pbutton->SetWindowText("暂  停");
		return;
	}
	pbutton->SetWindowText("开  始");
	KillTimer(3);
	
}


void CTestDlg::initializationSQL()//初使化SQL数据库变量以便接收
{
	for (int i=0;i<m_receiveSQLDate.GetSize();i++)
	{
		m_receiveSQLDate[i].InitializationDate();
	}

}

BOOL CTestDlg::TranslateSQLDate()
{
   
    CString strtemp,strtemp_tagID;
  
  	LONG long_temp_sum;
	CTimeSpan time_spantime;
	CTime   temp_time;
	CString sql_;
	_bstr_t strSQL;
	for (int i=0;i<m_receiveSQLDate.GetSize();i++)
		m_receiveSQLDate[i].SureTheDate();
 	critical_section.Lock();
	try
	{
			for ( i=0;i<m_receiveSQLDate.GetSize();i++)
			{	


				if (m_receiveSQLDate[i].m_boolIsRecieve)
				{
					if (m_receiveSQLDate[i].m_boolIsChange)
					{
						sql_.Format("update Recorder set IsChange=0 where TagID='%s'",m_receiveSQLDate[i].m_strTagID);
						strSQL=sql_;
						m_DBCnt->Execute(strSQL, NULL, adCmdText);
						temp_time=m_receiveSQLDate[i].m_time_first;
			        	strtemp.Format("%d-%d-%d %d:%d:%d",temp_time.GetYear(),temp_time.GetMonth(),temp_time.GetDay(),temp_time.GetHour(),temp_time.GetMinute(),temp_time.GetSecond());
						if (m_receiveSQLDate[i].m_bool_state)
						{
							sql_.Format("INSERT INTO Recorder (ReaderID,TagID,State,Firsttime,[Interval],[Count],[IsChange]) VALUES('%ld','%s','1','%s','0','1','1')",
							m_receiveSQLDate[i].m_dwordReaderID_first,m_receiveSQLDate[i].m_strTagID,strtemp);
						} 
						else
						{
							sql_.Format("INSERT INTO Recorder (ReaderID,TagID,State,Firsttime,[Interval],[Count],[IsChange]) VALUES('%ld','%s','0','%s','0','1','1')",
							m_receiveSQLDate[i].m_dwordReaderID_first,m_receiveSQLDate[i].m_strTagID,strtemp);
						}
						
						strSQL=sql_;
						m_DBCnt->Execute(strSQL, NULL, adCmdText);
					} 
					else
					{
						time_spantime=m_receiveSQLDate[i].m_time_last-m_receiveSQLDate[i].m_time_first;
						long_temp_sum = time_spantime.GetTotalSeconds();
						strtemp_tagID=m_receiveSQLDate[i].m_strTagID;
						sql_.Format("update Recorder set [Count]='%ld',State='%d',[Interval]='%ld' where IsChange<>0 and TagID='%s'",m_receiveSQLDate[i].m_longCount,m_receiveSQLDate[i].m_bool_state,long_temp_sum,strtemp_tagID);
					    //strtemp.Format("%s and TagID='%s'",sql_,strtemp_tagID);
					//	sql_=strtemp;
					 	strSQL=sql_;
			 			m_DBCnt->Execute(strSQL, NULL, adCmdText);

					}
				}
			}
				
	
	

	}
	catch(_com_error& e)
	{
		
       	AfxMessageBox(e.ErrorMessage());
		
		//return;
    } 

critical_section.Unlock();



	return true;
}



CString CTestDlg::GetTheSQLConnect()
{
	CString   m_strConnectString;  //返回的链接字符串 
 
 HRESULT   hr   =   S_OK;   
 MSDASC::IDataSourceLocatorPtr   ptrDataSourceWnd   =   NULL;   
 hr   =   ptrDataSourceWnd.CreateInstance(__uuidof(MSDASC::DataLinks));   
 if(FAILED(hr))   
  _com_issue_error(hr);  

 hr   =   ptrDataSourceWnd->put_hWnd((long)this->m_hWnd);   
 if(FAILED(hr))   
  _com_issue_error(hr);   
 
 IDispatchPtr   ptrDispatch   =   NULL;   
 hr   =   ptrDataSourceWnd->PromptNew(&ptrDispatch);   

 if(FAILED(hr))   
  _com_issue_error(hr);   
 
 if(ptrDispatch   !=   NULL)   
 {   
  _ConnectionPtr   m_Conn=     ptrDispatch;   
        BSTR   bstrConnectString   =m_Conn->ConnectionString;   
	

 m_strConnectString=bstrConnectString;
 return m_strConnectString;//成功
   
 } 

}

void CTestDlg::ConnectTheSQLDate()
{
	 try
    {
		m_DBCnt.CreateInstance(__uuidof(Connection));
	
	//	sql_.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=%s",filepath);

		_bstr_t sql=m_strConnect_name;
	 m_DBCnt->Open(sql, "", "", adModeUnknown);	
	 sql="update Recorder set IsChange=0 where IsChange<>0 ";
	  m_DBCnt->Execute(sql, NULL, adCmdText);	
    }
    catch (CException* e)
    {
    	AfxMessageBox("找不到数据库或者找不到数据库文件");
	
		return ;
    }

}

⌨️ 快捷键说明

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