Beispiel #1
0
        /// <summary>
        /// Writes the values.
        /// </summary>
        private void WriteValues()
        {
            try
            {
                // Prepare call to ClientAPI.
                List <WriteValue> nodesToWrite = new List <WriteValue>();
                List <StatusCode> results      = null;
                int        index        = 0;
                List <int> lstUsedIndex = new List <int>();

                // fill write values
                foreach (ListViewItem item in this.listView.Items)
                {
                    WriteValueInfo writeValInfo = item.Tag as WriteValueInfo;

                    if (writeValInfo.AttributesRead == true && writeValInfo.Error == false)
                    {
                        try
                        {
                            // convert string to type
                            if (writeValInfo.ValueRank == ValueRanks.Scalar)
                            {
                                DataValue dataValue = new DataValue();
                                dataValue.Value = TypeUtils.Cast(this.listView.Items[index].SubItems[0].Text, writeValInfo.DataType);

                                nodesToWrite.Add(new WriteValue()
                                {
                                    NodeId      = writeValInfo.NodeId,
                                    Value       = dataValue,
                                    AttributeId = Attributes.Value
                                });

                                lstUsedIndex.Add(index);
                            }
                            else if (writeValInfo.ValueRank == ValueRanks.OneDimension)
                            {
                                DataValue dataValue = new DataValue();
                                dataValue.Value = TypeUtils.Cast(this.listView.Items[index].SubItems[0].Text, writeValInfo.DataType);

                                nodesToWrite.Add(new WriteValue()
                                {
                                    NodeId      = writeValInfo.NodeId,
                                    Value       = dataValue,
                                    AttributeId = Attributes.Value
                                });

                                lstUsedIndex.Add(index);
                            }
                        }
                        catch (Exception exception)
                        {
                            this.listView.Items[index].SubItems[0].Text = exception.Message;
                        }
                    }
                    index++;
                }

                // Call to ClientAPI.
                results = m_Session.Write(
                    nodesToWrite,
                    null);

                // Update status label.
                lblStatus.Text = "Writing values succeeded.";
            }
            catch (Exception e)
            {
                // Update status label.
                lblStatus.Text = "An exception occured while writing values: " + e.Message;
            }
        }
Beispiel #2
0
        /// <summary>
        /// Loads the configuration for the system.
        /// </summary>
        private void Load()
        {
            foreach (string resourceName in Assembly.GetExecutingAssembly().GetManifestResourceNames())
            {
                if (resourceName.EndsWith(".SystemConfiguration.xml"))
                {
                    using (Stream istrm = Assembly.GetExecutingAssembly().GetManifestResourceStream(resourceName))
                    {
                        XmlSerializer serializer = new XmlSerializer(typeof(Configuration));
                        m_configuration = (Configuration)serializer.Deserialize(istrm);
                    }
                }
            }

            if (m_configuration.Controllers != null)
            {
                for (int ii = 0; ii < m_configuration.Controllers.Length; ii++)
                {
                    ControllerConfiguration controller = m_configuration.Controllers[ii];

                    int blockAddress = m_position;
                    int offset       = m_position - blockAddress;

                    BlockConfiguration data = new BlockConfiguration()
                    {
                        Address    = blockAddress,
                        Name       = controller.Name,
                        Type       = controller.Type,
                        Properties = new List <BlockProperty>()
                    };

                    if (controller.Properties != null)
                    {
                        for (int jj = 0; jj < controller.Properties.Length; jj++)
                        {
                            ControllerProperty property   = controller.Properties[jj];
                            NodeId             dataTypeId = NodeId.Parse(property.DataType);
                            string             value      = property.Value;
                            Range range = null;

                            if (!String.IsNullOrEmpty(property.Range))
                            {
                                try
                                {
                                    NumericRange nr = NumericRange.Parse(property.Range);
                                    range = new Range()
                                    {
                                        High = nr.End, Low = nr.Begin
                                    };
                                }
                                catch (Exception)
                                {
                                    range = null;
                                }
                            }

                            data.Properties.Add(new BlockProperty()
                            {
                                Offset    = offset,
                                Name      = controller.Properties[jj].Name,
                                DataType  = dataTypeId,
                                Writeable = controller.Properties[jj].Writeable,
                                Range     = range
                            });

                            switch ((uint)dataTypeId.Identifier)
                            {
                            case DataTypes.Int32:
                            {
                                Write(blockAddress, offset, (int)TypeUtils.Cast(value, BuiltInType.Int32));
                                offset += 4;
                                break;
                            }

                            case DataTypes.Double:
                            {
                                Write(blockAddress, offset, (double)TypeUtils.Cast(value, BuiltInType.Double));
                                offset += 4;
                                break;
                            }
                            }
                        }
                    }

                    m_position            += offset;
                    m_blocks[blockAddress] = data;
                }
            }
        }