📄 mexopc.cpp
字号:
}
else
{
pDoc->ReportError( _T("ASync Write: %s"), pErrors[0] );
z[0] = 2789561;
return ;
}
}
}
else
{
OPCAsyncIO opcAsyncIO;
if( opcAsyncIO.Attach( pDoc->opcGroup ) == S_OK )
{
COleVariant vt( dlg.m_value ); // initialize as a string
vt.ChangeType( item->value.vt ); // let COleVariant convert!
HRESULT hr = opcAsyncIO.Write(pDoc->dwConnection2, 1, &item->hServerHandle,
vt, &pDoc->transactionID, &pErrors);
if( SUCCEEDED(hr) )
{
if( FAILED(pErrors[0]) )
{
pDoc->ReportError( _T("ASync Write:%s "), pErrors[0] );
z[0] = 2789561;
return ;
}
CoTaskMemFree( pErrors );
}
else
{
pDoc->ReportError( _T("ASync Write: %s"), pErrors[0] );
z[0] = 2789561;
return ;
}
}
}
}
else
{
OPCSyncIO opcSyncIO;
if( opcSyncIO.Attach( pDoc->opcGroup ) == S_OK )
{
COleVariant vt( dlg.m_value ); // initialize as a string
vt.ChangeType(item->value.vt ); // let COleVariant convert!
HRESULT hr = opcSyncIO.Write( 1, &item->hServerHandle, vt, &pErrors);
if( SUCCEEDED(hr) )
{
if( FAILED(pErrors[0]) )
{
pDoc->ReportError( _T("Sync Write:%s "), pErrors[0] );
z[0] = 2789561;
return ;
}
CoTaskMemFree( pErrors );
}
else
{
pDoc-> ReportError( _T("Sync Write:%s "), pErrors[0] );
z[0] = 2789561;
return ;
}
}
}
}
}
void dill(CString para,CString name,double * z)
{
double * y;
y = (double*)mxGetData(inth);
//***********************wait***********************************
if(para.Compare( "wait" ) == 0)
{
lock.Lock(int(y[0]));
if (lock.IsLocked())
lock.Unlock();
event->ResetEvent();
}
//******************matlab将进入休眠*******************************
if(para.Compare( "sleep" ) == 0||para.Compare( "Sleep" ) == 0||para.Compare( "SLEEP" ) == 0)
{
double * y;
y = (double*)mxGetData(inth);
int r = int(y);
Sleep(r);
}
z[0] =0;
}
void fill(CString para,double * z)
{
if(start)
{
if(AfxOleInit())
{
for(int i=0;i<1000;i++)
{
DoubleItemValue[i] = 0;
}
}
}
start = 0;
//*********************加条操作**********************************************************
if(para.Compare( "add" ) == 0 ||para.Compare( "Add" ) == 0||para.Compare( "ADD" ) == 0)
{
USES_CONVERSION;
ASSERT( pDoc->opcGroup.IsOk() );
CAddItem dlg(pDoc,pDoc->opcServer);
dlg.DoModal();
HRESULT hr = itemMgt.Attach( pDoc->opcGroup );
if( FAILED(hr) )
{
z[0] = 2789561;
return;
}
// Add a new item
for(int i=0;i<itemcount;i++)
{
Item* item = new Item;
item->quality = OPC_QUALITY_GOOD;
item->name =allitems[i];//dlg.m_itemID;
itemID =allitems[i];//dlg.m_itemID;
// fill out its definition
accessPath =dlg.m_accessPath;
OPCITEMDEF idef;
idef.szItemID = T2OLE((LPTSTR)itemID);
idef.dwBlobSize = 0;
idef.pBlob = NULL;
idef.bActive = TRUE;
idef.hClient = (OPCHANDLE)item; // pointer to item is its "handle"
idef.szAccessPath = T2OLE((LPTSTR)accessPath);
idef.vtRequestedDataType = VT_EMPTY;
// add the item
OPCITEMRESULT * pResults;
HRESULT *pErrors;
hr = itemMgt.AddItems(1, &idef, &pResults, &pErrors);
if( FAILED( hr ) ) // if the call failed, get out
{
pDoc->ReportError( _T("AddItems: %s"), pErrors[0] );
delete item;
z[0] = 2789561;
return ;
}
// If the call was successful, memory must be cleaned up
item->hServerHandle = pResults->hServer; // save the server handle
//type = pResults->vtCanonicalDataType;
// Save the result in pErrors before freeing
HRESULT itemResult = pErrors[0]; // and the item's result
if( pResults->pBlob != NULL )
CoTaskMemFree( pResults->pBlob );
CoTaskMemFree( pResults );
CoTaskMemFree( pErrors );
// It this item failed, don't keep it (the server didn't)
if( FAILED(itemResult) )
{
pDoc->ReportError( _T("AddItems: %s"), itemResult );
delete item;
z[0] = 2789561;
return ;
}
pDoc->items.AddTail( item ); // store this item in the item list
// Read its initial value
OPCSyncIO opcSyncIO;
if( opcSyncIO.Attach( pDoc->opcGroup ) == S_OK )
{
OPCITEMSTATE* pItemState;
hr = opcSyncIO.Read( OPC_DS_CACHE,1, &item->hServerHandle, &pItemState, &pErrors);
if( SUCCEEDED(hr) )
{
ASSERT( pItemState->hClient == (OPCHANDLE)item );
item->quality = pItemState->wQuality;
item->value = pItemState->vDataValue;
VariantClear( &pItemState->vDataValue );
CoTaskMemFree( pItemState );
CoTaskMemFree( pErrors );
}
else
{
pDoc->ReportError( _T("Sync Read:%s "), pErrors[0] );
z[0] = 2789561;
return ;
}
}
if(itemMgt.IsOk())
{
}
// test some more interfaces
//#ifdef FULL_TEST
if( itemMgt.IsOk() )
{
hr = itemMgt.SetActiveState( 1, &item->hServerHandle, FALSE, &pErrors);
if( SUCCEEDED(hr) )
CoTaskMemFree( pErrors );
hr = itemMgt.SetActiveState( 1, &item->hServerHandle, TRUE, &pErrors);
if( SUCCEEDED(hr) )
CoTaskMemFree( pErrors );
hr = itemMgt.SetClientHandles( 1, &item->hServerHandle, (OPCHANDLE*)&item, &pErrors);
if( SUCCEEDED(hr) )
CoTaskMemFree( pErrors );
}
else if( FAILED(hr) )
pDoc->ReportError( _T("EnumOPCItemAttributes:%s "), pErrors[0] );
//#endif //FULL_TEST
clientitems1.Add(item);
}
//*************************下面是更新操作*****************************************
}
//*************************************************读函数*******************************************************
//***********************************************注册函数*****************************************************
if(para.Compare( "open" ) == 0||para.Compare( "Open" ) == 0||para.Compare( "OPEN" ) == 0)
{
pDoc = new OPCClientDoc();
OPCServerDlg dlg;
USES_CONVERSION;
ASSERT( !pDoc->opcServer.IsOk() );
HRESULT hr = S_OK;
// Prompt user for the server name (from list of installed servers)
if(dlg.DoModal()!=IDOK)
{
z[0] = 2789561;
init =0;
return;
}
m_synctime = dlg.m_synctime;
POSITION pos = dlg.m_Servers.GetHeadPosition();
while( pos )
{
OPCServerInfo* pServerInfo = dlg.m_Servers.GetNext( pos );
if(dlg.m_Server == pServerInfo->m_ProgID)
{
dlg.m_clsid = pServerInfo->m_clsid;
}
}
// If not found, Get CLSID from the ProgID
//USES_CONVERSION;
IOPCServerList* pServers=NULL;
COSERVERINFO si;
MULTI_QI qi;
si.dwReserved1 = 0;
si.pwszName = NULL;
if( !dlg.m_Node.IsEmpty() )
si.pwszName = T2OLE(dlg.m_Node.GetBuffer(0));
si.pAuthInfo = NULL;
si.dwReserved2 = 0;
qi.pIID = &IID_IOPCServerList;
qi.pItf = NULL;
qi.hr = 0;
hr = CoCreateInstanceEx(CLSID_OPCServerList, NULL, CLSCTX_ALL, &si, 1, &qi);
if (FAILED(hr) || FAILED(qi.hr))
{
CString msg(_T("Please install the OPC 2.0 Components."));
if( !dlg.m_Node.IsEmpty() )
msg.Format(_T("Please install the OPC 2.0 Components on %s."), (LPCTSTR)dlg.m_Node);
AfxMessageBox(msg);
z[0] = 2789561;
init =0;
return ;
}
pServers = (IOPCServerList*)qi.pItf;
hr = pServers->CLSIDFromProgID(T2OLE(dlg.m_Server.GetBuffer(0)), &dlg.m_clsid);
pServers->Release();
if (FAILED(hr))
{
CString msg;
msg.Format(_T("Error locating '%s'."), (LPCTSTR)dlg.m_Server);
if( !dlg.m_Node.IsEmpty() )
msg.Format(_T("Error locating '%s' on %s."), (LPCTSTR)dlg.m_Server, (LPCTSTR)dlg.m_Node);
AfxMessageBox(msg);
z[0] = 2789561;
init =0;
return ;
}
// Create a running object from the class ID
// (CLSCTX_ALL will allow in-proc, local and remote)
LPUNKNOWN pUnkn = NULL;
if( dlg.m_Node.IsEmpty() )
{
hr = CoCreateInstance(dlg.m_clsid, NULL, CLSCTX_ALL, IID_IUnknown, (LPVOID *)&pUnkn);
if( FAILED(hr) || pUnkn == NULL)
{
CString format( (LPCSTR)"Error connecting to OPC server '%s' \n" );
CString msg;
msg.Format( format, dlg.m_Server );
msg += _T("CoCreateInstance: %s");
pDoc->ReportError( msg, hr );
z[0] = 2789561;
init =0;
return ;
}
}
else // use the node name
{
COSERVERINFO si;
MULTI_QI qi;
si.dwReserved1 = 0;
si.pwszName = T2OLE(dlg.m_Node.GetBuffer(0));
si.pAuthInfo = NULL;
si.dwReserved2 = 0;
qi.pIID = &IID_IOPCServer;
qi.pItf = NULL;
qi.hr = 0;
hr = CoCreateInstanceEx(dlg.m_clsid, NULL, CLSCTX_ALL, &si, 1, &qi);
if (FAILED(hr))
{
CString format( (LPCSTR)"Error connecting to OPC server '%s' \n" );
CString msg;
msg.Format( format, dlg.m_Server );
msg += _T("CoCreateInstance:%s ");
pDoc->ReportError( msg, hr );
z[0] = 2789561;
init =0;
return ;
}
if (FAILED(qi.hr))
{
pDoc->ReportError( _T("MultiQI:%s "), qi.hr );
z[0] = 2789561;
init =0;
return ;
}
pUnkn = qi.pItf;
}
// Get the IOPCServer interface.
hr = pDoc->opcServer.Attach( pUnkn );
pUnkn = NULL;
if( FAILED(hr) )
{
CString appName((LPCSTR)AFX_IDS_APP_TITLE);
MessageBox(0, _T("You may not have registered the OPC Proxy dll!\n"), appName, MB_OK);
z[0] = 2789561;
init =0;
return ;
}
// OPC 2.0 Server shutdown ConnectionPoint
{
IConnectionPointContainer *pCPC = 0;
hr = pDoc->opcServer.QueryInterface(IID_IConnectionPointContainer, (void**)&pCPC);
if( SUCCEEDED(hr) ) // This server supports 2.0
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -