Example #1
0
        public override string ToString()
        {
            StringBuilder sb = new StringBuilder("OPCIST: ", 256);

            sb.AppendFormat("error=0x{0:x} hclt=0x{1:x}", Error, HandleClient);
            if (Error == HRESULTS.S_OK)
            {
                sb.AppendFormat(" val={0} time={1} qual=", DataValue, TimeStamp);
                sb.Append(OpcGroup.QualityToString(Quality));
            }

            return(sb.ToString());
        }
Example #2
0
        public OPCItemState[] Read(int[] arrHSrv)
        {
            OPCItemState[] arrStat;
            arrStat = null;
            int    count = arrHSrv.Length;
            IntPtr ptrStat;
            IntPtr ptrErr;



            int hresult = ifSync.Read(OPCDATASOURCE.OPC_DS_DEVICE, count, arrHSrv, out ptrStat, out ptrErr);

            if (HRESULTS.Failed(hresult))
            {
                #region 新建组读取
                values = new List <ItemValue>();
                readed = false;

                OPC.Data.OpcGroup grouop = new OpcGroup("wef2", true, 500, 500, 0);
                this.group = grouop;
                this.Server.OpcGroups.Add(grouop);
                grouop.DataChanged += new DataChangeEventHandler(grouop_DataChanged);
                int index = 0;

                foreach (int hid in arrHSrv)
                {
                    OPCItem item = this.Items.GetItemByServerHandler(hid);
                    values.Add(new ItemValue(item.ID, null));
                }

                foreach (int hid in arrHSrv)
                {
                    OPCItem item    = this.Items.GetItemByServerHandler(hid);
                    OPCItem newItem = new OPCItem(item.ID, index);
                    grouop.Items.Add(newItem);
                    index++;
                }

                while (true)
                {
                    if (readed)
                    {
                        OPCItemState[] states = new OPCItemState[arrHSrv.Length];
                        for (int i = 0; i < states.Length; i++)
                        {
                            states[i] = values[i].value;
                        }
                        this.group = null;
                        grouop.Items.Clear();
                        this.Server.OpcGroups.Remove(grouop);
                        return(states);
                    }
                    Thread.Sleep(20);
                }
                #endregion

                if (HRESULTS.Failed(hresult))
                {
                    throw (new Exception("读取Item值出错,public OPCItemState[] Read(int[] arrHSrv)函数ifSync.Read(OPCDATASOURCE.OPC_DS_CACHE, count, arrHSrv, out ptrStat, out ptrErr)语句"));
                }
            }

            int runErr  = (int)ptrErr;
            int runStat = (int)ptrStat;
            if ((runErr == 0) || (runStat == 0))
            {
                Marshal.ThrowExceptionForHR(HRESULTS.E_ABORT);
            }

            arrStat = new OPCItemState[count];
            for (int i = 0; i < count; i++)
            {                                                                                                           // WORKAROUND !!!
                OPCItemState item = new OPCItemState();
                arrStat[i] = item;
                item.Error = Marshal.ReadInt32((IntPtr)runErr);
                runErr    += 4;

                item.HandleClient = Marshal.ReadInt32((IntPtr)runStat);

                if (HRESULTS.Succeeded(item.Error))
                {
                    short vt = Marshal.ReadInt16((IntPtr)(runStat + 16));
                    if (vt == (short)VarEnum.VT_ERROR)
                    {
                        item.Error = Marshal.ReadInt32((IntPtr)(runStat + 24));
                    }
                    try
                    {
                        item.TimeStamp = DateTime.FromFileTime(Marshal.ReadInt64((IntPtr)(runStat + 4)));
                    }
                    catch
                    {
                    }
                    try
                    {
                        item.QualityString = OpcGroup.QualityToString(Marshal.ReadInt16((IntPtr)(runStat + 12)));
                    }
                    catch
                    {
                    }
                    item.DataValue = Marshal.GetObjectForNativeVariant((IntPtr)(runStat + 16));
                    DUMMY_VARIANT.VariantClear((IntPtr)(runStat + 16));
                }
                else
                {
                    item.DataValue = null;
                }

                runStat += 32;
            }

            Marshal.FreeCoTaskMem(ptrStat);
            Marshal.FreeCoTaskMem(ptrErr);
            return(arrStat);
            //if (hresult == HRESULTS.S_OK)
            //{
            //    return arrStat;
            //}
            //else
            //{
            //    return null;
            //}
        }