Пример #1
0
 /// <summary>
 /// 写变量
 /// </summary>
 /// <param name="tag"></param>
 /// <returns></returns>
 public bool Write(TagItem tag)
 {
     uAClient.WriteValues(new List <string>()
     {
         tag.ValueToWrite?.ToString()
     }, new List <string>()
     {
         tag.GetNodeId()
     });
     return(true);
 }
Пример #2
0
 /// <summary>
 /// 写入多个变量
 /// </summary>
 /// <param name="values">写入的值</param>
 /// <param name="nodeid">写入的地址,格式为:块名.变量名</param>
 public void PLC_WriteValues(List <string> values, List <string> nodeid)
 {
     if (m_Connected)
     {
         int i = 0;
         foreach (string str in nodeid)
         {
             nodeid[i] = _GetFormat(str);
             i++;
         }
         m_Server.WriteValues(values, nodeid);
     }
 }
Пример #3
0
        /// <summary>
        /// Helper function to writing a value to a variable.
        /// The function
        /// - reads the data type of the variable
        /// - converts the passed string to the data type
        /// - writes the value to the variable
        /// </summary>
        private void writeNewValue(NodeId nodeToWrite, string valueToWrite)
        {
            try
            {
                List <string> nodesToWrite = new List <string>();
                List <string> values       = new List <string>();

                nodesToWrite.Add(nodeToWrite.ToString());
                values.Add(valueToWrite);

                m_Server.WriteValues(values, nodesToWrite);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Writing new value failed:\n\n" + ex.Message);
            }
        }
Пример #4
0
        /// <summary>
        /// Writes the values.
        /// </summary>
        private void WriteValues()
        {
            try
            {
                // Prepare call to ClientAPI.
                List <string> nodesToWrite = new List <string>(this.listView.Items.Count);
                List <string> writeValues  = new List <string>(this.listView.Items.Count);

                int i = 0;
                foreach (ListViewItem item in this.listView.Items)
                {
                    // Values to write.
                    String sValue = (String)item.SubItems[0].Text;

                    // Leave current value if write value is empty.
                    if (sValue.Length == 0)
                    {
                        i++;
                        continue;
                    }
                    writeValues.Add(sValue);

                    // NodeIds.
                    nodesToWrite.Add(item.SubItems[1].Text);
                    i++;
                }

                // Call to ClientAPI.
                m_Server.WriteValues(writeValues, nodesToWrite);

                // Update status label.

                toolStripLabel1.Text = "Writing values succeeded.";
            }
            catch (Exception e)
            {
                // Update status label.
                toolStripLabel1.Text = "An exception occured while writing values: "
                                       + e.Message;
            }
        }
Пример #5
0
        private void WriteValue(string nodeId, string value)
        {
            if (value == "")
            {
                MessageBox.Show("Please enter valid value!");
                return;
            }

            List <String> values        = new List <string>();
            List <String> nodeIdStrings = new List <string>();

            values.Add(value);
            nodeIdStrings.Add(nodeId);
            try
            {
                myClientHelperAPI.WriteValues(values, nodeIdStrings);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error");
            }
        }
Пример #6
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");
            }
        }