Beispiel #1
0
        // ------------------------ IOPCSyncIO ---------------

        public bool Read(OPCDATASOURCE src, int[] arrHSrv, out OPCItemState[] arrStat)
        {
            arrStat = null;
            int    count = arrHSrv.Length;
            IntPtr ptrStat;
            IntPtr ptrErr;
            int    hresult = ifSync.Read(src, count, arrHSrv, out ptrStat, out ptrErr);

            if (HRESULTS.Failed(hresult))
            {
                Marshal.ThrowExceptionForHR(hresult);
            }

            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 !!!
                arrStat[i] = new OPCItemState();

                arrStat[i].Error = Marshal.ReadInt32((IntPtr)runErr);
                runErr          += 4;

                arrStat[i].HandleClient = Marshal.ReadInt32((IntPtr)runStat);

                if (HRESULTS.Succeeded(arrStat[i].Error))
                {
                    short vt = Marshal.ReadInt16((IntPtr)(runStat + 16));
                    if (vt == (short)VarEnum.VT_ERROR)
                    {
                        arrStat[i].Error = Marshal.ReadInt32((IntPtr)(runStat + 24));
                    }

                    arrStat[i].TimeStamp = Marshal.ReadInt64((IntPtr)(runStat + 4));
                    arrStat[i].Quality   = Marshal.ReadInt16((IntPtr)(runStat + 12));
                    arrStat[i].DataValue = Marshal.GetObjectForNativeVariant((IntPtr)(runStat + 16));
                    DUMMY_VARIANT.VariantClear((IntPtr)(runStat + 16));
                }
                else
                {
                    arrStat[i].DataValue = null;
                }

                runStat += 32;
            }

            Marshal.FreeCoTaskMem(ptrStat);
            Marshal.FreeCoTaskMem(ptrErr);
            return(hresult == HRESULTS.S_OK);
        }
        private void buttProbeSincRead_Click(object sender,EventArgs e)
        {
            OpcGroup theSinchroGrp;    // ���������� ��� ���, ���� ������
             OPCItemDef[] aD;           // ��(���������)�����, ���� ������
             OPCItemResult[] arrRes;    // ����������� ����������
             OPCItemState[] sts;        // �������(���������)�� ������ ��������� �����
             int[] serverhandles;       // ��� ������ ����� �.�.���� ����������
             string sPLC_Adr = "S7:[PLC01]DB2,int102";
             string[] sPLC_AdrLi = { "S7:[PLC01]DB2,int102"  // ACT_WATCHDOG01_PLC
                               ,"S7:[PLC11]DB2,int48"    // ACT_C1_WATCHDOG1_PLC
                               ,"S7:[PLC21]DB2,int48"    // ACT_C2_WATCHDOG1_PLC
                               ,"S7:[PLC31]DB2,int48"    // ACT_C3_WATCHDOG1_PLC
                               };
             string sErgValue = "";
             bool bErg = true;
             clsPointInfo[] probePointLi = new clsPointInfo[sPLC_AdrLi.GetLength(0)];

             try {
            theSinchroGrp = theSrv.AddGroup("OPCdotNET-Sinchro",true,60000);
            aD = new OPCItemDef[1];

            aD[0] = new OPCItemDef(sPLC_Adr,true,itmHandleClient,VarEnum.VT_EMPTY);
            theGrp.AddItems(aD,out arrRes);
            if (arrRes == null) {
               MessageBox.Show(this,"AddItems" + i,"arrRes == null",MessageBoxButtons.OK,MessageBoxIcon.Warning);
               }
            else {
               if (arrRes[0].Error != HRESULTS.S_OK) {
                  MessageBox.Show(this,"AddItems" + i,"arrRes[0].Error != HRESULTS.S_OK",MessageBoxButtons.OK,MessageBoxIcon.Warning);
                  }
               }

            //// add event handler for data changes
            //theSinchroGrp.DataChanged += new DataChangeEventHandler(this.theGrp_DataChange);
            //theSinchroGrp.WriteCompleted += new WriteCompleteEventHandler(this.theGrp_WriteComplete);
            sts = new OPCItemState[1];       // �� ����, ����� �� �������������
            itmHandleServer = 0;             // ��� �� 0. ������� � "sPLC_AdrLi"
            serverhandles = new int[1] { itmHandleServer };
            theSinchroGrp.Read(OPCDATASOURCE.OPC_DS_DEVICE,serverhandles,out sts);  // ������ ��������� ����� ��������� �����
            foreach (OPCItemState s in sts) {
               try {
                  if (HRESULTS.Succeeded(s.Error)) {
                     if (s.HandleClient < sPLC_AdrLi.GetLength(0)) {
                        if (s.DataValue != null) {
                           setTextInTxbViaDelegate(ref txtItemQual,OpcGroup.QualityToString(s.Quality));
                           setTextInTxbViaDelegate(ref txtItemTimeSt,DateTime.FromFileTime(s.TimeStamp).ToString());
                           probePointLi[s.HandleClient].opcItem = s;
                           if (s.DataValue.GetType() == typeof(SByte[])) {
                              probePointLi[s.HandleClient].sDataValue = ConvertToRus((SByte[])s.DataValue,probePointLi[s.HandleClient].iDataLength);
                              setTextInTxbViaDelegate(ref txtItemValue,probePointLi[s.HandleClient].sDataValue);
                              sErgValue = probePointLi[s.HandleClient].sDataValue;
                              }
                           else {
                              if (s.DataValue.GetType() == typeof(DateTime)) {
                                 probePointLi[s.HandleClient].dtDataValue = Convert.ToDateTime(s.DataValue);
                                 setTextInTxbViaDelegate(ref txtItemValue,LoggDateToString(probePointLi[s.HandleClient].dtDataValue));
                                 sErgValue = LoggDateToString(probePointLi[s.HandleClient].dtDataValue);
                                 }
                              else {
                                 probePointLi[s.HandleClient].iDataValue = Convert.ToInt32(s.DataValue);
                                 setTextInTxbViaDelegate(ref txtItemValue,probePointLi[s.HandleClient].iDataValue.ToString());
                                 sErgValue = probePointLi[s.HandleClient].iDataValue.ToString();
                                 }
                              }
                           AddLogg("Pkt" + s.HandleClient + "," + s.DataValue.GetType() + " ��������: '" + sErgValue + "'");
                           }
                        else {
                           AddLogg("s.HandleClient{" + s.HandleClient + "}>=probePointLi.Count{" + sPLC_AdrLi.GetLength(0) + "}");
                           }
                        }
                     }
                  else {
                     setTextInTxbViaDelegate(ref txtItemTimeSt,DateTime.FromFileTime(s.TimeStamp).ToString());
                     setTextInTxbViaDelegate(ref txtItemQual,"error");
                     setTextInTxbViaDelegate(ref txtItemValue,"ERROR 0x" + s.Error.ToString("X"));
                     AddLogg("s.HandleClient{" + s.HandleClient + "} ERROR 0x" + s.Error.ToString("X"));
                     }
                  }
               catch (Exception exc) {
                  AddLogg("Pkt" + s.HandleClient + ","
                     + ((s.DataValue == null) ? " DataValue==null" : s.DataValue.GetType().ToString())
                     + " " + exc.Message);
                  }
               }
            GC.Collect(); // just for fun
            }
             catch (Exception eXc) {
            MessageBox.Show(this,"OPC Exception!",eXc.Message,MessageBoxButtons.OK,MessageBoxIcon.Warning);
            return;
            }
        }
Beispiel #3
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;
            //}
        }
        // ------------------------ IOPCSyncIO ---------------
        public bool Read( OPCDATASOURCE src, int[] arrHSrv, out OPCItemState[] arrStat )
        {
            arrStat		= null;
            int			count = arrHSrv.Length;
            IntPtr		ptrStat;
            IntPtr		ptrErr;
            int	hresult = ifSync.Read( src, count, arrHSrv, out ptrStat, out ptrErr );
            if( HRESULTS.Failed( hresult ) )
            Marshal.ThrowExceptionForHR( hresult );

            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 !!!
            arrStat[i]				= new OPCItemState();

            arrStat[i].Error		= Marshal.ReadInt32( (IntPtr) runErr );
            runErr += 4;

            arrStat[i].HandleClient	= Marshal.ReadInt32( (IntPtr) runStat );

            if( HRESULTS.Succeeded( arrStat[i].Error ) )
                {
                short vt = Marshal.ReadInt16( (IntPtr) (runStat + 16) );
                if( vt == (short) VarEnum.VT_ERROR )
                    arrStat[i].Error = Marshal.ReadInt32( (IntPtr) (runStat + 24) );

                arrStat[i].TimeStamp	= Marshal.ReadInt64( (IntPtr) (runStat + 4) );
                arrStat[i].Quality		= Marshal.ReadInt16( (IntPtr) (runStat + 12) );
                arrStat[i].DataValue	= Marshal.GetObjectForNativeVariant( (IntPtr) (runStat + 16) );
                DUMMY_VARIANT.VariantClear( (IntPtr) (runStat + 16) );
                }
            else
                arrStat[i].DataValue = null;

            runStat += 32;
            }

            Marshal.FreeCoTaskMem( ptrStat );
            Marshal.FreeCoTaskMem( ptrErr );
            return hresult == HRESULTS.S_OK;
        }
Beispiel #5
0
        void IOPCDataCallback.OnReadComplete(
            int dwTransid, int hGroup, int hrMasterquality, int hrMastererror, int dwCount,
            IntPtr phClientItems, IntPtr pvValues, IntPtr pwQualities, IntPtr pftTimeStamps, IntPtr ppErrors)
        {
            Trace.WriteLine("OpcGroup.OnReadComplete");
            if ((dwCount == 0) || (hGroup != state.HandleClient))
            {
                return;
            }
            int count = (int)dwCount;

            int runh = (int)phClientItems;
            int runv = (int)pvValues;
            int runq = (int)pwQualities;
            int runt = (int)pftTimeStamps;
            int rune = (int)ppErrors;

            ReadCompleteEventArgs e = new ReadCompleteEventArgs();

            e.transactionID     = dwTransid;
            e.groupHandleClient = hGroup;
            e.masterQuality     = hrMasterquality;
            e.masterError       = hrMastererror;
            e.sts = new OPCItemState[count];

            for (int i = 0; i < count; i++)
            {
                OPCItemState item = new OPCItemState();
                e.sts[i]   = item;
                item.Error = Marshal.ReadInt32((IntPtr)rune);
                rune      += 4;

                item.HandleClient = Marshal.ReadInt32((IntPtr)runh);
                runh += 4;

                if (HRESULTS.Succeeded(item.Error))
                {
                    short vt = Marshal.ReadInt16((IntPtr)runv);
                    if (vt == (short)VarEnum.VT_ERROR)
                    {
                        item.Error = Marshal.ReadInt32((IntPtr)(runv + 8));
                    }

                    item.DataValue     = Marshal.GetObjectForNativeVariant((IntPtr)runv);
                    item.QualityString = QualityToString(Marshal.ReadInt16((IntPtr)runq));
                    try
                    {
                        item.TimeStamp = DateTime.FromFileTime(Marshal.ReadInt64((IntPtr)runt));
                    }
                    catch
                    {
                    }
                }

                runv += DUMMY_VARIANT.ConstSize;
                runq += 2;
                runt += 8;
            }

            if (ReadCompleted != null)
            {
                ReadCompleted(this, e);
            }
        }