Ejemplo n.º 1
0
        private void Worker_download_DoWork(object sender, DoWorkEventArgs e)
        {
            WorkerData workerData = e.Argument as WorkerData; // We gather the info from our class
            uint       offset     = 0;
            uint       total      = workerData.total;
            int        uiTransmitted;
            string     file = workerData.filename;
            int        progressnb;

            byte[]    readBuffer = new byte[2048 * 8];
            ErrorCode eReturn;

            // Create a FileStream object to write a stream to a file
            FileStream   fileStream = System.IO.File.Create(filename, readBuffer.Length);
            BinaryWriter binWriter  = new BinaryWriter(fileStream);

            while ((offset <= (total - readBuffer.Length)) && !e.Cancel)
            {
                if (Worker_download.CancellationPending)
                {
                    e.Cancel = true;
                }

                eReturn = mEpReader.Read(readBuffer, 1000, out uiTransmitted);
                if (eReturn == ErrorCode.None)
                {
                    binWriter.Write(readBuffer);
                    offset    += (uint)readBuffer.Length;
                    progressnb = (int)((float)((float)offset / (float)total) * 100.0F);
                    Worker_download.ReportProgress(progressnb);
                }

                else
                {
                    binWriter.Close();
                    fileStream.Close();
                    mEpReader.Flush();
                    return;
                }
            }

            if ((offset >= (total - readBuffer.Length)) && !Worker_download.CancellationPending)
            {
                mEpReader.Flush();
                //we create a buffer sized to the remaining bytes. This is to avoid writing a file too big.
                byte[] endBuffer = new byte[total - offset];
                mEpReader.Read(endBuffer, 1000, out uiTransmitted);
                binWriter.Write(endBuffer);
                return;
            }
            mEpReader.Flush();
            binWriter.Flush();
            binWriter.Close();
            fileStream.Flush();
            fileStream.Unlock(0, fileStream.Length);
            fileStream.Close();
        }
Ejemplo n.º 2
0
        private void b_download_Click(object sender, EventArgs e)
        {
            if (b_download.Text == "Cancel")
            {
                Worker_download.CancelAsync();
                if (Worker_download.CancellationPending)
                {
                    t_log.AppendText("Cancelling..." + "\r\n");
                }
                return;
            }

            if (saveFileDialog1.ShowDialog(this) == DialogResult.OK)
            {
                filename = saveFileDialog1.FileName;
                saveFileDialog1.Dispose();
                t_log.AppendText("## Saving to " + filename + "\r\n");
                byte[] dlinfo = new byte[0x000010a0]; //this array has the size of the info that the mini2440 sends us.
                b_upload.Enabled   = false;
                b_download.Enabled = false;

                int       uiTransmitted;
                uint      total;
                ErrorCode eReturn = mEpReader.Read(dlinfo, 1000, out uiTransmitted);
                if (eReturn == ErrorCode.None)
                {
                    // We gather the informations from the info buffer and display it.
                    total = BitConverter.ToUInt32(dlinfo, 0x30);
                    t_log.AppendText("Nand Flash Information: " + "\r\n"
                                     + "## type      : " + "0x" + BitConverter.ToInt32(dlinfo, 0x00).ToString("x08") + "\r\n"
                                     + "## flags     : " + "0x" + BitConverter.ToInt32(dlinfo, 0x04).ToString("x08") + "\r\n"
                                     + "## size      : " + ((BitConverter.ToInt32(dlinfo, 0x08) / 1024) / 1024).ToString() + "MB"
                                     + " - " + (BitConverter.ToInt32(dlinfo, 0x08) / 1024).ToString() + "KB" + "\r\n"
                                     + "## erasesize : " + (BitConverter.ToInt32(dlinfo, 0x0c) / 1024).ToString() + "KB" + "\r\n"
                                     + "## oobblock  : " + BitConverter.ToInt32(dlinfo, 0x10).ToString() + "\r\n"
                                     + "## oobsize   : " + BitConverter.ToInt32(dlinfo, 0x14).ToString() + "\r\n"
                                     + "## ecctype   : " + "0x" + BitConverter.ToInt32(dlinfo, 0x18).ToString("x") + "\r\n"
                                     + "## eccsize   : " + BitConverter.ToInt32(dlinfo, 0x1c).ToString() + "\r\n");

                    t_log.AppendText("Backup Information: " + "\r\n"
                                     + "## Start Addr       : " + "0x" + BitConverter.ToInt32(dlinfo, 0x20).ToString("x08") + "\r\n"
                                     + "## End Addr         : " + "0x" + BitConverter.ToInt32(dlinfo, 0x24).ToString("x08") + "\r\n"
                                     + "## bBackupOOB       : " + BitConverter.ToInt32(dlinfo, 0x28).ToString() + "\r\n"
                                     + "## bCheckBad        : " + BitConverter.ToInt32(dlinfo, 0x2c).ToString() + "\r\n"
                                     + "## dwBackupTotalLen : " + "0x" + total.ToString("x") + " (" + ((total / 1024) / 1024) + "MB)" + "\r\n"
                                     + "## dwReservedBlks   : " + BitConverter.ToInt32(dlinfo, 0x34).ToString() + "\r\n"
                                     + "## dwEPInPktSize    : " + BitConverter.ToInt32(dlinfo, 0x38).ToString() + "\r\n");

                    t_log.AppendText("## Got the info we need, now downloading data..." + "\r\n");

                    b_download.Enabled = true;
                    b_download.Text    = "Cancel";
                }

                else
                {
                    t_log.AppendText("## No info to read! " + eReturn + "\r\n");
                    b_upload.Enabled   = true;
                    b_download.Enabled = true;
                    return;
                }

                // We load the data in our class.
                WorkerData workerData = new WorkerData
                {
                    total = total,
                    start = BitConverter.ToUInt32(dlinfo, 0x20),
                    end   = BitConverter.ToUInt32(dlinfo, 0x24)
                };

                Worker_download.RunWorkerAsync(workerData); // We spawn the thread.
            }

            else
            {
                saveFileDialog1.Dispose();
            }
        }