Exemplo n.º 1
0
        public HistoryData[] BatchRead(DataSource source, bool isSync, params ITag[] itemArray)
        {
            IntPtr pErrors;
            int    len    = itemArray.Length;
            int    result = 0;

            int[]         arrHSrv = Array.ConvertAll(itemArray, c => c.Address.Start);
            HistoryData[] values  = new HistoryData[len];
            if (isSync)
            {
                IntPtr pItemValues;
                result = _sync.Read((OPCDATASOURCE)source, len, arrHSrv, out pItemValues, out pErrors);
                if (HRESULTS.Succeeded(result))
                {
                    for (int i = 0; i < len; i++)
                    {
                        var item = itemArray[i];
                        if (Marshal.ReadInt32(pErrors) == 0)
                        {
                            switch (item.Address.VarType)
                            {
                            case DataType.BOOL:
                                values[i].Value.Boolean = Marshal.ReadByte(pItemValues + 16) > 0;
                                break;

                            case DataType.BYTE:
                                values[i].Value.Byte = Marshal.ReadByte(pItemValues + 16);
                                break;

                            case DataType.WORD:
                                values[i].Value.Word = (ushort)Marshal.ReadInt16(pItemValues + 16);
                                break;

                            case DataType.SHORT:
                                values[i].Value.Int16 = Marshal.ReadInt16(pItemValues + 16);
                                break;

                            case DataType.DWORD:
                                values[i].Value.DWord = (uint)Marshal.ReadInt32(pItemValues + 16);
                                break;

                            case DataType.INT:
                                values[i].Value.Int32 = Marshal.ReadInt32(pItemValues + 16);
                                break;

                            case DataType.FLOAT:
                                float[] x = new float[1];
                                Marshal.Copy(pItemValues + 16, x, 0, 1);
                                values[i].Value.Single = x[0];
                                break;

                            case DataType.STR:
                                string    str = Marshal.PtrToStringUni(Marshal.ReadIntPtr(pItemValues + 16));
                                StringTag tag = item as StringTag;
                                if (tag != null)
                                {
                                    tag.String = str;
                                }
                                break;
                            }
                            values[i].ID        = item.ID;
                            values[i].Quality   = (QUALITIES)Marshal.ReadInt16(pItemValues + 12);
                            values[i].TimeStamp = Marshal.ReadInt64(pItemValues + 4).ToDateTime();
                            item.Update(values[i].Value, values[i].TimeStamp, values[i].Quality);
                        }
                        pItemValues += 32;
                        pErrors     += 4;
                    }
                    Marshal.FreeCoTaskMem(pItemValues);
                }
                else
                {
                    Marshal.FreeCoTaskMem(pItemValues);
                    return(null);
                }
            }
            else
            {
                int cancelID = 0;
                result = _async.Read(len, arrHSrv, 1, out cancelID, out pErrors);
            }
            Marshal.FreeCoTaskMem(pErrors);
            return(values);
        }
