Ejemplo n.º 1
0
        public void ConnectedGroup_DataChange(int TransactionID, int NumItems, ref System.Array ClientHandles, ref System.Array ItemValues, ref System.Array Qualities, ref System.Array TimeStamps)
        {
            if (!ConnectGroupDataChangeKapat)
            {
                //  We don't have error handling here since this is an event called from the OPC interface
                try
                {
                    short i;
                    for (i = 1; (i <= NumItems); i++)
                    {
                        //  Use the 'Clienthandles' array returned by the server to pull out the
                        //  index number of the control to update and load the value.
                        if (IsArray(ItemValues.GetValue(i).GetType()))
                        {
                            Array  ItsAnArray;
                            int    x;
                            string Suffix;
                            ItsAnArray = (System.Array)ItemValues.GetValue(i);
                            //  Store the size of array for use by sync write
                            OPCItemIsArray[(int)ClientHandles.GetValue(i)]       = (ItsAnArray.GetUpperBound(0) + 1);
                            OPCItem[(int)ClientHandles.GetValue(i)].OPCItemValue = "";
                            for (x = ItsAnArray.GetLowerBound(0); (x <= ItsAnArray.GetUpperBound(0)); x++)
                            {
                                if ((x == ItsAnArray.GetUpperBound(0)))
                                {
                                    Suffix = "";
                                }
                                else
                                {
                                    Suffix = ", ";
                                }
                                OPCItem[(int)ClientHandles.GetValue(i)].OPCItemValue = (OPCItem[(int)ClientHandles.GetValue(i)].OPCItemValue + (ItsAnArray.GetValue(x) + Suffix));
                            }
                        }
                        else
                        {
                            OPCItem[(int)ClientHandles.GetValue(i)].OPCItemValue = ItemValues.GetValue(i).ToString();
                        }


                        if (((OPCAutomation.OPCQuality)Qualities.GetValue(i)) == OPCAutomation.OPCQuality.OPCQualityGood)
                        {
                            OPCItem[(int)ClientHandles.GetValue(i)].OPCItemQuality = "Good";
                        }
                        else if ((((OPCAutomation.OPCQuality)Qualities.GetValue(i)) == OPCAutomation.OPCQuality.OPCQualityUncertain))
                        {
                            OPCItem[(int)ClientHandles.GetValue(i)].OPCItemQuality = "Uncertain";
                        }
                        else
                        {
                            OPCItem[(int)ClientHandles.GetValue(i)].OPCItemQuality = "Bad";
                        }
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(("OPC DataChange failed with exception: " + ex.Message), "SimpleOPCInterface Exception", MessageBoxButtons.OK);
                }
            }
        }
Ejemplo n.º 2
0
        public string  GetOPCItemSyncRead(string groupname, string adres)
        {
            SetConnectedGroup(groupname);

            if ((ConnectedGroup != null))
            {
                List <OpcItems> items = OPCItem.FindAll(p => p != null && p != null && p.OPCItemName == adres);

                try
                {
                    //        ' Read only 1 item
                    short ItemCount = 1;

                    //        ' Provide storage the arrays returned by the 'SyncRead' method
                    int[]        SyncItemServerHandles = new int[2];
                    System.Array SyncItemValues;
                    System.Array SyncItemServerErrors;

                    //        ' Get the Servers handle for the desired item.  The server handles were
                    //        ' returned in add item subroutine.
                    SyncItemServerHandles[1] = (int)ItemServerHandles.GetValue(items[0].AdresNo);

                    //        ' Invoke the SyncRead operation.  Remember this call will wait until
                    //        ' completion. The source flag in this case, 'OPCDevice' , is set to
                    //        ' read from device which may take some time.
                    object       timestamp             = null;
                    object       qualities             = null;
                    System.Array syncitemserverhandles = SyncItemServerHandles.ToArray <int>();
                    ConnectedGroup.SyncRead((short)OPCAutomation.OPCDataSource.OPCDevice, ItemCount, ref syncitemserverhandles, out SyncItemValues, out SyncItemServerErrors, out qualities, out timestamp);

                    //        ' Save off the value returned after checking for error
                    if (((int)SyncItemServerErrors.GetValue(1) == 0))
                    {
                        if (IsArray(SyncItemValues.GetValue(1).GetType()))
                        {
                            Array  ItsAnArray;
                            int    x;
                            string Suffix;
                            ItsAnArray = (System.Array)SyncItemValues.GetValue(1);
                            //((TextBox)OPCItemValue.GetValue(index)).Text = "";
                            items[0].OPCItemValue = "";
                            for (x = ItsAnArray.GetLowerBound(0); (x <= ItsAnArray.GetUpperBound(0)); x++)
                            {
                                if ((x == ItsAnArray.GetUpperBound(0)))
                                {
                                    Suffix = "";
                                }
                                else
                                {
                                    Suffix = ", ";
                                }
                                items[0].OPCItemValue = (items[0].OPCItemValue + (ItsAnArray.GetValue(x) + Suffix));
                                return(items[0].OPCItemValue);
                            }
                        }
                        else
                        {
                            items[0].OPCItemValue = SyncItemValues.GetValue(1).ToString();
                            return(items[0].OPCItemValue);
                        }
                    }
                    else
                    {
                        MessageBox.Show(("SyncItemServerError: " + SyncItemServerErrors.GetValue(1)));
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("OPC server read item failed with exception: " + ex.Message, "SimpleOPCInterface Exception", MessageBoxButtons.OK);
                }
            }
            return(string.Empty);
        }