Beispiel #1
0
        private void BGW_OPCUA_DoWork(object sender_DW, DoWorkEventArgs e_DW)
        {
            try
            {
                int count = Decimal.ToInt32(Config.Sets.Primary_SQL_NumberOfRec);
                myStructList = new List <string[]>();
                List <String> values        = new List <string>();
                List <String> nodeIdStrings = new List <string>();

                if (BGW_OPCUA.CancellationPending)
                {
                    e_DW.Cancel = true;
                    return;
                }

                if (startRead && !e_DW.Cancel)
                {
                    try
                    {
                        myStructList = myClientHelperAPI.ReadStructUdt("ns=3;s=\"" + Config.Sets.Primary_S7_DBName + "\".\"" + textBoxS7RecArrayName.Text + "\"");
                    }
                    catch (Exception ex)
                    {
                        if (BGW_OPCUA.IsBusy)
                        {
                            BGW_OPCUA.CancelAsync();
                        }
                        MessageBox.Show(ex.Message, "Error");
                    }

                    if (count > myStructList.Count / 5)
                    {
                        count = myStructList.Count / 5;
                        numericS7RecordsCount.Invoke(new Action(() => numericS7RecordsCount.Maximum = count));
                    }

                    string[] row = new string[3 * count];

                    for (int j = 0; j < count; j++)
                    {
                        row[3 * j]     = myStructList[j * 5 + 3].ElementAt(1);
                        row[3 * j + 1] = myStructList[j * 5 + 4].ElementAt(1);
                        row[3 * j + 2] = myStructList[j * 5 + 2].ElementAt(1);
                    }

                    InsertRow(count, row);

                    // Обнуление счётчика
                    values.Clear();
                    values.Add("0");
                    nodeIdStrings.Clear();
                    nodeIdStrings.Add("ns=3;s=\"" + Config.Sets.Primary_S7_DBName + "\".\"" + textBoxS7RecResetCountName.Text + "\"");
                    try
                    {
                        myClientHelperAPI.WriteValues(values, nodeIdStrings);
                        MessageBox.Show("Test completed successfully!", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                    catch (Exception ex)
                    {
                        if (BGW_OPCUA.IsBusy)
                        {
                            BGW_OPCUA.CancelAsync();
                        }
                        MessageBox.Show(ex.Message, "Error");
                    }
                }
            }
            catch (Exception ex)
            {
                if (BGW_OPCUA.IsBusy)
                {
                    BGW_OPCUA.CancelAsync();
                }
                MessageBox.Show(ex.Message, "Error");
            }
        }