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

📄 gkdlseries102dlg.cpp

📁 102规约事例
💻 CPP
📖 第 1 页 / 共 2 页
字号:
				//传输终止否?
				if(my102Class.TransfersFinished(tmpBuf) != 0) 
					return ;
				GetValueFromData(tmpBuf, &recDataLen);
				//写入dat文件
				WriteDataToFl();  	
			}
		}
	}
	CDialog::OnTimer(nIDEvent);
}


BOOL CGKDLSeries102Dlg::DestroyWindow() 
{
	// TODO: Add your specialized code here and/or call the base class
	//关闭串口
	if(my102Class.bCOMOpened == TRUE)	
		my102Class.ClosePort(nPort);
	
	//关闭时钟
	KillTimer(GKDLSENDREV);
    
	myFailNotice->DestroyWindow();
	myFailNotice = NULL;

	return CDialog::DestroyWindow();
}

LRESULT CGKDLSeries102Dlg::DefWindowProc(UINT message, WPARAM wParam, LPARAM lParam) 
{
	// TODO: Add your specialized code here and/or call the base class
	if(message == 133)
	{
#ifndef _DEBUG 
	    ShowWindow(SW_HIDE);
#endif
	}
	return CDialog::DefWindowProc(message, wParam, lParam);
}


//功能:读取配置文件myGKDLConfig.ini中的配置
//节名 config
//项名
//     Elapse
//     Sleep
//     Port  
//     Baud
//     Bit
//     Stop
//     Parity
//节名 Gkdl_Pt
//项名
//     POINT1
//     POINT2
//     POINT3
//       .
//       .
//       .
//     POINT32
void CGKDLSeries102Dlg::ReadConfig(void)
{
	//应用程序全路径
	char exeFullPath[MAX_PATH];
	int tmpPos = 0;
	int pos = 0;

	int len = GetModuleFileName(NULL,exeFullPath, MAX_PATH);
	//配置文件路径
    CString path="\\myGKDLConfig.ini"; 
	CString fullpath(exeFullPath);

    tmpPos = fullpath.Find('\\', tmpPos);
	while(tmpPos != -1)
	{
		pos = tmpPos;
        tmpPos = fullpath.Find('\\', tmpPos+1);		
	}
    
	strcpy(exeFullPath+pos,path);

	nElapse = GetPrivateProfileInt(
		 "config",          //节名
		 "Elapse",          //项名
		 300000,            //没找到此项时返回300000,即5分钟		 		 
		 exeFullPath);
	nSleep = GetPrivateProfileInt(
		 "config",          //节名
		 "Sleep",           //项名
		 3000,              //没找到此项时返回3000,即3秒		 		 
		 exeFullPath);
    nPort = GetPrivateProfileInt(
		 "config",          //节名
		 "Port",            //项名
		 3,                 //没找到此项时返回COM3		 	 
		 exeFullPath);
    nBaud = GetPrivateProfileInt(
		 "config",          //节名
		 "Baud",            //项名
		 12,                //没找到此项时返回12,即9600		 		 
		 exeFullPath);
    nBit = GetPrivateProfileInt(
		 "config",          //节名
		 "Bit",             //项名
		 3,                 //没找到此项的返回3,即8位数据位		 		 
		 exeFullPath);
    nStop = GetPrivateProfileInt(
		 "config",          //节名
		 "Stop",            //项名
		 0,                 //没找到此项时返回0,即1位停止位		 		 
		 exeFullPath);
    nParity = GetPrivateProfileInt(
		 "config",          //节名
		 "Parity",          //项名
		 24,                //没找到此项的返回24,即偶校验even		 
		 exeFullPath);
   
    //读出测点名称,数量
    char temp[50],szBuf[5];
	int i;
	for(i=1; i<50; i++)
	{
		itoa(i,szBuf,10);
		strcpy(temp,"POINT");
		strcat(temp,szBuf);
		GetPrivateProfileString(
			"Gkdl_Pt",
			temp,
			"",
			m_sPointName[i-1],
			50,
			exeFullPath);
		
		if(m_sPointName[i-1][0] == 0) break;
	}
	m_nPointCount = i-1;	
}




//功能:得到电量值;条件:链路状态好 复位链路成功 有class1数据
//                    68 D2 D2 68 28 4B FE 02 1C 05 4B FE 0C   //1C为电量个数
//                             68 28 4B FE 02    05 4B FE 0C   //报文可确定部分
//参数:
//retData    从串口接收到的数据
//length     接收到的报文的长度
//返回值:      
//0       获取数据成功
//1       校验失败
int CGKDLSeries102Dlg::GetValueFromData(BYTE*retData, int *length)
{			
	BYTE sumByte;
	
	//计算校验位
	WORD value = 0;		
	for(int j=4; j<(*length-2); j++)
	{
		value += retData[j];
	}
	sumByte = value & 0x0FF;			
	
	//比较校验位
	if(sumByte != retData[*length-2])
	{
		my102Class.bCJQIsOk = FALSE;
		return 1;		 
	}
	
	//得到数据个数	
	int tmpPointCount = retData[8];

	//如果返回的电量个数多于m_nPointCount个,那么保证只能写入m_nPointCount个
	if(tmpPointCount > m_nPointCount)		
		 tmpPointCount = m_nPointCount;
	
	for(int i=0; i<tmpPointCount; i++)
	{			
		if((13+i*7+4) > (*length-1))
			break;			
        //个位:retData[13 + i*7 + 1];
		//十位:retData[13 + i*7 + 2];
		//百位:retData[13 + i*7 + 3];
		//千位:retData[13 + i*7 + 4];			
		/*对每组数据进行和校验*/
		//计算校验位
		value = retData[5] + retData[6] + retData[7] + retData[12] +
				retData[13+i*7] + retData[13+i*7+1] + retData[13+i*7+2] + retData[13+i*7+3] + retData[13+i*7+4] +
                retData[*length-7] + retData[*length-6] + retData[*length-5] + retData[*length-4] + retData[*length-3];
        sumByte = value & 0x0FF;		
	
		//比较校验位
		if(sumByte != retData[13 + i*7 + 6])
			continue;
        
		//注意:为了防止因为某一块表停止,而导致后面的表的数据前移,最终导致数据错位
		//      也就是说,传了x块表的数据,但是这x块表不是连续的,例如:1 2 3 4 5 7 8
		//      第七块表的数据移到了第六块表上,而第八块表的数据移到了第七块表上
		m_fPointValue[retData[13+i*7]-1] = retData[13+i*7+1] | (retData[13+i*7+2]<<8) | (retData[13+i*7+3]<<16) | (retData[13+i*7+4]<<24);			
	}
	
	return 0;	
}

//功能:写入dat文件
int CGKDLSeries102Dlg::WriteDataToFl()
{
	CTime curTm =  CTime::GetCurrentTime();
	curTm -= CTimeSpan(0, 0, 15+curTm.GetMinute()%5, curTm.GetSecond());
	FILE *fp_in;
	if((fp_in=fopen("data.dat","w"))==NULL)
	{
		return 0;
	}		
	//time_t tm=time(NULL);//获取当前时间
	time_t osBinaryTime = curTm.GetTime();
	for(int i = 0; i<m_nPointCount; i++)
	{
		fprintf(fp_in,"%s,%d,%.3f,,1\n", m_sPointName[i], osBinaryTime, m_fPointValue[i]);
	}
	fclose(fp_in);	
	fp_in = NULL;

	return 1;
}

/*
//按钮:打开并设置串口
void CGKDLSeries102Dlg::OnBtnSetport() 
{
	// TODO: Add your control notification handler code here
	
	if(my102Class.bCOMOpened == FALSE)
	{
		if(my102Class.OpenPort(nPort, nBaud, nBit, nStop, nParity)==0)
			my102Class.bCOMOpened = TRUE;
	}	
}
*/

/*
//按钮:读取串口设置
void CGKDLSeries102Dlg::OnBtnReadset() 
{
	// TODO: Add your control notification handler code here
	
	int myBaud;
	int myStopbit;
	int myBit;
	char myParity[6];
    
	int ret;
	ret = sio_getbaud ( nPort ); //返回值为实际的波特率,如9600,200,38400
    if (ret < 0){	    
		return ;
	}
    myBaud = ret;

	ret = sio_getmode ( nPort );
    if (ret < 0) {	    
		return ;
	}
	else{
		//获取停止位的位数
		switch( ret & 0x04 )
		{
		case 0x00 :// stop bit = 1 
            myStopbit = 1;
			break;
		case 0x04: // stop bit = 2 
			myStopbit = 2;
			break;
		}

		//获取数据位的位数
		switch( ret & 0x03 )
		{
		case 0x00 :// bit = 5 
            myBit = 5;
			break;
		case 0x01: // bit = 6 
			myBit = 6;
			break;
		case 0x02 :// bit = 7 
            myBit = 7;
			break;
		case 0x03: // bit = 8 
			myBit = 8;
			break;
		}
        
		//获取校验方式
		switch( ret & 0x38 )
		{
		case 0x00 :// none 
            strcpy(myParity, "none");
			break;
		case 0x08: // odd  
			strcpy(myParity, "odd");
			break;
		case 0x18 :// even  
            strcpy(myParity, "even");
			break;
		case 0x28: // mark  
			strcpy(myParity, "mark");
			break;
		case 0x38: // space 
			strcpy(myParity, "space");
			break;
		}		
	}
    CString str;
	str.Format("%d,%s,%d,%d", myBaud, myParity, myBit, myStopbit);
	SetDlgItemText(IDC_PORTSET,(LPCTSTR)str);	
}
*/

/*
//注意:IDC_EDITSEND	
//     每次最多只能发送2000个字符
//按钮:发送数据
void CGKDLSeries102Dlg::OnBtnSend() 
{
	// TODO: Add your control notification handler code here	
	SendRequest(3, NULL);				
}
*/


/*
//注意:IDC_EDITRECEIVE
//     每次最多只能接收2000个字符
//按钮:接收数据
void CGKDLSeries102Dlg::OnBtnReceive() 
{
	// TODO: Add your control notification handler code here				
}
*/


/*
//按钮:重新读取配置文件
void CGKDLSeries102Dlg::OnBtnReadconfig() 
{
	// TODO: Add your control notification handler code here
	ReadConfig();	
}
*/


/*
//按钮:关闭串口
void CGKDLSeries102Dlg::OnBtnCloseport() 
{
	// TODO: Add your control notification handler code here	
	if(my102Class.bCOMOpened == TRUE)
	{
		if(my102Class.ClosePort(nPort)==0)
			my102Class.bCOMOpened = FALSE;
	}
}
*/

⌨️ 快捷键说明

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