回复:技术论坛开设T-CPU、能源管理、工业无线局域网、OPC通讯等4项有奖交流(1月10日更新)

ZPK_SAEM_秦立

  • 帖子

    14
  • 精华

    0
  • 被关注

    1

论坛等级:游民

注册时间:2011-03-12

普通 普通 如何晋级?

发布于 2012-01-12 22:06:41

172楼

我最近做一个OPC通讯实例,有两台OPC客户机希望通过OPC服务器,访问SIMOTION的变量。通过SCOUT,OPC服务器可以浏览SIMOTION的变量,OPC客户机可以通过OPC服备器查看SIMOTION的变量。但我的VC2005应用程序,在OPC服务器的电脑上可以访问SIMOTION变量。同一个程序,放在OPC客户机的电脑上,不管怎样更改,都无法通过OPC服务器读取SIMOTION的变量。我想问题出现在OPC的起动阶段,其部分代码如下:

const LPWSTR szAxis1Pos = L"SYM1.D425.Axis_1.basicmotion.position";
const LPWSTR szAxis1Vel = L"SYM1.D425.Axis_1.basicmotion.velocity";
const LPWSTR szAxis1SwN = L"SYM1.D425.Axis_1.swlimit.minusposition";
const LPWSTR szAxis1SwP = L"SYM1.D425.Axis_1.swlimit.plusposition";

const DWORD vtAxis1Pos = VT_R4;
const DWORD vtAxis1Vel = VT_R4;


