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

📄 opcclientdoc.cpp

📁 opc的客户端程序,在csdn上下的,不过我想看看这里下的详细说明
💻 CPP
📖 第 1 页 / 共 2 页
字号:
			SetModifiedFlag(TRUE);
		}
	}
}

void OPCClientDoc::OnUpdateOpcGroup(CCmdUI* pCmdUI) 
{
	// TODO: Add your command update UI handler code here
	pCmdUI->Enable(current_server && current_server->is_connected());
}

void OPCClientDoc::OnOpcItem() 
{
	// TODO: Add your command handler code here
	CItemDialog dialog(current_server->get_opc_server());
	if(dialog.DoModal() == IDOK){
		SetModifiedFlag(TRUE);
	}
}

void OPCClientDoc::OnUpdateOpcItem(CCmdUI* pCmdUI) 
{
	// TODO: Add your command update UI handler code here
	if(current_server == NULL){
		pCmdUI->Enable(false);
		return;
	}

	COPCGroup* group = current_server->get_current_group();
	if(group == NULL){
		pCmdUI->Enable(false);
		return;
	}
	
	pCmdUI->Enable(group->opc_group.IsOk());
}



void OPCClientDoc::OnOpcConnect() 
{
	// TODO: Add your command handler code here
	if(current_server && !current_server->is_connected())
		current_server->connect();
}

void OPCClientDoc::OnUpdateOpcConnect(CCmdUI* pCmdUI) 
{
	// TODO: Add your command update UI handler code here
	pCmdUI->Enable(!current_server->is_connected());
}

void OPCClientDoc::OnOpcDisconnect() 
{
	// TODO: Add your command handler code here
	
}

void OPCClientDoc::OnUpdateOpcDisconnect(CCmdUI* pCmdUI) 
{
	// TODO: Add your command update UI handler code here
	pCmdUI->Enable(FALSE);
}

//导出配置文件
void OPCClientDoc::OnFileExport() 
{
	// TODO: Add your command handler code here
	CFileDialog dialog(
		false, 
		NULL, 
		_T("cmx_opcclient_items.txt"),
		OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, 
		_T("CMX_OPCClient Configure File(*.txt)|*.txt|All Files (*.*)|*.*||"),
		NULL
		);
	if(dialog.DoModal() == IDOK){
		CString file_path_name = dialog.GetPathName();
		if(file_path_name.IsEmpty())
			return;
		
		CFile file;
		if(!file.Open(file_path_name, CFile::modeCreate | CFile::modeWrite))
			return;
		
		file.SeekToEnd();
		
		char* desc[10];
		desc[0] = "//*********************************************************//\r\n";
		desc[1] = "//*             CMX_OPCClient items configrue file        *//\r\n";
		desc[2] = "//*             Chen Maoxiang                             *//\r\n";
		desc[3] = "//*             EMail:chenmaoxiang@gmail.com              *//\r\n";
		desc[4] = "//*             MSN:cubeboxstudio@yahoo.com.cn            *//\r\n";
		desc[5] = "//*             QQ:125180837                              *//\r\n";
		desc[6] = "//*********************************************************//\r\n";
		
		
		for(int i = 0; i < 7; i++)
			file.Write(desc[i], strlen(desc[i]));
		
		char sz_data[MAX_PATH];

		Lock wait(&item_cs);
		POSITION server_pos = servers.GetHeadPosition();
		while(server_pos){
			COPCServer* server = servers.GetNext(server_pos);
			if(!server)
				continue;

			OPCServerInfo* info = server->get_server_info();
			ASSERT(info);
			CString node_name = info->m_NodeName;
			if(node_name.IsEmpty())
				node_name = _T("localhost");
			sprintf(sz_data, "\r\nNode Name: \t%s\r\n", node_name.GetBuffer(0));
			file.Write(sz_data, strlen(sz_data));
			memset(sz_data, 0, sizeof(sz_data));

			sprintf(sz_data, "Server Name: \t%s\r\n", info->m_ProgID.GetBuffer(0));
			file.Write(sz_data, strlen(sz_data));
			memset(sz_data, 0, sizeof(sz_data));

			sprintf(sz_data, "CLSID: \t\t{0X%x,0X%x,0X%x,{0X%02x,0X%02x,0X%02x,0X%02x,0X%02x,0X%02x,0X%02x,0X%02x}}\r\n",
				info->m_clsid.Data1,
				info->m_clsid.Data2,
				info->m_clsid.Data3,
				info->m_clsid.Data4[0],
				info->m_clsid.Data4[1],
				info->m_clsid.Data4[2],
				info->m_clsid.Data4[3],
				info->m_clsid.Data4[4],
				info->m_clsid.Data4[5],
				info->m_clsid.Data4[6],
				info->m_clsid.Data4[7]);
			for(size_t i = 0; i < strlen(sz_data); i++){
				sz_data[i] = toupper(sz_data[i]);
			}

			file.Write(sz_data, strlen(sz_data));
			memset(sz_data, 0, sizeof(sz_data));

			POSITION group_pos = server->groups.GetHeadPosition();
			while(group_pos){
				COPCGroup* group = server->groups.GetNext(group_pos);
				if(!group)
					continue;

				sprintf(sz_data, "\t%s\r\n", group->get_name().GetBuffer(0));
				file.Write(sz_data, strlen(sz_data));
				memset(sz_data, 0, sizeof(sz_data));

				POSITION item_pos = group->items.GetHeadPosition();
				while(item_pos){
					Item* item = group->items.GetNext(item_pos);
					if(!item)
						continue;

					sprintf(sz_data, "\t\t%s\r\n", item->name.GetBuffer(0));
					file.Write(sz_data, strlen(sz_data));
					memset(sz_data, 0, sizeof(sz_data));
				}
			}
			
		}
		
		
		desc[7] = "\r\n//*********************************************************//\r\n";
		desc[8] = "//                          End                            //\r\n";
		desc[9] = "//*********************************************************//\r\n";
		
		for(i = 7; i < 10; i++)
			file.Write(desc[i], strlen(desc[i]));
		file.Close();
	}

}

