Пример #1
0
        /*
         * Do the programming
         */
        private void _btnProgram_Click(object sender,EventArgs e)
        {
            List<ImageFile> files;
              long total;
              ImageFile info;
              SerialPort sp=null;

              // protocol is 8 bits, 1 stop bit

              try {

            sp=new SerialPort(_serialPort.Text,int.Parse(_baudRate.Text),Parity.None,8,StopBits.One);
            sp.ReadTimeout=10000;
            sp.WriteTimeout=10000;
            sp.Open();

            if(!sp.IsOpen)
              throw new Exception(_serialPort.Text+" is not open");

            sp.DataReceived+=OnDataReady;

            // gather the files

            files=new List<ImageFile>();
            total=0;

            foreach(ListViewItem lvi in _filesList.Items) {

              info=new ImageFile() {
            Address=int.Parse(lvi.SubItems[1].Text),
            Info=new FileInfo(lvi.SubItems[0].Text)
              };

              total+=info.Info.Length;

              files.Add(info);
            }

            // set up the progress control

            using(ProgressForm progress=new ProgressForm()) {

              progress.Total=(int)total;

              // program it

              total=0;

              progress.Show();

              foreach(ImageFile i in files) {

            progress.Prompt=i.Info.Name;
            ProgramFile(sp,i,progress,ref total);
              }
            }
              } catch(Exception ex) {
            MessageBox.Show(ex.Message,"Program Error",MessageBoxButtons.OK,MessageBoxIcon.Exclamation);
              } finally {
            if(sp != null) {
              sp.DataReceived-=OnDataReady;
              sp.Close();
            }
              }
        }
Пример #2
0
        /*
         * Do the programming
         */

        private void _btnProgram_Click(object sender, EventArgs e)
        {
            List <ImageFile> files;
            long             total;
            ImageFile        info;
            SerialPort       sp = null;

            // protocol is 8 bits, 1 stop bit

            try {
                sp              = new SerialPort(_serialPort.Text, int.Parse(_baudRate.Text), Parity.None, 8, StopBits.One);
                sp.ReadTimeout  = 10000;
                sp.WriteTimeout = 10000;
                sp.Open();

                if (!sp.IsOpen)
                {
                    throw new Exception(_serialPort.Text + " is not open");
                }

                sp.DataReceived += OnDataReady;

                // gather the files

                files = new List <ImageFile>();
                total = 0;

                foreach (ListViewItem lvi in _filesList.Items)
                {
                    info = new ImageFile()
                    {
                        Address = int.Parse(lvi.SubItems[1].Text),
                        Info    = new FileInfo(lvi.SubItems[0].Text)
                    };

                    total += info.Info.Length;

                    files.Add(info);
                }

                // set up the progress control

                using (ProgressForm progress = new ProgressForm()) {
                    progress.Total = (int)total;

                    // program it

                    total = 0;

                    progress.Show();

                    foreach (ImageFile i in files)
                    {
                        progress.Prompt = i.Info.Name;
                        ProgramFile(sp, i, progress, ref total);
                    }
                }
            } catch (Exception ex) {
                MessageBox.Show(ex.Message, "Program Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            } finally {
                if (sp != null)
                {
                    sp.DataReceived -= OnDataReady;
                    sp.Close();
                }
            }
        }
Пример #3
0
        /*
         * Do the actual programming
         */
        private void ProgramFile(SerialPort sp,ImageFile info,ProgressForm progress,ref long total)
        {
            int address,pages,i,fileBytesRead,length;
              byte[] page;

              // set up for the programming loop

              address=info.Address;
              length=(int)info.Info.Length;

              page=new byte[256];

              if((length % 256)==0)
            pages=length/256;
              else
            pages=(length/256)+1;

              // program each page

              using(FileStream fs=info.Info.OpenRead()) {

            for(i=0;i<pages;i++) {

              // read the next page - the last one may be a short read if
              // last page and length is not a page size multiple

              Array.Clear(page,0,page.Length);
              fileBytesRead=fs.Read(page,0,page.Length);

              SendPreamble(sp);
              SendAddress(sp,address);
              SendPage(sp,page);
              ReadResponse(sp,progress);

              // update address for next write

              address+=256;

              // update progress

              if(progress.Cancelled)
            throw new Exception("User cancelled");

              progress.Progress=(int)total;
              total+=fileBytesRead;
            }
              }
        }