/*
         * public static string ReadStruct(int varHandle, List<string> types, int[] size, ref TcAdsClient tcClient, out List<List<object>> output)
         * {
         *  output = new List<List<object>>();
         *  try
         *  {
         *      string message = "";
         *      int streamLength = 0;
         *      for (int i = 0; i < size.Length; i++) streamLength += StreamLength(types[i]) * size[i];
         *
         *      AdsStream dataStream = new AdsStream(streamLength);
         *      AdsBinaryReader reader = new AdsBinaryReader(dataStream);
         *      for (int i = 0; i < size.Length; i++)
         *      {
         *          List<object> o = new List<object>();
         *          for (int j = 0; j < size[i]; j++)
         *          {
         *              object obj = new object();
         *              if (!ReadObject(reader, types[i], out obj))
         *                  message = String.Format("Error while reading " + types[i] + " at struct position (i, j): (" + i + ", " + j + ")");
         *              o.Add(obj);
         *          }
         *          output.Add(o);
         *      }
         *
         *      return message;
         *  }
         *  catch (Exception e)
         *  {
         *      return e.ToString();
         *  }
         *
         * }
         */


        public void WriteStruct(string structName, string[] types, object[][] values)
        {
            int dataStreamLength = 0;

            for (int i = 0; i < values.Length; i++)
            {
                dataStreamLength += StreamLength(types[i]) * values[i].Length;
            }

            using (AdsStream dataStream = new AdsStream(dataStreamLength))
                using (AdsBinaryWriter writer = new AdsBinaryWriter(dataStream))
                {
                    int varHandle = TcClient.CreateVariableHandle(structName);
                    dataStream.Position = 0;

                    for (int i = 0; i < values.Length; i++)
                    {
                        foreach (object obj in values[i])
                        {
                            WriteObjectToWriter(writer, types[i], obj);
                            if (types[i] == "string")
                            {
                                dataStream.Position += 81 - obj.ToString().Length;
                            }
                        }
                    }

                    TcClient.Write(varHandle, dataStream);
                    writer.Flush();
                }
        }
        /// <summary>
        /// Write item array to TwinCAT Variables
        /// </summary>
        /// <param name="name"></param>
        /// <param name="type"></param>
        /// <param name="values"></param>
        public void WriteArray(string name, string type, object[] values)
        {
            int arrLength    = values.Length;
            int streamLength = StreamLength(type);

            using (AdsStream dataStream = new AdsStream(arrLength * streamLength))
                using (AdsBinaryWriter writer = new AdsBinaryWriter(dataStream))
                {
                    int varHandle = TcClient.CreateVariableHandle(name);
                    dataStream.Position = 0;

                    foreach (object val in values)
                    {
                        WriteObjectToWriter(writer, type, val);
                    }
                    TcClient.Write(varHandle, dataStream);
                    writer.Flush();
                }
        }