📄 group.cpp
字号:
else GetInfoServer()->DisplayError(res);
return FALSE;
}
DWORD CGroup::GetUpdateRate()
{
return m_dwUpdateRate;
}
void CGroup::SetUpdateRate(DWORD dwUpdateRate)
{
m_dwUpdateRate = dwUpdateRate;
}
long CGroup::GetTimeBias()
{
return m_lTimeBias;
}
void CGroup::SetTimeBias(long lTimeBias)
{
m_lTimeBias = lTimeBias;
}
float CGroup::GetDeadBand()
{
return m_fDeadBand;
}
void CGroup::SetDeadBand(float fDeadBand)
{
m_fDeadBand = fDeadBand;
}
LCID CGroup::GetLCID()
{
return m_dwLCID;
}
void CGroup::SetLCID(LCID lcid)
{
m_dwLCID = lcid;
}
BOOL CGroup::Advise()
{
BOOL bRet = FALSE;
if(GetInfoServer()->GetOPCVersion() < OPC_VERSION_20)
{
// OPC 1.0 Callbacks
HRESULT res;
DWORD dwConnection;
CComPtr<IOPCGroupStateMgt> pGRP = NULL;
GetInterface()->QueryInterface(IID_IOPCGroupStateMgt, (void**)&pGRP);
CIDataObject comDO(pGRP);
FORMATETC fe;
//FORMATETC init
fe.lindex = -1;
fe.tymed = TYMED_HGLOBAL;
fe.ptd = NULL;
fe.dwAspect = DVASPECT_CONTENT;
fe.cfFormat = RegisterClipboardFormat("OPCSTMFORMATWRITECOMPLETE");
res = comDO.DAdvise(&fe, ADVF_PRIMEFIRST, *GetInfoServer()->GetAdviseSink(), &dwConnection);
if(FAILED(res))
{
GetInfoServer()->DisplayError(res);
comDO.DUnadvise(dwConnection);
return FALSE;
}
else SetOPCSTMFormatWriteCompleteConnection(dwConnection);
if(GetInfoServer()->GetDataChangeType() > 0) //with time stamp
{
fe.cfFormat = RegisterClipboardFormat("OPCSTMFORMATDATATIME");
res = comDO.DAdvise(&fe, ADVF_PRIMEFIRST, *GetInfoServer()->GetAdviseSink(), &dwConnection);
if(FAILED(res))
{
GetInfoServer()->DisplayError(res);
comDO.DUnadvise(dwConnection);
return FALSE;
}
else SetOPCSTMFormatDataTimeConnection(dwConnection);
}
else
{
fe.cfFormat = RegisterClipboardFormat("OPCSTMFORMATDATA");
res = comDO.DAdvise(&fe, ADVF_PRIMEFIRST, *GetInfoServer()->GetAdviseSink(), &dwConnection);
if(FAILED(res))
{
GetInfoServer()->DisplayError(res);
comDO.DUnadvise(dwConnection);
return FALSE;
}
else SetOPCSTMFormatDataConnection(dwConnection);
}
bRet = TRUE;
TRACE("add the unadvise code !\n");
}
else
{
// OPC 2.0 ConnectionPoints
if((*GetInfoServer()->GetDataAccessConnectionPoint())->Init(this))
{
//default is enabled, therefore only for disable needed
if(!m_bDataChange)
bRet = SetDataChangeEnabled(m_bDataChange);
else
bRet = TRUE;
}
}
if(bRet) ((CInfoServerExplorerApp*)AfxGetApp())->DisplayText(_T("CGroup::Advise() was successful."));
return bRet;
}
BOOL CGroup::UnAdvise()
{
BOOL bRet = FALSE;
HRESULT res;
if(GetInfoServer()->GetOPCVersion() < OPC_VERSION_20)
{
// OPC 1.0 Callbacks
CComPtr<IOPCGroupStateMgt> pGRP = NULL;
GetInterface()->QueryInterface(IID_IOPCGroupStateMgt, (void**)&pGRP);
CIDataObject comDO(pGRP);
if(GetOPCSTMFormatWriteCompleteConnection() != 0)
{
res = comDO.DUnadvise(GetOPCSTMFormatWriteCompleteConnection());
if(FAILED(res))
((CInfoServerExplorerApp*)AfxGetApp())->DisplayText(_T("DUnadvise(OPCSTMFormatDataTime)"), res);
else
SetOPCSTMFormatWriteCompleteConnection(0);
}
if(GetOPCSTMFormatDataTimeConnection() != 0)
{
res = comDO.DUnadvise(GetOPCSTMFormatDataTimeConnection());
if(FAILED(res))
((CInfoServerExplorerApp*)AfxGetApp())->DisplayText(_T("DUnadvise(OPCSTMFormatDataTime) in ReloadFromServer()"), res);
else
SetOPCSTMFormatDataTimeConnection(0);
}
if(GetOPCSTMFormatDataConnection() != 0)
{
res = comDO.DUnadvise(GetOPCSTMFormatDataConnection());
if(FAILED(res))
((CInfoServerExplorerApp*)AfxGetApp())->DisplayText(_T("DUnadvise(OPCSTMFormatData) in ReloadFromServer()"), res);
else
SetOPCSTMFormatDataConnection(0);
}
bRet = TRUE;
}
else
{
// OPC 2.0 ConnectionPoints
bRet = (*GetInfoServer()->GetDataAccessConnectionPoint())->Term(this);
}
if(bRet) ((CInfoServerExplorerApp*)AfxGetApp())->DisplayText(_T("CGroup::UnAdvise() was successful."));
return bRet;
}
BOOL CGroup::ReadItemsSync(OPCDATASOURCE dwSource, DWORD dwNumItems, OPCHANDLE* phServer)
{
//now read the values
BOOL bRet = FALSE;
HRESULT res;
HRESULT* pReadErrors;
OPCITEMSTATE* pItemValues;
CIOPCSyncIO opcSyncIO(GetInterface());
res = opcSyncIO.Read(dwSource,dwNumItems,phServer,&pItemValues,&pReadErrors);
if(SUCCEEDED(res))
{
for(ULONG r = 0; r < dwNumItems; r++)
{
if(SUCCEEDED(pReadErrors[r]))
{
try
{
CItem* pItem = (CItem*) pItemValues[r].hClient;
pItem->SetQuality(pItemValues[r].wQuality);
pItem->SetValue(pItemValues[r].vDataValue);
pItem->SetTimeStamp(pItemValues[r].ftTimeStamp);
//value needs to be updated in the view
pItem->SetChanged(TRUE);
}
catch(...)
{
((CInfoServerExplorerApp*)AfxGetApp())->DisplayText(_T("Exception in CGroup::ReadItemsSync()"));
}
}
else
{
GetInfoServer()->DisplayError(pReadErrors[r]);
}
VariantClear(&pItemValues[r].vDataValue);
}
bRet = TRUE;
}
else
{
GetInfoServer()->DisplayError(res);
}
CoTaskMemFree(pItemValues);
CoTaskMemFree(pReadErrors);
return bRet;
}
BOOL CGroup::Cancel()
{
/* cancel an active async operation: read, write or group refresh
OPC 1.0: only a TransactionID is used
OPC 2.0: transaction and CancelID is used
*/
HRESULT res;
BOOL bRet = FALSE;
if(GetCurrentTransactionID() == 0)
{
((CInfoServerExplorerApp*)AfxGetApp())->DisplayText(_T("No active transaction !"));
return FALSE;
}
if(GetInfoServer()->GetOPCVersion() >= OPC_VERSION_20)
{
CIOPCAsyncIO2 opcAsyncIO2(GetInterface());
DWORD dwTransactionID = TRANSACTION_CANCEL; //in (returned in OnCancelComplete()) can be any number
if(GetCurrentCancelID() == 0)
{
//should not happen since TransactionID and CancelID are managed aleays together
((CInfoServerExplorerApp*)AfxGetApp())->DisplayText(_T("No active cancel ID !"));
return FALSE;
}
res = opcAsyncIO2.Cancel2(GetCurrentCancelID());
}
else
{
CIOPCAsyncIO opcAsyncIO(GetInterface());
res = opcAsyncIO.Cancel(GetCurrentTransactionID());
}
if(SUCCEEDED(res))
{
SetCurrentTransactionID(0);
SetCurrentCancelID(0);
bRet = TRUE;
}
else GetInfoServer()->DisplayError(res);
return bRet;
}
BOOL CGroup::ReadItemsAsync(OPCDATASOURCE dwSource, DWORD dwNumItems, OPCHANDLE* phServer)
{
//now read the values
BOOL bRet = FALSE;
HRESULT res;
HRESULT* pReadErrors;
CheckForOpenTransactions();
if(GetInfoServer()->GetOPCVersion() >= OPC_VERSION_20)
{
CIOPCAsyncIO2 opcAsyncIO2(GetInterface());
DWORD dwTransactionID = TRANSACTION_READ; //in (returned in OnReadComplete()) can be any number
DWORD dwCancelID; //out
res = opcAsyncIO2.Read(dwNumItems, phServer, dwTransactionID, &dwCancelID, &pReadErrors);
SetCurrentTransactionID(dwTransactionID);
SetCurrentCancelID(dwCancelID);
}
else
{
CIOPCAsyncIO opcAsyncIO(GetInterface());
DWORD dwTransactionID; //out
if(GetInfoServer()->GetDataChangeType() > 0) //0 = no timestamp; 1 = with timestamp
res = opcAsyncIO.Read(GetOPCSTMFormatDataTimeConnection(), dwSource, dwNumItems, phServer,&dwTransactionID,&pReadErrors);
else
res = opcAsyncIO.Read(GetOPCSTMFormatDataConnection(), dwSource, dwNumItems, phServer,&dwTransactionID,&pReadErrors);
SetCurrentTransactionID(dwTransactionID);
}
if(SUCCEEDED(res))
{
for(ULONG r = 0; r < dwNumItems; r++)
{
if(FAILED(pReadErrors[r]))
{
GetInfoServer()->DisplayError(pReadErrors[r]);
bRet = FALSE;
}
}
bRet = TRUE;
}
else
{
GetInfoServer()->DisplayError(res);
}
CoTaskMemFree(pReadErrors);
return bRet;
}
BOOL CGroup::CommandItemsSync(DWORD dwNumItems, OPCHANDLE* phServer, VARIANT* pItemValues)
{
//now read the values
BOOL bRet = TRUE;
HRESULT res;
HRESULT* pWriteErrors;
CIOPCSyncIO opcSyncIO(GetInterface());
res = opcSyncIO.Write(dwNumItems,phServer,pItemValues,&pWriteErrors);
if(SUCCEEDED(res))
{
for(ULONG w = 0; w < dwNumItems; w++)
{
if(FAILED(pWriteErrors[w]))
{
GetInfoServer()->DisplayError(pWriteErrors[w]);
bRet = FALSE;
}
}
}
else
{
GetInfoServer()->DisplayError(res);
bRet = FALSE;
}
CoTaskMemFree(pWriteErrors);
return bRet;
}
BOOL CGroup::CommandItemsAsync(DWORD dwNumItems, OPCHANDLE* phServer, VARIANT* pItemValues)
{
BOOL bRet = TRUE;
HRESULT res;
HRESULT* pWriteErrors;
CheckForOpenTransactions();
if(GetInfoServer()->GetOPCVersion() >= OPC_VERSION_20)
{
CIOPCAsyncIO2 opcAsyncIO2(GetInterface());
DWORD dwTransactionID = TRANSACTION_WRITE; //in (returned in OnWriteComplete()) can be any number
DWORD dwCancelID; //out
res = opcAsyncIO2.Write(dwNumItems,phServer,pItemValues, dwTransactionID, &dwCancelID, &pWriteErrors);
SetCurrentTransactionID(dwTransactionID);
SetCurrentCancelID(dwCancelID);
}
else
{
CIOPCAsyncIO opcAsyncIO(GetInterface());
DWORD dwTransactionID; //out
res = opcAsyncIO.Write(GetOPCSTMFormatWriteCompleteConnection(), dwNumItems,phServer,pItemValues, &dwTransactionID,&pWriteErrors);
SetCurrentTransactionID(dwTransactionID);
}
if(SUCCEEDED(res))
{
for(ULONG w = 0; w < dwNumItems; w++)
{
if(FAILED(pWriteErrors[w]))
{
GetInfoServer()->DisplayError(pWriteErrors[w]);
bRet = FALSE;
}
}
}
else
{
GetInfoServer()->DisplayError(res);
bRet = FALSE;
}
CoTaskMemFree(pWriteErrors);
return bRet;
}
BOOL CGroup::GetDataChangeEnabled(BOOL* bEnable, BOOL bReadFromServer /* = TRUE */)
{
BOOL bRet = FALSE;
HRESULT res;
if(GetInfoServer()->GetOPCVersion() >= OPC_VERSION_20)
{
if(bReadFromServer)
{
CIOPCAsyncIO2 opcAsyncIO2(GetInterface());
res = opcAsyncIO2.GetEnable(bEnable);
if(SUCCEEDED(res))
{
bRet = TRUE;
}
else
{
GetInfoServer()->DisplayError(res);
*bEnable = FALSE;
bRet = FALSE;
}
}
else
{
*bEnable = m_bDataChange;
bRet = TRUE;
}
}
else
{
//OPC 1.0 always enabled
*bEnable = TRUE;
bRet = TRUE;
}
m_bDataChange = *bEnable;
return bRet;
}
BOOL CGroup::SetDataChangeEnabled(BOOL bEnable, BOOL bWriteToServer /*= TRUE*/)
{
BOOL bRet = FALSE;
HRESULT res;
#pragma message ("---> SetDataChangeEnabled():: need to zero current transactionID and CancelID ?")
CheckForOpenTransactions();
if(GetInfoServer()->GetOPCVersion() >= OPC_VERSION_20)
{
if(bWriteToServer)
{
CIOPCAsyncIO2 opcAsyncIO2(GetInterface());
res = opcAsyncIO2.SetEnable(bEnable);
if(SUCCEEDED(res))
{
//? needed
SetCurrentTransactionID(0);
SetCurrentCancelID(0);
bRet = TRUE;
}
else
{
GetInfoServer()->DisplayError(res);
bEnable = FALSE;
bRet = FALSE;
}
}
else
{
bRet = TRUE;
}
}
else
{
//OPC 1.0 always enabled
bEnable = TRUE;
bRet = TRUE;
}
m_bDataChange = bEnable;
return bRet;
}
//dynmaic transaction handling
void CGroup::SetCurrentTransactionID(DWORD dwCurrentTransactionID)
{
m_dwCurrentTransactionID = dwCurrentTransactionID;
}
DWORD CGroup::GetCurrentTransactionID()
{
return m_dwCurrentTransactionID;
}
void CGroup::SetCurrentCancelID(DWORD dwCurrentCancelID)
{
m_dwCancelID = dwCurrentCancelID;
}
DWORD CGroup::GetCurrentCancelID()
{
return m_dwCancelID;
}
BOOL CGroup::ValidateItems(DWORD dwNumItems, OPCITEMDEF* pItemArray)
{
/*
this is not and "real" OPCGroup function, in our case it
uses the servers cache
*/
BOOL bRet = TRUE;
HRESULT res;
CIOPCItemMgt opcItemMgt(GetInterface());
OPCITEMRESULT* pValidationResults;
HRESULT* pErrorValidate;
res = opcItemMgt.ValidateItems(dwNumItems, pItemArray, FALSE , &pValidationResults, &pErrorValidate);
if(SUCCEEDED(res))
{
for(ULONG i = 0; i < dwNumItems; i++)
{
if(FAILED(pErrorValidate[i]))
{
((CInfoServerExplorerApp*)AfxGetApp())->DisplayText(_T("CIOPCItemMgt::ValidateItems()"), pErrorValidate[i]);
bRet = FALSE;
}
}
((CInfoServerExplorerApp*)AfxGetApp())->DisplayText(_T("CIOPCItemMgt::ValidateItems() was successful."));
CoTaskMemFree(pErrorValidate);
CoTaskMemFree(pValidationResults);
}
else
{
((CInfoServerExplorerApp*)AfxGetApp())->DisplayText(_T("CIOPCItemMgt::ValidateItems()"), res);
bRet = FALSE;
}
return bRet;
}
//determines if there is an async transaction not finished.
BOOL CGroup::CheckForOpenTransactions()
{
if(GetCurrentTransactionID() != NULL)
{
((CInfoServerExplorerApp*)AfxGetApp())->DisplayText(_T("Open Async transaction (TransactionID != 0) !"));
return FALSE;
}
if(GetInfoServer()->GetOPCVersion() >= OPC_VERSION_20)
{
if(GetCurrentCancelID() != NULL)
{
((CInfoServerExplorerApp*)AfxGetApp())->DisplayText(_T("Open Async transaction (CancelID != 0) !"));
return FALSE;
}
}
return TRUE;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -