예제 #1
0
        public void CreateEV3File(String fullname, byte[] content)
        {
            int chunksize   = 900;
            int pos         = 0;
            int transfernow = Math.Min(content.Length - pos, chunksize - fullname.Length);

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

            b.Append32(content.Length);
            b.AppendZeroTerminated(fullname);
            b.AppendBytes(content, pos, transfernow);

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

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

            pos += transfernow;

            int handle = response[1] & 0xff;

            // transfer bytes in small chunks
            while (pos < content.Length)
            {
                transfernow = Math.Min(content.Length - pos, chunksize);
                b.Clear();
                b.Append8(handle);
                b.AppendBytes(content, pos, transfernow);
                response = SystemCommand(CONTINUE_DOWNLOAD, b);

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

                pos += transfernow;
            }
        }
예제 #2
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);
        }