void COPCView::OnBnClickedButtonStart()
{
HRESULT r1;
CLSID clsid;
LONG TimeBias = 0;
FLOAT PercentDeadband = 0.0;
DWORD RevisedUpdateRate;
LPWSTR ErrorStr1,ErrorStr2;
CString szErrorText;

m_pItemResult = NULL;

// Initialize the COM library
r1 = CoInitialize(NULL);
if (r1 != S_OK)
{ if (r1 == S_FALSE)
{
// MessageBox(_T("COM Library already initialized"),
// _T("Error CoInitialize()"), MB_OK+MB_ICONEXCLAMATION);
}
else
{ szErrorText.Format(_T("Initialisation of COM Library failed. ErrorCode= %4x"), r1);
MessageBox(szErrorText,_T("Error CoInitialize()"), MB_OK+MB_ICONERROR);
SendMessage(WM_CLOSE);
return;
}
}

// Given a ProgID, this looks up the associated CLSID in the registry
r1 = CLSIDFromProgID(L"OPC.SimaticNET", &clsid);
if (r1 != S_OK)
{
MessageBox(_T("Retrival of CLSID failed"),
_T("Error CLSIDFromProgID()"), MB_OK+MB_ICONERROR);
CoUninitialize();
SendMessage(WM_CLOSE);
return;
}

// Create the OPC server object and querry for the IOPCServer interface of the object
r1 = CoCreateInstance (clsid, NULL, CLSCTX_LOCAL_SERVER ,IID_IOPCServer, (void**)&m_pIOPCServer);
if (r1 != S_OK)
{ MessageBox(_T("Creation of IOPCServer-Object failed"),
_T("Error CoCreateInstance()"), MB_OK+MB_ICONERROR);
m_pIOPCServer = NULL;
CoUninitialize();
SendMessage(WM_CLOSE);
return;
}

// Add a group object "grp1" and querry for interface IOPCItemMgt
r1=m_pIOPCServer->AddGroup( L"grp1", // [in] group name
FALSE, // [in] not active, so no OnDataChange callback
500, // [in] Request this Update Rate from Server
1, // [in] Client Handle, not necessary in this sample
&TimeBias, // [in] No time interval to system UTC time
&PercentDeadband, // [in] No Deadband, so all data changes are reported
LOCALE_ID, // [in] Server uses english language to for text values
&m_GrpSrvHandle, // [out] Server handle to identify this group in later calls
&RevisedUpdateRate, // [out] The answer from Server to the requested Update Rate
IID_IOPCItemMgt, // [in] requested interface type of the group object
(LPUNKNOWN*)&m_pIOPCItemMgt); // [out] pointer to the requested interface


if (r1 == OPC_S_UNSUPPORTEDRATE)
{
szErrorText.Format (_T("Revised Update Rate %d is different from Requested Update Rate 500"),RevisedUpdateRate );
AfxMessageBox (szErrorText);
}
else
if (FAILED(r1))
{ MessageBox(_T("Can't add Group to Server!"),
_T("Error AddGroup()"), MB_OK+MB_ICONERROR);
m_pIOPCServer->Release();
m_pIOPCServer = NULL;
CoUninitialize();
SendMessage(WM_CLOSE);
return;
}

// define item tables with 2 items as in parameters for AddItem

m_Items[0].szAccessPath = L""; // Accesspath not needed
m_Items[0].szItemID = szAxis1Pos; // ItemID, see above
m_Items[0].bActive = TRUE; // AddItem Active, so OnDataChange will come in an active group for this item
m_Items[0].hClient = 0; // Client handle , needed in this sample in callback OnDataChange
m_Items[0].dwBlobSize = 0; // "BinaryLargeOBject" not needed by SimaticNet OPC Server
m_Items[0].pBlob = NULL; // no blob
m_Items[0].vtRequestedDataType = vtAxis1Pos; // Return values in native (cannonical) datatype defined by the item itself

m_Items[1].szAccessPath = L"";
m_Items[1].szItemID = szAxis1Vel;
m_Items[1].bActive = TRUE;
m_Items[1].hClient = 1;
m_Items[1].dwBlobSize = 0;
m_Items[1].pBlob = NULL;
m_Items[1].vtRequestedDataType = vtAxis1Vel; // Return values - if possible - in data
m_Items[2].szAccessPath = L""; // Accesspath not needed
m_Items[2].szItemID = szAxis1SwN; // ItemID, see above
m_Items[2].bActive = TRUE; // AddItem Active, so OnDataChange will come
m_Items[2].hClient = 2; // Client handle , needed in this
m_Items[2].dwBlobSize = 0; // "BinaryLargeOBject" not needed by
m_Items[2].pBlob = NULL; // no blob
m_Items[2].vtRequestedDataType = vtAxis1Pos; // Return values in native
.
.
.
. m_Items[49].szAccessPath = L"";
m_Items[49].szItemID = sziOpcSynStep;
m_Items[49].bActive = TRUE;
m_Items[49].hClient = 49;
m_Items[49].dwBlobSize = 0;
m_Items[49].pBlob = NULL;
m_Items[49].vtRequestedDataType = VT_I4;
r1 = m_pIOPCItemMgt->AddItems( 50, // [in] Add two Items
m_Items, // [in] defined in this structure (see above)
&m_pItemResult, // [out] array with additional information about the item &m_pErrors // [out] tells which of the items was successfully added. For any );

…………

我认为下面这三行程序应包含OPC服务的地址,但不管我如何变更,都无法实献。
1、const LPWSTR szAxis1Pos = L"SYM1.D425.Axis_1.basicmotion.position";
2、r1 = CLSIDFromProgID(L"OPC.SimaticNET", &clsid);
3、r1 = CoCreateInstance (clsid, NULL, CLSCTX_LOCAL_SERVER ,IID_IOPCServer, (void**) &m_pIOPCServer);

不知哪位大侠能给予指点。
whatname
评论
编辑推荐: 关闭

请填写推广理由:

本版热门话题

谈天说地

共有13244条技术帖

相关推荐

热门标签

相关帖子推荐

guzhang

恭喜,你发布的帖子

评为精华帖!

快扫描右侧二维码晒一晒吧!

再发帖或跟帖交流2条,就能晋升VIP啦!开启更多专属权限!

  • 分享

  • 只看
    楼主

top
您收到0封站内信:
×
×
信息提示
很抱歉!您所访问的页面不存在,或网址发生了变化,请稍后再试。