/* * 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(); } } }
/* * Read the response */ private void ReadResponse(SerialPort sp,ProgressForm progress) { byte[] response; int i; // wait for 30 seconds max for(i=0;i<60;i++) { if(_dataReadyEvent.WaitOne(TimeSpan.FromMilliseconds(500))) break; Application.DoEvents(); if(progress.Cancelled) throw new Exception("User cancelled"); } if(i==60) throw new Exception("No response received from the Arduino after 30 seconds"); response=new byte[1]; sp.Read(response,0,1); if(response[0]!=1) throw new Exception("Serial transfer error: the Arduino NACK'd the data"); }
/* * 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(); } } }
/* * 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; } } }