using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.IO.Ports; using System.IO; using System.Text.RegularExpressions; using System.Configuration; using System.Timers; using ZedGraph; namespace Bubbler { public partial class Form1 : Form { //Command To Send Password const int BSL_TXPWORD = 0x10; //Command to Transmit Block const int BSL_TXBLK = 0x12; //Command to Recieve Block const int BSL_RXBLK = 0x14; //Command to Mass Erase const int BSL_MERAS = 0x18; const int BSL_ECHECK = 0x1C; //passwdFile is always null string passwdFile = null; //Finds the Saved .txt file name in Properties.DateFolder public string UploadedFileName = (string)(Properties.Settings.Default.DateFolder); //Finds the Saved Starting Port Number from Properties.ComportStartingNumber public int StartingPortNumber = (int)(Properties.Settings.Default.ComportStartingNumber); //Finds the Saved Number of Usable Prorts from Properties.Comports public int UsablePorts = Properties.Settings.Default.Comports; // public string filename = null; //Reference to CLASS FileSystemHandler FileSystemHandler FS = new FileSystemHandler(); //Holds Error Number (Not Used at this time) private int error; //Maximum Data bytes transferable in a block const int MAX_DATA_BYTES = 250; string PortName; Form3 GraphForm; string permanantfilename; //double[][] AllChannelBubblerFrequencyData = new double[UsablePorts][]; string fileWithPAppended; private byte[] blkin = new byte[MAX_DATA_BYTES]; /* Receive buffer */ private byte[] blkout = new byte[MAX_DATA_BYTES]; /* Receive buffer */ private byte[] serialnumber = new byte[8]; /* Hold Serial Number*/ public List AllPorts = new List(); public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { //Initialises the ports InitializePorts(UsablePorts); //Creates a new GasClassInstance and watches for a new file creation Gases GasClassInstance = new Gases(); GasClassInstance.CreateWatcher(); //Adds some file path info to the text box textBox1.AppendText("The default directory is " + Properties.Settings.Default.DefaultDirectory + "\r\n"); textBox1.AppendText("To change these go to File-Options\r\n"); textBox1.Refresh(); //Shows the current default directory from Properties.DefaultDirectory label2.Text = Properties.Settings.Default.DefaultDirectory; } public void InitializePorts(int Ports) { //****Important Function***** //Creates SerialPortCommunication Instances and adds all the Valid ones to the AllPorts List StartingPortNumber = Properties.Settings.Default.ComportStartingNumber; //Empties the AllPorts first AllPorts.Clear(); UsablePorts = Ports; for (int i = StartingPortNumber; i < (UsablePorts + StartingPortNumber); i++) { PortName = "Com"+i; AllPorts.Add(new SerialPortCommunication(this,PortName)); } } private void openToolStripMenuItem_Click(object sender, EventArgs e) { OpenFileDialog opnFile = new OpenFileDialog(); opnFile.Filter = "txt files (*.txt)|*.txt|All files(*.*)|*.*"; opnFile.InitialDirectory = Properties.Settings.Default.FileforUpload; if (opnFile.ShowDialog() == DialogResult.OK) { filename = opnFile.FileName; textBox4.Text = File.ReadAllText(opnFile.FileName); } else { opnFile.Dispose(); } DownFileToMicrochip.Enabled = true; } private void DownFileToMicrochip_Click(object sender, EventArgs e) { textBox1.Text = ""; DownFileToMicrochip.Enabled = false; UploadBubbleData.Enabled= false; AllPorts.ForEach(delegate(SerialPortCommunication q) { try { q.comInit(PortName); q.bslReset(true,q._SERIALPORT.PortName); //q.EraseMainMemory(); q.MassErases(); q.txPasswd(passwdFile); q.ProgramFile(filename); textBox1.Refresh(); } catch(Exception ex) { } }); DownFileToMicrochip.Enabled = true; UploadBubbleData.Enabled = true; RunProgram.Enabled = true; } private void UploadBubbleData_Click(object sender, EventArgs e) { FS.SetUpSearchforDate(); string currentfiledir = FS.CreateTimeDirectory(); progressBar1.Minimum = 0x1100; progressBar1.Maximum = 0x38FF; progressBar1.Step = ((0x4F9C - 0x6800)/0x00fa); UploadData(currentfiledir); timer1.Enabled = true; timer1.Interval = Convert.ToInt32((numericUpDown1.Value * 1000 * 60)); //CGL } private void RunProgram_Click(object sender, EventArgs e) { AllPorts.ForEach(delegate(SerialPortCommunication q) { q.bslReset(false,q._SERIALPORT.PortName); } ); } private void saveToolStripMenuItem_Click(object sender, EventArgs e) { Dispose(); } private void button2_Click_2(object sender, EventArgs e) { FolderBrowserDialog changeFolder = new FolderBrowserDialog(); changeFolder.RootFolder = Environment.SpecialFolder.MyComputer; changeFolder.ShowNewFolderButton= true; if (changeFolder.ShowDialog() == DialogResult.OK) { Properties.Settings.Default.DefaultDirectory = changeFolder.SelectedPath; Properties.Settings.Default.Save(); label2.Text = Properties.Settings.Default.DefaultDirectory; } } private void menuStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e) { } private void helpToolStripMenuItem_Click(object sender, EventArgs e) { } private void aboutToolStripMenuItem_Click(object sender, EventArgs e) { MessageBox.Show("Copyright 2008 Princeton University Physics Dept", "Bubble Rate Acquisition Program"); } private void timer1_Tick(object sender, EventArgs e) { timer1.Stop(); AllPorts.ForEach(delegate(SerialPortCommunication q) { q.bslReset(false,q._SERIALPORT.PortName); } ); System.Threading.Thread.Sleep(AllPorts.Count * 10000); timer1.Start(); DownLoadData(); } void DownLoadData() { FS.SetUpSearchforDate(); string currentfiledir = FS.CreateTimeDirectory(); progressBar1.Minimum = 0x1100; progressBar1.Maximum = 0x38FF; progressBar1.Step = ((0x4F9C - 0x6800) / 0x00fa); UploadData(currentfiledir); } private void button6_Click(object sender, EventArgs e) { if (timer1.Enabled == true) { timer1.Enabled = false; button6.Text = "Start Timer"; } else { timer1.Enabled = true; button6.Text = "Stop Timer"; } } private void zedGraphControl1_Load(object sender, EventArgs e) { } private void chartToolStripMenuItem_Click(object sender, EventArgs e) { //Form Form3 = new Form3(); //Form3.Show(); } public void UpdateTextBox1(string p) { textBox1.AppendText(p); } public void UploadData(string UploadedFileName) { string localfilename = UploadedFileName; int j = 0; double[][] AllChannelBubblerFrequencyData = new double[UsablePorts][]; AllPorts.ForEach(delegate(SerialPortCommunication q) { try { q.comInit(q._SERIALPORT.PortName); q.bslReset(true, q._SERIALPORT.PortName); q.txPasswd2(passwdFile); permanantfilename = (localfilename + "\\" + q._SERIALPORT.PortName + ".txt"); TextWriter tw = new StreamWriter(permanantfilename); //q.bslTxRx(BSL_RXBLK, 0x1000, 0x0008, null, serialnumber); try { for (uint g = 0x1000; g < 0x5000; g = (g + (0x00FA))) { q.bslTxRx(BSL_RXBLK, g, 0x00FA, null, blkin); //progressBar1.Value = (int)g; textBox1.Refresh(); //tw.Write(g); //tw.Write("\r\n"); for (int d = 0; d <= 249; d++) { string s = blkin[d].ToString("x2"); tw.Write(s); tw.Write(" "); if (d % 25 == 24) { tw.Write("\r\n"); } } } } catch(Exception ex) { MessageBox.Show(ex.Message); } tw.Close(); textBox1.Refresh(); tw.Close(); RunProgram.Enabled = true; ParseData p = new ParseData(q._SERIALPORT.PortName); p.FormatData(permanantfilename); fileWithPAppended = p.fileWithP; p.GetFrequency(fileWithPAppended); AllChannelBubblerFrequencyData[j] = p.BoardChannel; j++; textBox1.AppendText(q._SERIALPORT.PortName + " file Downloaded Sucessfully\r\n"); } catch (Exception ex) { j++; textBox1.AppendText("Port " + q._SERIALPORT.PortName + " " + ex.Message+"\r\n"); } }); try { GraphForm.Close(); } catch { } GraphForm = new Form3(AllChannelBubblerFrequencyData, UploadedFileName); GraphForm.Show(); } private SerialPort _serialPort; private void checkCommunicationToolStripMenuItem_Click(object sender, EventArgs e) { AllPorts.Clear(); UsablePorts = Properties.Settings.Default.Comports; for (int x = StartingPortNumber; x < (UsablePorts + StartingPortNumber); x++) { PortName = "Com" + x; AllPorts.Add(new SerialPortCommunication(this, PortName)); } AllPorts.ForEach(delegate(SerialPortCommunication q) { try { bool p = q._SERIALPORT.IsOpen; q.bslReset(true, q._SERIALPORT.PortName); q.bslTxRx(BSL_TXPWORD, 0xffe0, 0x0020, blkout, blkin); //textBox1.AppendText(q._SERIALPORT.PortName + " seems to be working\r\n"); textBox1.Refresh(); q._SERIALPORT.Close(); } catch (Exception ex) { //textBox1.AppendText("Port "+q._SERIALPORT.PortName+" "+ex.Message); } }); textBox1.AppendText("ALL DONE\r\n"); } private void optionsToolStripMenuItem_Click(object sender, EventArgs e) { Options myOptions = new Options(this); myOptions.Show(); } private void numericUpDown1_ValueChanged_1(object sender, EventArgs e) { if ((numericUpDown1.Value > 0)) //CGL | { timer1.Interval = Convert.ToInt32((numericUpDown1.Value * 1000 * 60)); //CGL } } private void fileToolStripMenuItem_Click(object sender, EventArgs e) { } private void button1_Click(object sender, EventArgs e) { } private void label4_Click(object sender, EventArgs e) { } private void label2_Click(object sender, EventArgs e) { } private void setCheckSerialNumbersToolStripMenuItem_Click(object sender, EventArgs e) { SetSerialNumber Sn = new SetSerialNumber(this); Sn.Show(); } private void percentagesToolStripMenuItem_Click(object sender, EventArgs e) { Graph_Gas GG = new Graph_Gas(); GG.ReadGasDataFromDatabase(); GG.Show(); } private void runUpdateToolStripMenuItem_Click(object sender, EventArgs e) { Gases GT = new Gases(); GT.ReadGasFile("xx","xx"); } } }