📄 group.cpp
字号:
phServer = (OPCHANDLE *) CoTaskMemAlloc (cdwItems * sizeof (OPCHANDLE));
// Return if allocation failed:
if (phServer == NULL)
{
ASSERT (FALSE);
return;
}
// Fill request [in] arguments:
for (dwIndex = 0; dwIndex < cdwItems; dwIndex++)
{
// Get pointer to item in input list:
CKItem *pItem = (CKItem *) cItemList [dwIndex];
ASSERT (pItem != NULL);
// Set server handle:
phServer [dwIndex] = pItem->GetServerHandle ();
}
// Wrap request processing in exception handler in case we get
// a bad pointer:
try
{
// Issue read request using IOPCSyncIO interface:
hr = m_pISync->Read (
dwSource, // Source (device or cache)
cdwItems, // Item count
phServer, // Array of server handles for items
&pValues, // Array of values
&pErrors); // Array of errors
// Check results:
for (dwIndex = 0; dwIndex < cdwItems; dwIndex++)
{
// Get pointer to item in input list:
CKItem *pItem = (CKItem *) cItemList [dwIndex];
ASSERT (pItem != NULL);
// Request succeeded and there were no errors:
if (SUCCEEDED (hr) && (pErrors && SUCCEEDED (pErrors [dwIndex])))
{
// Increment the successful read counter:
++cdwSuccess;
}
// Failure:
else
{
// Request succeeded, but one or more items could not be read:
if (pErrors && FAILED (pErrors [dwIndex]))
{
// Log error message with error code. (Could use GetErrorString()
// to get user friendly error string if we wish.)
LogMsg (bDeviceRead ? IDS_SYNC_READDEVICE_FAILED : IDS_SYNC_READCACHE_FAILED,
pItem->GetItemID (), GetName (), pErrors [dwIndex]);
}
// Request failed:
else
{
// Log error message with error code. (Could use GetErrorString()
// to get user friendly error string if we wish.)
LogMsg (bDeviceRead ? IDS_SYNC_READDEVICE_FAILED : IDS_SYNC_READCACHE_FAILED,
pItem->GetItemID (), GetName (), hr);
}
}
// Update item data:
pItem->UpdateData (pValues [dwIndex].vDataValue,
pValues [dwIndex].wQuality, pValues [dwIndex].ftTimeStamp);
// Should clear variant before we free it later:
VariantClear (&pValues [dwIndex].vDataValue);
}
}
catch (...)
{
// Probably hit a bad pointer. Try to process next item.
TRACE (_T("OTC: ReadSync exception handler invoked\r\n"));
if (m_pServer->IsConnected ())
{
// We not disconnected so why was an exception thrown?
ASSERT (FALSE);
}
}
// COM requires us to free memory allocated for [out] and [in/out]
// arguments (i.e. phServer, pValues, and pErrors):
CoTaskMemFree (phServer);
if (pValues)
CoTaskMemFree (pValues);
if (pErrors)
CoTaskMemFree (pErrors);
// Log success status if we are asked to post messages:
if (bPostMsg && cdwSuccess > 0)
{
// Log diferent message for read device and read cache:
LogMsg (bDeviceRead ? IDS_SYNC_READDEVICE_SUCCESS : IDS_SYNC_READCACHE_SUCCESS,
cdwSuccess, GetName ());
}
}
}
// **************************************************************************
// WriteSync ()
//
// Description:
// Perform synchronous write for specified items.
//
// Parameters:
// CObArray &cItemList Array of items to read.
// CStringArray &cValues Array of values to write.
// DWORD cdwItems Number of items in cItemList.
//
// Returns:
// void
// **************************************************************************
void CKGroup::WriteSync (CObArray &cItemList, CStringArray &cValues, DWORD cdwItems)
{
ASSERT (cdwItems > 0);
// There is no sence in building a request unless we are connected to
// the OPC Server and have a pointer to the IOPCSyncIO interface.
if (m_pServer->IsConnected () && m_pISync)
{
DWORD dwIndex = 0;
CKItem *pItem = NULL;
OPCHANDLE *phServer = NULL;
VARIANT *pValues = NULL;
HRESULT *pErrors = NULL;
HRESULT hr = E_FAIL;
DWORD cdwSuccess = 0;
DWORD cdwSentItems = 0;
// Allocate storage for server item handles and write values
phServer = (OPCHANDLE *) CoTaskMemAlloc (cdwItems * sizeof (OPCHANDLE));
pValues = (VARIANT *) CoTaskMemAlloc (cdwItems * sizeof (VARIANT));
// Return if allocation failed:
if (phServer == NULL || pValues == NULL)
{
ASSERT (FALSE);
return;
}
// Fill request [in] arguments:
for (dwIndex = 0, cdwSentItems = 0; dwIndex < cdwItems; dwIndex++)
{
// Get pointer to item in input list:
CKItem *pItem = (CKItem *) cItemList [dwIndex];
ASSERT (pItem != NULL);
// Load data for array type:
if (pItem->GetDataType () & VT_ARRAY)
{
// Convert array values in string format to variant array. If succeed,
// set server handle:
if (MapStringValToArrayVariant (cValues [cdwSentItems], pItem->GetValue (), &pValues [cdwSentItems]))
phServer [cdwSentItems++] = pItem->GetServerHandle ();
else
{
// Failed to convert array values in string format to variant array:
LogMsg (IDS_WRITE_DATA_MAP_FAILED, pItem->GetItemID ());
// Invalidate item:
cItemList [dwIndex] = NULL;
}
}
// Load data for non-array type:
else
{
// Convert value in string format to variant. If succeed, set server
// handle:
if (MapStringValToVariant (cValues [cdwSentItems], pValues [cdwSentItems], pItem->GetDataType ()))
phServer [cdwSentItems++] = pItem->GetServerHandle ();
else
{
// Failed to convert value in string format to variant:
LogMsg (IDS_WRITE_DATA_MAP_FAILED, pItem->GetItemID ());
// Invalidate item:
cItemList [dwIndex] = NULL;
}
}
}
// Wrap request processing in execption handler in case we get
// a bad pointer:
try
{
// Issue read request using IOPCSyncIO interface:
hr = m_pISync->Write (
cdwSentItems, // Item count
phServer, // Array of server handles for items
pValues, // Array of values
&pErrors); // Array of errors
// Check results:
for (dwIndex = 0, cdwSentItems = 0; dwIndex < cdwItems; dwIndex++)
{
// Get pointer to item in input list:
CKItem *pItem = (CKItem *) cItemList [dwIndex];
// Continue to next item if we invalidated current item above in
// MapStringToVariant() failure:
if (pItem == NULL)
continue;
// Request succeeded and there were no errors:
if (SUCCEEDED (hr) && (pErrors && SUCCEEDED (pErrors [cdwSentItems])))
{
// Increment the successful write counter:
++cdwSuccess;
}
// Failure:
else
{
// Request succeeded, but on or more items could not be written to:
if (pErrors && FAILED (pErrors [cdwSentItems]))
{
// Log error message with error code. (Could use GetErrorString()
// to get user friendly error string if we wish.)
LogMsg (IDS_SYNC_WRITE_FAILED, pItem->GetItemID (), GetName (),
pErrors [cdwSentItems]);
}
// Request failed:
else
{
// Log error message with error code. (Could use GetErrorString()
// to get user friendly error string if we wish.)
LogMsg (IDS_SYNC_WRITE_FAILED, pItem->GetItemID (), GetName (), hr);
}
}
// Should clear variant before we free it later:
VariantClear (&pValues [cdwSentItems]);
// Increment sent items counter:
cdwSentItems++;
}
}
catch (...)
{
// Probably hit a bad pointer. Try to process next item.
TRACE (_T("OTC: WriteSync exception handler invoked\r\n"));
if (m_pServer->IsConnected ())
{
// We not disconnected so why was an exception thrown?
ASSERT (FALSE);
}
}
// COM requires us to free memory allocated for [out] and [in/out]
// arguments (i.e. phServer, pValues, and pErrors):
CoTaskMemFree (phServer);
CoTaskMemFree (pValues);
if (pErrors)
CoTaskMemFree (pErrors);
// Log success status:
if (cdwSuccess > 0)
LogMsg (IDS_SYNC_WRITE_SUCCESS, cdwSuccess, GetName ());
}
}
// **************************************************************************
// ReadAsync10 ()
//
// Description:
// Perform an asynchronous 1.0 read for specified items.
//
// Parameters:
// CObArray &cItemList Array of items to read.
// DWORD cdwItems Number of items in cItemList.
// bool bDeviceRead Set to true to perform device read, false
// for cache read.
//
// Returns:
// void
// **************************************************************************
void CKGroup::ReadAsync10 (CObArray &cItemList, DWORD cdwItems, bool bDeviceRead)
{
ASSERT (cdwItems > 0);
// There is no sence in building a request unless we are connected to
// the OPC Server and have a pointer to the IOPCAsyncIO interface.
if (m_pServer->IsConnected () && m_pIAsync)
{
ASSERT (m_dwCookieRead != 0);
DWORD dwIndex = 0;
CKItem *pItem = NULL;
OPCDATASOURCE dwSource = bDeviceRead ? OPC_DS_DEVICE : OPC_DS_CACHE;
OPCHANDLE *phServer = NULL;
DWORD dwTransID = 0;
HRESULT *pErrors = NULL;
HRESULT hr = E_FAIL;
DWORD cdwSuccess = 0;
// Allocate storage for server item handles:
phServer = (OPCHANDLE *) CoTaskMemAlloc (cdwItems * sizeof (OPCHANDLE));
// Return if allocation failed:
if (phServer == NULL)
{
ASSERT (FALSE);
return;
}
// Fill request [in] arguments:
for (dwIndex = 0; dwIndex < cdwItems; dwIndex++)
{
// Get pointer to item in input list:
CKItem *pItem = (CKItem *) cItemList [dwIndex];
ASSERT (pItem != NULL);
// Set server handle:
phServer [dwIndex] = pItem->GetServerHandle ();
}
// Wrap request processing in exception hander in case we get
// a bad pointer:
try
{
// Issue read request using IOPCAsyncIO interface:
hr = m_pIAsync->Read (
m_dwCookieRead, // Cookie returned from DAdvise
dwSource, // Source (device of cache)
cdwItems, // Number of items requested
phServer, // Item server handles
&dwTransID, // Server generated transaction id
&pErrors); // Item errors
// Check results:
for (dwIndex = 0; dwIndex < cdwItems; dwIndex++)
{
// Get pointer to item in input list:
CKItem *pItem = (CKItem *) cItemList [dwIndex];
ASSERT (pItem != NULL);
// Request succeeded and there were no errors:
if (SUCCEEDED (hr) && (pErrors && SUCCEEDED (pErrors [dwIndex])))
{
// Increment the successful read counter:
++cdwSuccess;
}
// Failure:
else
{
// Request succeeded, but one or more items could not be read:
if (pErrors && FAILED (pErrors [dwIndex]))
{
// Log error message with error code. (Could use GetErrorString()
// to get user friendly error string if we wish.)
LogMsg (bDeviceRead ? IDS_ASYNC10_READDEVICE_FAILED : IDS_ASYNC10_READCACHE_FAILED,
pItem->GetItemID (), GetName (), pErrors [dwIndex]);
}
// Request failed:
else
{
// Log error message with error code. (Could use GetErrorString()
// to get user friendly error string if we wish.)
LogMsg (bDeviceRead ? IDS_ASYNC10_READDEVICE_FAILED : IDS_ASYNC10_READCACHE_FAILED,
pItem->GetItemID (), GetName (), hr);
}
}
}
}
catch (...)
{
// Probably hit a bad pointer. Try to process next item.
TRACE (_T("OTC: ReadAsyncSync10 exception handler invoked\r\n"));
if (m_pServer->IsConnected ())
{
// We not disconnected so why was an exception thrown?
ASSERT (FALSE);
}
}
// COM requires us to free memory allocated for [out] and [in/out]
// arguments (i.e. phServer, and pErrors):
CoTaskMemFree (phServer);
if (pErrors)
CoTaskMemFree (pErrors);
// Log success status:
if (cdwSuccess > 0)
{
// Log diferent message for read device and read cache:
LogMsg (bDeviceRead ? IDS_ASYNC10_READDEVICE_SUCCESS : IDS_ASYNC10_READCACHE_SUCCESS,
dwTransID, cdwSuccess, GetName ());
}
}
}
// **************************************************************************
// RefreshAsync10 ()
//
// Description:
// Perform an asynchronous 1.0 refresh.
//
// Parameters:
// bool bDeviceRead Set to true to perform device refresh, false
// for cache refresh.
//
// Returns:
// void
// **************************************************************************
void CKGroup::RefreshAsync10 (bool bDeviceRead)
{
ASSERT (m_cdwItems > 0);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -