Exemple #1
0
 public void TestRemoveItems01()
 {
     //Testing ---
     int[] arrHSrv = new int[1];
     int[] arrErr  = null;
     //Test Procedure Call
     bool b = group.RemoveItems(arrHSrv, out arrErr);
 }
Exemple #2
0
 private void cmdRemItem_Click(object sender, EventArgs e)
 {
     int[] aE = new int[2];
     if (theGroup != null)
     {
         theGroup.RemoveItems(HandlesServer, out aE);
     }
     cmdRemItem.Enabled    = false;
     cmdWriteSync.Enabled  = false;
     cmdWriteAsync.Enabled = false;
     cmdReadSync.Enabled   = false;
     cmdReadAsync.Enabled  = false;
     cmdAddItem.Enabled    = true;
     cmdRemGroup.Enabled   = true;
 }
Exemple #3
0
        // remove previous OPC item if any
        public bool RemoveItem()
        {
            try
            {
                if (itmHandleClient != 0)
                {
                    itmHandleClient       = 0;
                    txtItemID.Text        = "";                                 // clear screen texts
                    txtItemValue.Text     = "";
                    txtItemDataType.Text  = "";
                    txtItemQual.Text      = "";
                    txtItemTimeSt.Text    = "";
                    txtItemSendValue.Text = "";
                    txtItemWriteRes.Text  = "";
                    btnItemWrite.Enabled  = false;
                    btnItemMore.Enabled   = false;

                    int[] serverhandles = new int[1] {
                        itmHandleServer
                    };
                    int[] remerrors;
                    theGrp.RemoveItems(serverhandles, out remerrors);
                    itmHandleServer = 0;
                }
            }
            catch (COMException)
            {
                MessageBox.Show(this, "RemoveItem OPC error!", "RemoveItem", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return(false);
            }
            return(true);
        }
        // remove previous OPC item if any
        public bool RemoveItem()
        {
            try
            {
                if (itmHandleClient != 0)
                {
                    itmHandleClient = 0;
                    //txtItemID.Text = "";		// clear screen texts
                    //txtItemValue.Text = "";
                    //txtItemDataType.Text = "";
                    //txtItemQual.Text = "";
                    //txtItemTimeSt.Text = "";
                    //txtItemSendValue.Text = "";
                    //txtItemWriteRes.Text = "";
                    //btnItemWrite.Enabled = false;
                    //btnItemMore.Enabled = false;

                    int[] serverhandles = new int[1] {
                        itmHandleServer
                    };
                    int[] remerrors;
                    theGrp.RemoveItems(serverhandles, out remerrors);
                    itmHandleServer = 0;
                }
            }
            catch (COMException)
            {
                return(false);
            }
            return(true);
        }
        // remove previous OPC item if any
        private void RemoveItem()
        {
            if (_itmHandleClient == 0)
            {
                return;
            }

            _itmHandleClient = 0;

            int[] serverhandles = new int[1] {
                _itmHandleServer
            };

            int[] remerrors;

            _theGrp.RemoveItems(serverhandles, out remerrors);

            _itmHandleServer = 0;
        }
Exemple #6
0
        /// <summary>
        /// Removes OPC Item from OPC Group
        /// </summary>
        /// <param name="opcID">OPC item name</param>
        /// <returns>sucessfully removed or not</returns>
        /// OPCID is the Datapoint Name
        public bool RemoveOPCItem(string opcID)
        {
            string Function_Name = "RemoveOPCItem";

            LogHelper.Trace(CLASS_NAME, Function_Name, "Function_Entered");
            lock (m_opcDataDicObj)
            {
                if (!m_opcDataIndexDic.ContainsKey(opcID))
                {
                    LogHelper.Info(CLASS_NAME, Function_Name, "Function_Exited");
                    return(true);
                }

                //remove from OPC Group
                int handleClient = m_opcDataIndexDic[opcID];
                if (!m_opcDataDic.ContainsKey(handleClient))
                {
                    //error
                    LogHelper.Info(CLASS_NAME, Function_Name, "Function_Exited");
                    return(false);
                }

                OPCDataItem opcDataItemVar = m_opcDataDic[handleClient];
                try
                {
                    int[] serverhandles = new int[1] {
                        opcDataItemVar.HandleServer
                    };
                    int[] remerrors;
                    if (m_OPCGroup != null)
                    {
                        m_OPCGroup.RemoveItems(serverhandles, out remerrors);
                    }

                    //remove from internal map
                    m_opcDataIndexDic.Remove(opcID);
                    m_opcDataDic.Remove(handleClient);
                    m_OPCDataSeqNum--;
                }
                catch (Exception localException)
                {
                    LogHelper.Error(CLASS_NAME, Function_Name, localException);
                    //if (localException.Message.Contains("The RPC server is unavailable."))
                    // Fixed issue - related to System Language, to avoid it used Error code
                    if (localException.Message.Contains("0x800706BA"))
                    {
                        //when opc server is crashed.
                        throw localException;
                    }
                    return(false);
                }
            }
            LogHelper.Trace(CLASS_NAME, Function_Name, "Function_Exited");
            return(true);
        }
        private bool SetOneOPCTag(IOPCTag opcTag, VarEnum dataType)
        {
            var items = new OPCItemDef[1];

            items[0] = new OPCItemDef(opcTag.FullName, true, 0, dataType);

            OPCItemResult[] addRslt;
            int             rtc = OPCWriteGroup.AddItems(items, out addRslt);

            if (HRESULTS.Failed(rtc))
            {
                return(false);// "Error at AddItem";
            }
            var iHnd = new Int32[addRslt.Length];

            for (int i = 0; i < addRslt.Length; ++i)
            {
                iHnd[i] = addRslt[i].HandleServer;
            }
            int[] err;
            var   val = new object[addRslt.Length];

            val[0] = opcTag.ObjectValue;

            rtc = OPCWriteGroup.Write(iHnd, val, out err);

            //string errTxt = "OK";
            if (HRESULTS.Failed(rtc))
            {
                return(false);
                //errTxt = srv.GetErrorString(rtc, 0);
            }
            // succeeded
            // check item error codes
            if (err.Any(HRESULTS.Failed))
            {
                return(false);
            }

            OPCWriteGroup.RemoveItems(iHnd, out err);
            return(true);
            //            return errTxt;
        }
        public void Disconnect()
        {
            List <int> l_ServerHandleList = _serverModel.GetServerHandleList();

            if (l_ServerHandleList.Count > 0)
            {
                try
                {
                    int[] l_RemoveRes;
                    _group.RemoveItems(l_ServerHandleList.ToArray(), out l_RemoveRes);
                }
                catch (Exception)
                {
                }
            }

            if (_group != null)
            {
                _group.DataChanged -= new DataChangeEventHandler(Group_DataChange);

                try
                {
                    _group.Remove(true);
                }
                catch (Exception)
                {
                }
            }

            if (_server != null)
            {
                _server.ShutdownRequested -= new ShutdownRequestEventHandler(Server_ShutdownRequest);

                try
                {
                    _server.Disconnect();
                }
                catch (Exception)
                {
                }
            }
        }
Exemple #9
0
        // remove previous OPC item if any
        public bool RemoveItem()
        {
            try
            {
                if (itmHandleClient != 0)
                {
                    itmHandleClient = 0;


                    int[] serverhandles = new int[1] {
                        itmHandleServer
                    };
                    int[] remerrors;
                    theGrp.RemoveItems(serverhandles, out remerrors);
                    itmHandleServer = 0;
                }
            }
            catch (COMException)
            {
                MessageBox.Show(this, "RemoveItem OPC error!", "RemoveItem", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return(false);
            }
            return(true);
        }
Exemple #10
0
        public void Work()
        {
            /*	try						// disabled for debugging
             *      {	*/

            theSrv = new OpcServer();
            theSrv.Connect(serverProgID);
            Thread.Sleep(500);                                          // we are faster then some servers!

            // add our only working group
            theGrp = theSrv.AddGroup("OPCCSharp-Group", false, 900);

            // add two items and save server handles
            itemDefs[0] = new OPCItemDef(itemA, true, 1234, VarEnum.VT_EMPTY);
            itemDefs[1] = new OPCItemDef(itemB, true, 5678, VarEnum.VT_EMPTY);
            OPCItemResult[] rItm;
            theGrp.AddItems(itemDefs, out rItm);
            if (rItm == null)
            {
                return;
            }
            if (HRESULTS.Failed(rItm[0].Error) || HRESULTS.Failed(rItm[1].Error))
            {
                Console.WriteLine("OPC Tester: AddItems - some failed"); theGrp.Remove(true); theSrv.Disconnect(); return;
            }
            ;

            handlesSrv[0] = rItm[0].HandleServer;
            handlesSrv[1] = rItm[1].HandleServer;

            // asynch read our two items
            theGrp.SetEnable(true);
            theGrp.Active         = true;
            theGrp.DataChanged   += new DataChangeEventHandler(this.theGrp_DataChange);
            theGrp.ReadCompleted += new ReadCompleteEventHandler(this.theGrp_ReadComplete);
            int CancelID;

            int[] aE;
            theGrp.Read(handlesSrv, 55667788, out CancelID, out aE);

            // some delay for asynch read-complete callback (simplification)
            Thread.Sleep(500);


            // asynch write
            object[] itemValues = new object[2];
            itemValues[0]          = (int)1111111;
            itemValues[1]          = (double)2222.2222;
            theGrp.WriteCompleted += new WriteCompleteEventHandler(this.theGrp_WriteComplete);
            theGrp.Write(handlesSrv, itemValues, 99887766, out CancelID, out aE);

            // some delay for asynch write-complete callback (simplification)
            Thread.Sleep(500);


            // disconnect and close
            Console.WriteLine("************************************** hit  to close...");
            Console.ReadLine();
            theGrp.DataChanged    -= new DataChangeEventHandler(this.theGrp_DataChange);
            theGrp.ReadCompleted  -= new ReadCompleteEventHandler(this.theGrp_ReadComplete);
            theGrp.WriteCompleted -= new WriteCompleteEventHandler(this.theGrp_WriteComplete);
            theGrp.RemoveItems(handlesSrv, out aE);
            theGrp.Remove(false);
            theSrv.Disconnect();
            theGrp = null;
            theSrv = null;


            /*	}
             * catch( Exception e )
             *      {
             *      Console.WriteLine( "EXCEPTION : OPC Tester " + e.ToString() );
             *      return;
             *      }	*/
        }
Exemple #11
0
        public void Work()
        {
            /*	try						// disabled for debugging
             *  {	*/

            theSrv = new OpcServer();
            theSrv.Connect(serverProgID);
            Thread.Sleep(500);              // we are faster then some servers!

            // add our only working group
            theGrp = theSrv.AddGroup("OPCCSharp-Group", false, timeref);


            if (sendtags > tags.Length)
            {
                sendtags = tags.Length;
            }

            var itemDefs = new OPCItemDef[tags.Length];

            for (var i = 0; i < tags.Length; i++)
            {
                itemDefs[i] = new OPCItemDef(tags[i], true, i, VarEnum.VT_EMPTY);
            }

            OPCItemResult[] rItm;
            theGrp.AddItems(itemDefs, out rItm);
            if (rItm == null)
            {
                return;
            }
            if (HRESULTS.Failed(rItm[0].Error) || HRESULTS.Failed(rItm[1].Error))
            {
                Console.WriteLine("OPC Tester: AddItems - some failed"); theGrp.Remove(true); theSrv.Disconnect(); return;
            }
            ;

            var handlesSrv = new int[itemDefs.Length];

            for (var i = 0; i < itemDefs.Length; i++)
            {
                handlesSrv[i] = rItm[i].HandleServer;
            }

            currentValues = new Single[itemDefs.Length];

            // asynch read our two items
            theGrp.SetEnable(true);
            theGrp.Active         = true;
            theGrp.DataChanged   += new DataChangeEventHandler(this.theGrp_DataChange);
            theGrp.ReadCompleted += new ReadCompleteEventHandler(this.theGrp_ReadComplete);

            int CancelID;

            int[] aE;
            theGrp.Read(handlesSrv, 55667788, out CancelID, out aE);

            // some delay for asynch read-complete callback (simplification)
            Thread.Sleep(500);

            while (webSend == "yes")
            {
                HttpListenerContext  context  = listener.GetContext();
                HttpListenerRequest  request  = context.Request;
                HttpListenerResponse response = context.Response;
                context.Response.AddHeader("Access-Control-Allow-Origin", "*");


                byte[] buffer = System.Text.Encoding.UTF8.GetBytes(responseStringG);
                // Get a response stream and write the response to it.
                response.ContentLength64 = buffer.Length;
                System.IO.Stream output = response.OutputStream;
                output.Write(buffer, 0, buffer.Length);
                // You must close the output stream.
                output.Close();
            }
            // disconnect and close
            Console.WriteLine("************************************** hit <return> to close...");
            Console.ReadLine();
            theGrp.ReadCompleted -= new ReadCompleteEventHandler(this.theGrp_ReadComplete);
            theGrp.RemoveItems(handlesSrv, out aE);
            theGrp.Remove(false);
            theSrv.Disconnect();
            theGrp = null;
            theSrv = null;


            /*	}
             * catch( Exception e )
             *  {
             *  Console.WriteLine( "EXCEPTION : OPC Tester " + e.ToString() );
             *  return;
             *  }	*/
        }