Exemplo n.º 2
0
        public void LSE_Connect()
        {
            IOPCServer                m_OPCServer;
            IOPCGroupStateMgt2        m_OPCGroup2;
            IOPCItemMgt               m_OPCItem;
            IConnectionPointContainer m_OPCConnPointCntnr;
            IConnectionPoint          m_OPCConnPoint;

            //---------Connect LSEOPC Server

            Type typeofOPCserver = Type.GetTypeFromProgID("Intellution.LSEOPC");

            m_OPCServer = (IOPCServer)Activator.CreateInstance(typeofOPCserver);

            //----------Add Group for DA3.0

            int    m_iServerGroup;
            int    iRevisedUpdateRate;
            Type   typGrpMgt         = typeof(IOPCGroupStateMgt2);
            Guid   guidGroupStateMgt = typGrpMgt.GUID;
            object group             = null;

            m_OPCServer.AddGroup("Group1", 1, 1000, 0, IntPtr.Zero, IntPtr.Zero, 0, out m_iServerGroup, out iRevisedUpdateRate, ref guidGroupStateMgt, out group);
            m_OPCGroup2 = (IOPCGroupStateMgt2)group;

            //------------Add items

            string[] ItemName = new string[iItemCount];
            ItemName[0] = "Device0:D00000";
            ItemName[1] = "Device0:D00001";
            ItemName[2] = "Device0:D00002";
            ItemName[3] = "Device0:D00003";
            //ItemName[4] = "Device0:D00004";
            //ItemName[5] = "Device0:D00005";
            //ItemName[6] = "Device0:D00006";
            //ItemName[7] = "Device0:D00007";
            //ItemName[8] = "Device0:D00008";
            //ItemName[9] = "Device0:D00009";
            //ItemName[10] = "Device0:D00010";
            //ItemName[11] = "Device0:D00011";
            //ItemName[12] = "Device0:D00012";
            //ItemName[13] = "Device0:D00013";
            //ItemName[14] = "Device0:D00014";
            //ItemName[15] = "Device0:D00015";

            OPCITEMDEF[] itemDef = new OPCITEMDEF[iItemCount];

            for (int i = 0; i < iItemCount; i++)
            {
                itemDef[i].szItemID = ItemName[i];
                itemDef[i].bActive  = 1;
                itemDef[i].hClient  = i;
            }

            m_OPCItem = (IOPCItemMgt)m_OPCGroup2;

            IntPtr ppResult;
            IntPtr ppErrors;

            m_OPCItem.AddItems(iItemCount, itemDef, out ppResult, out ppErrors);

            //----------Sync Read items

            IOPCSyncIO2 m_Sync = (IOPCSyncIO2)m_OPCGroup2;   //for DA3.0

            OPCITEMRESULT itemResult;

            int[]  errors   = new int[iItemCount];
            int[]  ServerHd = new int[iItemCount];
            IntPtr posRes   = ppResult;

            for (int i = 0; i < iItemCount; i++)
            {
                itemResult = (OPCITEMRESULT)Marshal.PtrToStructure(posRes, typeof(OPCITEMRESULT));
                if (errors[i] == 0)
                {
                    ServerHd[i] = itemResult.hServer;
                }
                Marshal.DestroyStructure(posRes, typeof(OPCITEMRESULT));
                posRes = (IntPtr)(posRes.ToInt32() + Marshal.SizeOf(typeof(OpcRcw.Da.OPCITEMRESULT)));
            }

            IntPtr ppItemVal;

            m_Sync.Read(OPCDATASOURCE.OPC_DS_DEVICE, iItemCount, ServerHd, out ppItemVal, out ppErrors);

            //--------------Read Data values

            IntPtr       posItem;
            OPCITEMSTATE ItemState;

            Values = new object[iItemCount];

            Marshal.Copy(ppErrors, errors, 0, iItemCount);
            posItem = ppItemVal;

            try
            {
                for (int i = 0; i < iItemCount; i++)
                {
                    ItemState = (OPCITEMSTATE)Marshal.PtrToStructure(posItem, typeof(OPCITEMSTATE));
                    if (errors[i] == 0)
                    {
                        Values[i] = ItemState.vDataValue;
                        //TimeStamps[i] = ItemState.ftTimeStamp;
                        //Qualities[i] = ItemState.wQuality;
                    }
                    Marshal.DestroyStructure(posItem, typeof(OPCITEMSTATE));
                    posItem = (IntPtr)(posItem.ToInt32() + Marshal.SizeOf(typeof(OpcRcw.Da.OPCITEMSTATE)));
                }
            }
            catch
            {
                MessageBox.Show("Please confirm LSEOPC Server and Address (Devicename:Address)...");
                Marshal.FreeCoTaskMem(ppItemVal);
                Marshal.FreeCoTaskMem(ppErrors);
                iItemCount = 0;
                return;
            }
            Marshal.FreeCoTaskMem(ppItemVal);
            Marshal.FreeCoTaskMem(ppErrors);

            //------------Async Read items DA3.0 after Sync Read

            int iKeepAliveTime = 10000;

            m_OPCGroup2.SetKeepAlive(iKeepAliveTime, out iKeepAliveTime);

            //Add for auto refresh Async read
            m_OPCConnPointCntnr = (IConnectionPointContainer)m_OPCGroup2;
            Guid guidDataCallback = Marshal.GenerateGuidForType(typeof(IOPCDataCallback));

            m_OPCConnPointCntnr.FindConnectionPoint(ref guidDataCallback, out m_OPCConnPoint);

            int m_iCallBackConnection;

            //Async Read Callback Auto refresh set
            m_OPCConnPoint.Advise((LSEOPC)this, out m_iCallBackConnection);

            int wCancelID = 0;

            IOPCAsyncIO3 m_Async = (IOPCAsyncIO3)m_OPCGroup2;

            m_Async.Read(iItemCount, ServerHd, 1234567, out wCancelID, out ppErrors);

            return;
        }