titanium-as / TitaniumAS.Opc.Client

Open source .NET client library for OPC DA
MIT License
193 stars 94 forks source link

OPC variable write error. #44

Open AnsMumtaz-IMS opened 4 years ago

AnsMumtaz-IMS commented 4 years ago

This my function which i am using to write new values for OPC DA server variable. Actually, variable is MeterCount which shows the length in meters and i am making a button to reset its value(means 0). I want to write the new value from WPF application(VS 2015) but its not working... can anyone please help me with this. Reading the values is not problem its working perfectly but writing is not working.

   private void MeterReset_Click(object sender, RoutedEventArgs e)
    {
        try
        {
            object[] opcvalues = { true , "0.00 m" };   
            HRESULT[] rs = readGroup.Write(readGroup.Items, opcvalues);
            readGroup.ValuesChanged += OnGroupValuesChanged;
            readGroup.UpdateRate = TimeSpan.FromMilliseconds(100);
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }
AnsMumtaz-IMS commented 4 years ago

private void opceditor_Click(object sender, RoutedEventArgs e) { string opcIds = ""; string opcHierarchy = "";

        foreach (OpcItemHelper item in videoSettings.selectedDevice1.opcItemList)
        {
            Dispatcher.BeginInvoke((Action)delegate ()
            {
                // lbOpcTest.Content = "ItemId: " + value.Item.ItemId + " Value: " + value.Value + " Quality: " + value.Quality + " Timestamp: " + value.Timestamp;
                try
                {
                    if (item.itemId.ToString().Contains("MeterCount"))
                    {
                        opcIds = item.itemId;                                               // + ",";
                        //MessageBox.Show(opcIds);
                        object[] opcvalues = { 0 };
                        OpcDaItem itemMeterCount = readGroup.Items.FirstOrDefault(i => i.ItemId == opcIds);
                        OpcDaItem[] itemsVal = { itemMeterCount };
                        HRESULT[] rs = readGroup.Write(itemsVal, opcvalues);
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Error(Reset): " + ex.Message);
                }
            });
        }
        readGroup.ValuesChanged += OnGroupValuesChanged;
        readGroup.UpdateRate = TimeSpan.FromMilliseconds(100);
    }