}           //	end ProcessCommandLine

        public void Terminate()
        {
            if (m_daSubscription.CurrentState != EnumObjectState.DISCONNECTED)
            {
                m_daSubscription.Disconnect(new ExecutionOptions());
            }
            if (m_daSession.CurrentState != EnumObjectState.DISCONNECTED)
            {
                m_daSession.Disconnect(new ExecutionOptions());
            }

            m_daSubscription.RemoveDaItem(m_daItem1);
            m_daSubscription.RemoveDaItem(m_daItem2);
            m_daSubscription.RemoveDaItem(m_daItem3);
            m_daSubscription.RemoveDaItem(m_daItem4);
            m_daSubscription.RemoveDaItem(m_daItem5);
            m_daSubscription.RemoveDaItem(m_daItem6);

            m_daSession.RemoveDaSubscription(m_daSubscription);

            GetApplication().RemoveDaSession(m_daSession);

            GetApplication().Terminate();
            m_daSession      = null;
            m_daSubscription = null;
            m_daItem1        = null;
            m_daItem2        = null;
            m_daItem3        = null;
            m_daItem4        = null;
            m_daItem5        = null;
            m_daItem6        = null;
        }           //	end Terminate
        }           //	end InitializeDaObjects

        public string ReadItem()
        {
            string message = String.Empty;

            try
            {
                uint       readCount  = 1;
                MyDaItem[] m_itemList = new MyDaItem[readCount];
                m_itemList[0] = m_daItem1;

                ValueQT[] m_values  = null;
                int[]     m_results = null;

                if (ResultCode.SUCCEEDED(
                        m_daSubscription.Read(
                            0,
                            m_itemList,
                            out m_values,
                            out m_results,
                            null)))
                {
                    message += " \nRead item synchronously using subscription \n";

                    for (int i = 0; i < m_values.Length; i++)
                    {
                        if (ResultCode.SUCCEEDED(m_results[i]))
                        {
                            message += " " + m_itemList[i].Id + " - ";
                            message += m_values[i].ToString() + "\n\n";
                        }
                        else
                        {
                            message += "Read failed for item " + m_itemList[i].Id + "\n\n";
                        }                   //	end if...else
                    }                       //	end for
                }
                else
                {
                    message += " Subscription synchronous read failed!" + "\n\n";
                }                   //	end if...else
            }
            catch (Exception exc)
            {
                GetApplication().Trace(
                    EnumTraceLevel.ERR,
                    EnumTraceGroup.USER,
                    "OpcClient::ReadItem",
                    exc.ToString());
            }       //	end try...catch
            return(message);
        }           //	end ReadItem
示例#3
0
		} // end HandleValueChanged


		public static void HandlePerformStateTransition(
			ObjectSpaceElement sender,
			uint executionContext,
			int result)
		{
			if (ResultCode.SUCCEEDED(result))
			{
				MyDaItem item = sender as MyDaItem;
				System.Console.WriteLine(sender + " " + item.Id + " Performed state transition - " + executionContext);
			}
			else
			{
				System.Console.WriteLine(sender + "  Performed state transition failed! Result: " + result);
			}
		} // end HandlePerformStateTransition
示例#4
0
		//--
		#endregion

		#region Private Members
		//---------------------

		#region Private Attributes
		//------------------------


		//--
		#endregion

		//--
		#endregion

		#region Public Methods
		//---------------------

		//--
		#endregion

		#region Public Properties
		//-----------------------


		//--
		#endregion

		#region Handles

		public static void HandleStateChanged(ObjectSpaceElement sender, EnumObjectState state)
		{
			MyDaItem item = sender as MyDaItem;
			System.Console.WriteLine(item + " " + item.Id + " State Changed - " + state);
		} // end HandleStateChanged
        }         //end ReadItems

        public void WriteItems(bool sync)
        {
            DateTime writeDateTime = new DateTime();

            if (sync)
            {
                ValueQT[] inValues = new ValueQT[4];

                SByte[] v1 = new SByte[5];
                v1[0]       = 7; v1[1] = 8; v1[2] = 9; v1[3] = 10; v1[4] = 11;
                inValues[0] = new ValueQT(v1, EnumQuality.QUALITY_NOT_SET, writeDateTime);

                System.String v2 = "abcdefghijklmnopqrstuvxyzABCDEFGHIJKLMNOPQRSTUVXYZ";
                inValues[1] = new ValueQT(v2, EnumQuality.QUALITY_NOT_SET, writeDateTime);

                int v3 = v1[2];
                inValues[2] = new ValueQT(v3, EnumQuality.QUALITY_NOT_SET, writeDateTime);

                //Prepare bounded array (these are the values to be written)
                int[] zeroBoundArray = new int[] { 1, 2, 3 };
                int   lowBound       = 1;
                Array lowBoundArray  = Array.CreateInstance(typeof(object), new int[] { zeroBoundArray.Length }, new int[] { lowBound });
                zeroBoundArray.CopyTo(lowBoundArray, lowBound);
                //end prepare bounded array

                inValues[3] = new ValueQT(lowBoundArray, EnumQuality.QUALITY_NOT_SET, writeDateTime);

                int        result  = (int)EnumResultCode.E_FAIL;
                MyDaItem[] inItems = new MyDaItem[4];
                inItems.SetValue(m_daItem3, 0);
                inItems.SetValue(m_daItem4, 1);
                inItems.SetValue(m_daItem5, 2);
                inItems.SetValue(m_daItem6, 3);

                int[] inResults = new int[4];

                if (ResultCode.SUCCEEDED(result = m_daSubscription.Write(inItems, inValues, out inResults, new ExecutionOptions())))
                {
                    System.Console.WriteLine(" Subscription synchronous write succeeded");
                    for (int i = 0; i < inValues.Length; i++)
                    {
                        if (inResults[i] >= 0)
                        {
                            System.Console.WriteLine(inValues[i].ToString());
                            System.Console.WriteLine();
                        }
                        else
                        {
                            System.Console.WriteLine(" Item synchronous write failed!  " + inItems[i].Id + " [ResultID: " + inResults[i] + " ]");
                        }
                    }
                }
                else
                {
                    System.Console.WriteLine("Subscription synchronous write failed! Result: " + result);
                }
            }             // end if
            else
            {
                ValueQT[] inValues = new ValueQT[4];

                SByte[] v1 = new SByte[5];
                v1[0]       = 7; v1[1] = 8; v1[2] = 9; v1[3] = 10; v1[4] = 11;
                inValues[0] = new ValueQT(v1, EnumQuality.QUALITY_NOT_SET, writeDateTime);

                System.String v2 = "SOFTING";
                inValues[1] = new ValueQT(v2, EnumQuality.QUALITY_NOT_SET, writeDateTime);

                int v3 = 6;
                inValues[2] = new ValueQT(v3, EnumQuality.UNCERTAIN, writeDateTime);

                //Prepare bounded array (these are the values to be written)
                int[] zeroBoundArray = new int[] { 4, 5, 6 };
                int   lowBound       = 1;
                Array lowBoundArray  = Array.CreateInstance(typeof(object), new int[] { zeroBoundArray.Length }, new int[] { lowBound });;
                zeroBoundArray.CopyTo(lowBoundArray, lowBound);
                //end prepare bounded array

                inValues[3] = new ValueQT(lowBoundArray, EnumQuality.QUALITY_NOT_SET, writeDateTime);

                MyDaItem[] inItems = new MyDaItem[4];
                inItems[0] = m_daItem3;
                inItems[1] = m_daItem4;
                inItems[2] = m_daItem5;
                inItems[3] = m_daItem6;

                int[] inResults = new int[4];

                m_daSubscription.Write(inItems, inValues, out inResults, m_executionOptions);
                m_executionOptions.ExecutionContext++;
            }     // end else
        }         //end WriteItems
        }           //	end Terminate

        public int InitializeDaObjects()
        {
            int connectResult = (int)EnumResultCode.E_FAIL;

            m_executionOptions = new ExecutionOptions();
            m_executionOptions.ExecutionType    = EnumExecutionType.ASYNCHRONOUS;
            m_executionOptions.ExecutionContext = 0;

            m_itemList = new MyDaItem[6];
            m_itemIds  = new string[] { "maths.sin", "time.local.second", "increment.UI1 array", "increment.BSTR", "secure.I2", "IOP.static.I1 array" };

            try
            {
                //	TODO add your server URL here
                //	this is the server url for Softing OPCToolbox Demo Server
                //	first choice is COM-DA
                string url = "opcda:///Softing.OPCToolboxDemo_ServerDA.1/{2E565242-B238-11D3-842D-0008C779D775}";

                //uncomment if you need an XML-DA access
                //	string url = "http://localhost:8079/OPC/DA";

                m_daSession = new MyDaSession(url);

                if (!m_daSession.Valid)
                {
                    return(connectResult);
                }

                m_daSubscription = new MyDaSubscription(1000, m_daSession);

                if (!m_daSubscription.Valid)
                {
                    return(connectResult);
                }

                m_daItem1 = new MyDaItem("maths.sin", m_daSubscription);

                if (!m_daItem1.Valid)
                {
                    return(connectResult);
                }
                m_itemList[0] = m_daItem1;

                m_daItem2 = new MyDaItem("time.local.second", m_daSubscription);

                if (!m_daItem2.Valid)
                {
                    return(connectResult);
                }
                m_itemList[1] = m_daItem2;

                m_daItem3 = new MyDaItem("increment.UI1 array", m_daSubscription);
                if (!m_daItem3.Valid)
                {
                    return(connectResult);
                }
                m_itemList[2] = m_daItem3;

                m_daItem4 = new MyDaItem("increment.BSTR", m_daSubscription);
                if (!m_daItem4.Valid)
                {
                    return(connectResult);
                }

                m_itemList[3] = m_daItem4;

                m_daItem5 = new MyDaItem("secure.I2", m_daSubscription);
                if (!m_daItem5.Valid)
                {
                    return(connectResult);
                }

                m_itemList[4] = m_daItem5;

                m_daItem6 = new MyDaItem("IOP.static.I1 array", m_daSubscription);
                if (!m_daItem6.Valid)
                {
                    return(connectResult);
                }

                m_itemList[5] = m_daItem6;



                connectResult = m_daSession.Connect(true, false, m_executionOptions);
            }
            catch (Exception exc)
            {
                GetApplication().Trace(
                    EnumTraceLevel.ERR,
                    EnumTraceGroup.USER,
                    "OpcClient::InitializaDaObjects",
                    exc.ToString());
            }               //	end try...catch

            return(connectResult);
        }           //	end InitializeDaObjects