Esempio n. 1
0
        public byte[] ReadEV3File(String fullname, ByteCodeBuffer pingercommand = null)
        {
            long nextping  = DateTime.Now.Ticks + 500;
            int  chunksize = 900;

            // start the transfer
            BinaryBuffer b = new BinaryBuffer();

            b.Append16(0);                     // transfer no content right now
            b.AppendZeroTerminated(fullname);

            byte[] response = SystemCommand(BEGIN_UPLOAD, b);

            if (response == null)
            {
                throw new Exception("No response to BEGIN_UPLOAD");
            }
            if (response.Length < 6)
            {
                throw new Exception("Response too short for BEGIN_UPLOAD");
            }
            if (response[0] != SUCCESS && response[0] != END_OF_FILE)
            {
                throw new Exception("Unexpected status at BEGIN_UPLOAD: " + response[0]);
            }

            int len    = ((int)response[1]) + (((int)response[2]) << 8) + (((int)response[3]) << 16) + (((int)response[4]) << 24);
            int handle = response[5] & 0xff;

            //    Console.WriteLine("Start uploading file of size: " + len + ". handle=" + handle);

            byte[] buffer = new byte[len];
            int    pos    = 0;

            // transfer bytes in small chunks
            while (pos < len)
            {
                int transfernow = Math.Min(len - pos, chunksize);
                b.Clear();
                b.Append8(handle);
                b.Append16(transfernow);

                response = SystemCommand(CONTINUE_UPLOAD, b);

                if (response == null)
                {
                    throw new Exception("No response to CONTINUE_UPLOAD");
                }
                if (response.Length < 2 + transfernow)
                {
                    throw new Exception("Response too short for CONTINUE_UPLOAD");
                }
                if (response[0] != SUCCESS && response[0] != END_OF_FILE)
                {
                    throw new Exception("Unexpected status at CONTINUE_UPLOAD: " + response[0]);
                }

                for (int i = 0; i < transfernow; i++)
                {
                    buffer[pos + i] = response[2 + i];
                }
                pos += transfernow;

                // check if it is necessary to send intermediary pings to the watchdog program
                if (pingercommand != null && DateTime.Now.Ticks > nextping)
                {
                    DirectCommand(pingercommand, 4, 0);
                    nextping = DateTime.Now.Ticks + 500;
                }
            }

            return(buffer);
        }