void OPCClientDoc::OnUpdateFileExport(CCmdUI* pCmdUI) 
{
	// TODO: Add your command update UI handler code here
	
}

void OPCClientDoc::OnFileExportLog() 
{
	// TODO: Add your command handler code here
	
}

void OPCClientDoc::OnUpdateFileExportLog(CCmdUI* pCmdUI) 
{
	// TODO: Add your command update UI handler code here
	pCmdUI->Enable(FALSE);
}

void OPCClientDoc::OnItemProperties() 
{
	// TODO: Add your command handler code here
	
}

void OPCClientDoc::OnUpdateItemProperties(CCmdUI* pCmdUI) 
{
	// TODO: Add your command update UI handler code here
	/*
	pCmdUI->Enable(current_server
		&& current_server->get_current_group()
		&& current_server->get_current_group()->current_item
		);
	*/
	pCmdUI->Enable(FALSE);
}

void OPCClientDoc::OnItemRemove() 
{
	// TODO: Add your command handler code here
	if(current_server && current_server->get_current_group()){
		COPCGroup* group = current_server->get_current_group();
		group->remove_item();
	}
}

void OPCClientDoc::OnUpdateItemRemove(CCmdUI* pCmdUI) 
{
	// TODO: Add your command update UI handler code here
	pCmdUI->Enable(current_server
		&& current_server->get_current_group()
		&& current_server->get_current_group()->current_item
		);
}

void OPCClientDoc::OnItemWrite() 
{
	// TODO: Add your command handler code here
	if(current_server == NULL)
		return;

	CWriteDialog dialog;
	if(dialog.DoModal() == IDOK){
		CWaitCursor wait;
		HRESULT* errors = NULL;
	
		COPCGroup* group = current_server->get_current_group();
		if(group == NULL)
			return;
		Item* item = group->current_item;
		if(item == NULL)
			return;

		DWORD transaction_id = 2;
		COleVariant value(dialog.m_value);
		value.ChangeType(item->value.vt);
		
		if(dialog.m_async){		//异步写入
			if(group->use_cp){	//使用连接点
				OPCAsyncIO2 async2;
				if(async2.Attach(group->opc_group) == S_OK){
					
					HRESULT hr = async2.Write(
						1,
						&item->hServerHandle,
						&value,
						transaction_id,
						&transaction_id,
						&errors);
					if(SUCCEEDED(hr)){
						if(FAILED(errors[0]))
							ReportError(_T("Async Write Error: "), errors[0]);

						CoTaskMemFree(errors);
					}
					else
						ReportError(_T("Async Write Error: "), hr);

					async2.Detach();
				}
			}
			else{		//无连接点
				OPCAsyncIO async;
				if(async.Attach(group->opc_group) == S_OK){
					HRESULT hr = async.Write(
						group->connection2,
						1,
						&item->hServerHandle,
						&value,
						&transaction_id,
						&errors);
					if(SUCCEEDED(hr)){
						if(FAILED(errors[0]))
							ReportError(_T("Async Write Error: "), errors[0]);

						CoTaskMemFree(errors);
					}
					else
						ReportError(_T("Async Write Error: "), hr);
					
					async.Detach();
				}
			}
		}
		else{				//同步写入
			OPCAsyncIO async;
			if(async.Attach(group->opc_group) == S_OK){
				HRESULT hr = async.Write(
					group->connection2,
					1,
					&item->hServerHandle,
					&value,
					&transaction_id,
					&errors);
				if(SUCCEEDED(hr)){
					if(FAILED(errors[0]))
						ReportError(_T("Sync Write Error: "), errors[0]);
					
					CoTaskMemFree(errors);
				}
				else
					ReportError(_T("Sync Write Error: "), hr);
				
				async.Detach();
			}
		}
	}
}

void OPCClientDoc::OnUpdateItemWrite(CCmdUI* pCmdUI) 
{
	// TODO: Add your command update UI handler code here
	
	pCmdUI->Enable(current_server
		&& current_server->get_current_group()
		&& current_server->get_current_group()->current_item
		);
	
	//pCmdUI->Enable(FALSE);
}

⌨️ 快捷键说明

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