Esempio n. 1
0
        /// <summary>
        /// Reads all the bytes needed to fill a struct in C#, starting from a certain address, and return an object that can be casted to the struct.
        /// </summary>
        /// <param name="structType">Type of the struct to be readed (es.: TypeOf(MyStruct)).</param>
        /// <param name="db">Address of the DB.</param>
        /// <param name="startByteAdr">Start byte address. If you want to read DB1.DBW200, this is 200.</param>
        /// <returns>Returns a struct that must be cast.</returns>
        public object ReadStruct(Type structType, int db, int startByteAdr = 0)
        {
            int numBytes = Struct.GetStructSize(structType);
            // now read the package
            var resultBytes = ReadBytes(DataType.DataBlock, db, startByteAdr, numBytes);

            // and decode it
            return(Struct.FromBytes(structType, resultBytes));
        }
Esempio n. 2
0
        public object ReadStruct(DataBlock structType, int db, int startByteAdr = 0)
        {
            int numBytes = Struct.GetStructSize(structType);

            // now read the package
            byte[] db1Buffer = new byte[512];

            result = client.DBRead(db, startByteAdr, numBytes, db1Buffer);
            if (result != 0)
            {
                Console.WriteLine("Error: " + client.ErrorText(result));
            }

            // and decode it
            return(Struct.FromBytes(structType, db1Buffer, this));
        }