?? opcclientdoc.cpp
字號(hào):
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);
}
//導(dǎo)出配置文件
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;
COleVariant value;
value=Str2Variant(dialog.m_value,item->value.vt);
DWORD transaction_id = 2;
//(dialog.m_value);
//value.ChangeType(item->value.vt);
// for (int i =0; i<51;i++)
// {
// value.ChangeType(VT_I2);
// value.intVal = i;//i;
transaction_id=2;//要重新付初值
if(dialog.m_async){ //異步寫入
if(group->use_cp){ //使用連接點(diǎn)
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{ //無連接點(diǎn)
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;
OPCSyncIO async;//同步不能為OPCAsyncIO
if(async.Attach(group->opc_group) == S_OK){
HRESULT hr = async.Write(
1,
&item->hServerHandle,
&value,
&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);
}
COleVariant OPCClientDoc::Str2Variant(CString valuestr,unsigned short vtype)
{
VARIANT itmV;
char *pBuff=valuestr.GetBuffer(0);
itmV.vt=vtype;
switch(itmV.vt )
{
case VT_I1:
case VT_UI1: // BYTE
itmV.bVal =(BYTE)atoi(pBuff);
break;
case VT_I2: // SHORT
itmV.iVal=atoi(pBuff);
break;
case VT_UI2: // UNSIGNED SHORT
itmV.uiVal =(USHORT)atoi(pBuff);
break;
case VT_I4: // LONG
itmV.lVal= atol(pBuff);
break;
case VT_UI4: // UNSIGNED LONG
itmV.ulVal =(ULONG)atol(pBuff);
break;
case VT_INT: // INTEGER
itmV.intVal =atoi(pBuff);
break;
case VT_UINT: // UNSIGNED INTEGER
itmV.uintVal =( USHORT)atoi(pBuff);
break;
case VT_R4: // FLOAT
itmV.fltVal =(float)atof(pBuff);
break;
case VT_R8: // DOUBLE
itmV.dblVal =atof(pBuff);
break;
case VT_BSTR: //BSTR
VariantClear((VARIANT *)&itmV.bstrVal);//SysAllocString((BSTR)
itmV.bstrVal =_com_util::ConvertStringToBSTR(pBuff);
itmV.vt=VT_BSTR;
break;
case VT_BOOL:
if(!stricmp("TRUE",pBuff))
itmV.boolVal=TRUE;
else
itmV.boolVal=FALSE;
break;
case VT_DATE:
// VariantTimeToSystemTime (itmV.date, &st);
// ct = st;
// cs = ct.Format ("%H:%M:%S %d %b %Y");
// strncpy (buf, cs, 200);
// return;
break;
default:
// return;
break;
}
return itmV;
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -