private void GetItemChildren(List <OPCItem> items, string szNameFilter, IOPCBrowseServerAddressSpace pParent, OPCItem parentItem = null) { opcprox.IEnumString pEnum; uint cnt; string strName; string szItemID; try { pParent.BrowseOPCItemIDs(tagOPCBROWSETYPE.OPC_LEAF, szNameFilter, (ushort)VarEnum.VT_EMPTY, 0, out pEnum); pEnum.RemoteNext(1, out strName, out cnt); int nLeavesCount = 0; while (cnt != 0) { pParent.GetItemID(strName, out szItemID); // получает полный идентификатор тега GetItemProperties(szItemID); items.Add( new OPCItem() { Parent = parentItem, ItemName = strName, Enabled = true, ItemType = OPCItemType.LEAF, ItemID = szItemID } ); pEnum.RemoteNext(1, out strName, out cnt); nLeavesCount++; } pParent.BrowseOPCItemIDs(tagOPCBROWSETYPE.OPC_BRANCH, szNameFilter, (ushort)VarEnum.VT_EMPTY, 0, out pEnum); pEnum.RemoteNext(1, out strName, out cnt); int nBranchesCount = 0; while (cnt != 0) { pParent.GetItemID(strName, out szItemID); OPCItem item = new OPCItem() { Parent = parentItem, ItemName = strName, Enabled = true, ItemType = OPCItemType.BRANCH, ItemID = szItemID }; items.Add(item); pParent.ChangeBrowsePosition(tagOPCBROWSEDIRECTION.OPC_BROWSE_TO, szItemID); GetItemChildren(items, "", pParent, item); pParent.ChangeBrowsePosition(tagOPCBROWSEDIRECTION.OPC_BROWSE_UP, szItemID); pEnum.RemoteNext(1, out strName, out cnt); nBranchesCount++; } } catch (System.Exception ex) { Console.Out.WriteLine(ex.Message); } }
public DataCallback(OPCItem item) { m_item = item; SetItemID(item.ItemID); dwTransid = 1; hGroup = 1; phClientItems = Marshal.AllocCoTaskMem(7 * sizeof(uint)); pvValues = Marshal.AllocCoTaskMem(7 * sizeof(long)); pwQualities = Marshal.AllocCoTaskMem(7 * sizeof(ushort)); pftTimeStamps = Marshal.AllocCoTaskMem(7 * sizeof(long)); }
public void AdviseOnServer(OPCServer server, OPCItem item) { if (m_pItemMgt == null) { return; } try { IConnectionPointContainer pCPC; pCPC = (IConnectionPointContainer)m_pItemMgt; Guid riid = typeof(IOPCDataCallback).GUID; //IEnumConnectionPoints ppEnum; //pCPC.EnumConnectionPoints(out ppEnum); pCPC.FindConnectionPoint(ref riid, out m_pDataCallback); //Если ранее была активирована подписка, то отменить ее if (m_dwCookie != 0) { m_pDataCallback.Unadvise(m_dwCookie); } DataCallback m_pSink = new DataCallback(item); m_pDataCallback.Advise(m_pSink, out m_dwCookie); } catch (COMException ex) { string msg; //Запрашиваем у сервера текст ошибки, соответствующий текущему HRESULT server.Server.GetErrorString(ex.HResult, 2, out msg); Console.Out.WriteLine(msg, "Ошибка"); } catch (ServerException ex) { string msg; //Запрашиваем у сервера текст ошибки, соответствующий текущему HRESULT server.Server.GetErrorString(ex.HResult, 2, out msg); Console.Out.WriteLine(msg, "Ошибка"); } }
public int SyncRead() { //if (!this.server.IsConnected) this.server.Connect(); //if(m_pItemMgt != null) RemoveFromServer(); IOPCSyncIO m_pSyncIO = null; int iRet = 0; try { m_pSyncIO = (IOPCSyncIO)m_pItemMgt; //При наличии подписки событий группы на сервере удаляем её if (m_dwCookie != 0) { m_pDataCallback.Unadvise(m_dwCookie); m_dwCookie = 0; } } catch (ApplicationException ex) { Console.Out.WriteLine(ex.Message); } tagOPCDATASOURCE ds = new tagOPCDATASOURCE(); ds = tagOPCDATASOURCE.OPC_DS_DEVICE; IntPtr iptrItemValue = IntPtr.Zero; IntPtr iptrErrors = IntPtr.Zero; try { m_pSyncIO.Read(ds, (uint)m_dwReadCount, ref m_hItems[0], out iptrItemValue, out iptrErrors); } catch (System.Exception ex) { iRet = -1; Console.Out.WriteLine(ex.Message); } try { OPCItem[] items = Items.ToArray(); tagOPCITEMSTATE[] itemSates = new tagOPCITEMSTATE[m_dwReadCount]; Type type = typeof(tagOPCITEMSTATE); Marshal.Copy(iptrErrors, m_hRes, 0, m_dwReadCount); for (int i = 0; i < items.Length; i++) { OPCItem item = items[i]; itemSates[i] = (tagOPCITEMSTATE)Marshal.PtrToStructure(iptrItemValue + i * Marshal.SizeOf(type), type); uint index = indecies[i]; item.Value = itemSates[i].vDataValue != null ? itemSates[i].vDataValue.ToString() : null; string tst = OPCLibrary.Converter.GetFTSting(itemSates[i].ftTimeStamp); item.TimeStamp = tst; item.Quality = itemSates[i].wQuality.ToString(); //Генерируем исключение в случае ошибки в HRESULT Marshal.ThrowExceptionForHR(m_hRes[i]); } } catch (System.Exception ex) { Console.Out.WriteLine(ex.Message); string msg; //Получаем HRESULT соответствующий сгененрированному исключению int hRes = Marshal.GetHRForException(ex); throw new ServerException(hRes); } finally { Marshal.FreeCoTaskMem(iptrItemValue); Marshal.FreeCoTaskMem(iptrErrors); } return(iRet); }
private void RegItemsInServer() { int dwCount = Items.Count(); Type type = typeof(tagOPCITEMDEF); IntPtr pItems = Marshal.AllocCoTaskMem((int)dwCount * Marshal.SizeOf(type)); m_dwReadCount = 0; indecies.Clear(); tagOPCITEMDEF[] itemDefs = new tagOPCITEMDEF[dwCount]; for (int i = 0; i < dwCount; i++) { OPCItem item = ((List <OPCItem>)Items)[i]; if (item.Enabled) { itemDefs[m_dwReadCount] = item.GetItemDef(); Marshal.StructureToPtr(itemDefs[m_dwReadCount], pItems + m_dwReadCount * Marshal.SizeOf(type), false); indecies.Add((uint)m_dwReadCount); m_dwReadCount++; } } IntPtr iptrErrors = IntPtr.Zero; IntPtr iptrResults = IntPtr.Zero; try { // Добавляем элемент данных в группу m_pItemMgt.AddItems((uint)m_dwReadCount, pItems, out iptrResults, out iptrErrors); } catch (ApplicationException ex) { Console.Out.WriteLine(ex.Message); } m_hItems = new uint[m_dwReadCount]; tagOPCITEMRESULT[] pResults = new tagOPCITEMRESULT[m_dwReadCount]; m_hRes = new int[dwCount]; Marshal.Copy(iptrErrors, m_hRes, 0, dwCount); try { for (int i = 0; i < m_dwReadCount; i++) { pResults[i] = (tagOPCITEMRESULT)Marshal.PtrToStructure(iptrResults + i * Marshal.SizeOf(typeof(tagOPCITEMRESULT)), typeof(tagOPCITEMRESULT)); m_hItems[i] = pResults[i].hServer; //Генерируем исключение в случае ошибки в HRESULT Marshal.ThrowExceptionForHR(m_hRes[i]); } } catch (System.Exception ex) { Console.Out.WriteLine(ex.Message); string msg; //Получаем HRESULT соответствующий сгененрированному исключению int hRes = Marshal.GetHRForException(ex); throw new ServerException(hRes); } Marshal.FreeCoTaskMem(pItems); Marshal.FreeCoTaskMem(iptrErrors); Marshal.FreeCoTaskMem(iptrResults); }
public OPCItem(OPCItem parent = null) { Parent = parent; m_dlg = new SetItemCallback(SetItemValue); }