示例#1
0
        /// <summary>
        /// Reads the item values and stores the results in the request object.
        /// </summary>
        /// <param name="requests">The requests.</param>
        public void ReadItemValues(List <ReadRequest> requests)
        {
            ComDaGroup group = new ComDaGroup(this, false);

            try
            {
                int           count1          = 0;
                GroupItem[]   items           = new GroupItem[requests.Count];
                ReadRequest[] addItemRequests = new ReadRequest[requests.Count];

                // create the items in the temporary group.
                for (int ii = 0; ii < requests.Count; ii++)
                {
                    ReadRequest request = requests[ii];

                    if (request == null)
                    {
                        continue;
                    }

                    if (!request.ValueRequired)
                    {
                        continue;
                    }

                    // add the item.
                    items[count1]           = group.CreateItem(request.ItemId, 0, 0, true);
                    addItemRequests[count1] = request;
                    count1++;
                }

                // create the items on the server.
                group.ApplyChanges();

                // build the list of values to write.
                int           count2        = 0;
                int[]         serverHandles = new int[count1];
                ReadRequest[] readRequests  = new ReadRequest[count1];

                for (int ii = 0; ii < count1; ii++)
                {
                    // check for error on create.
                    GroupItem   item    = items[ii];
                    ReadRequest request = addItemRequests[ii];

                    if (item.ErrorId < 0)
                    {
                        request.Value.Error = item.ErrorId;
                        continue;
                    }

                    serverHandles[count2] = item.ServerHandle;
                    readRequests[count2]  = request;
                    count2++;
                }

                // write values to the server.
                DaValue[] values = group.SyncRead(serverHandles, count2);

                // read the values.
                for (int ii = 0; ii < count2; ii++)
                {
                    readRequests[ii].Value = values[ii];
                }
            }
            finally
            {
                // delete the group and items.
                group.Delete();
            }
        }
示例#2
0
        /// <summary>
        /// Writes the item values to servers.
        /// </summary>
        /// <param name="requests">The requests.</param>
        private void WriteItemValues(List <WriteRequest> requests)
        {
            ComDaGroup group = new ComDaGroup(this, false);

            try
            {
                int            count1          = 0;
                GroupItem[]    items           = new GroupItem[requests.Count];
                WriteRequest[] addItemRequests = new WriteRequest[requests.Count];

                // create the items in the temporary group.
                for (int ii = 0; ii < requests.Count; ii++)
                {
                    WriteRequest request = requests[ii];

                    if (request == null)
                    {
                        continue;
                    }

                    // status code writes not supported.
                    if (request.Value.StatusCode != StatusCodes.Good)
                    {
                        request.Error = ResultIds.E_NOTSUPPORTED;
                        continue;
                    }

                    // timestamp writes not supported.
                    if (request.Value.SourceTimestamp != DateTime.MinValue)
                    {
                        request.Error = ResultIds.E_NOTSUPPORTED;
                        continue;
                    }

                    // add the item.
                    items[count1]           = group.CreateItem(request.ItemId, 0, 0, true);
                    addItemRequests[count1] = request;
                    count1++;
                }

                // create the items on the server.
                group.ApplyChanges();

                // build the list of values to write.
                int            count2        = 0;
                int[]          serverHandles = new int[count1];
                object[]       values        = new object[count1];
                WriteRequest[] writeRequests = new WriteRequest[count1];

                for (int ii = 0; ii < count1; ii++)
                {
                    // check for error on create.
                    GroupItem    item    = items[ii];
                    WriteRequest request = addItemRequests[ii];

                    if (item.ErrorId < 0)
                    {
                        request.Error = item.ErrorId;
                        continue;
                    }

                    serverHandles[count2] = item.ServerHandle;
                    values[count2]        = ComUtils.GetVARIANT(request.Value.Value);
                    writeRequests[count2] = request;
                    count2++;
                }

                // write values to the server.
                int[] errors = group.SyncWrite(serverHandles, values, count2);

                // read the errors.
                for (int ii = 0; ii < count2; ii++)
                {
                    writeRequests[ii].Error = errors[ii];
                }
            }
            finally
            {
                // delete the group and items.
                group.Delete();
            }
        }