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; namespace Bubbler { public partial class SetSerialNumber : Form { uint addr= 0; ushort len = 0; const int BSL_TXPWORD = 0x10; //Command To Send Password const int BSL_TXBLK = 0x12; //Command to Transmit Block const int BSL_RXBLK = 0x14; const int BSL_ERASEBLOCK = 0x16; const int BSL_MERAS = 0x18; //Command to Mass Erase const int BSL_ECHECK = 0x1C; const int MAX_DATA_BYTES = 250; private byte[] blkout = new byte[MAX_DATA_BYTES]; /* Transmit buffer */ // private byte[] blkin = new byte[MAX_DATA_BYTES]; /* Receive buffer */ private byte[] blkin = new byte[32]; /* Receive buffer */ string PortName; string SerialNumber; string passwdFile = null; string SerialNumberFileName; private Form1 themainform; private byte[] serialnumber = new byte[40]; /* Hold Serial Number*/ public List AllPorts = new List(); public SetSerialNumber(Form1 parentform) { InitializeComponent(); themainform = parentform; } private void button1_Click(object sender, EventArgs e) { int i = 0; string thisserialnumber = textBox1.Text; foreach (char ch in thisserialnumber.ToCharArray()) { blkout[i]=(byte)ch; i++; } //PortName = textBox2.Text; PortName = System.Convert.ToString(listBox1.SelectedItem); SerialPortCommunication q = new SerialPortCommunication(this,themainform,PortName); q.comInit(PortName); q.bslReset(true, q._SERIALPORT.PortName); q.txPasswd2(passwdFile); q.bslTxRx(BSL_TXBLK,4096, 8, blkout, blkin); themainform.textBox1.Text = "Done"; if (q._SERIALPORT.IsOpen == true) q._SERIALPORT.Close(); } private void button2_Click(object sender, EventArgs e) { PortName = textBox2.Text; SerialPortCommunication q = new SerialPortCommunication(this,themainform, PortName); q.comInit(PortName); q.bslReset(true, q._SERIALPORT.PortName); q.txPasswd2(passwdFile); q.bslTxRx(BSL_RXBLK, 4096, 8, null, blkin); string str; System.Text.ASCIIEncoding enc = new System.Text.ASCIIEncoding(); str = enc.GetString(blkin); if (q._SERIALPORT.IsOpen == true) q._SERIALPORT.Close(); } private void button3_Click(object sender, EventArgs e) { PortName = textBox2.Text; SerialPortCommunication q = new SerialPortCommunication(this, themainform, PortName); q.comInit(textBox2.Text); q.bslReset(true, q._SERIALPORT.PortName); q.txPasswd2(passwdFile); q.bslTxRx(BSL_ERASEBLOCK, /* Command: Mass Erase */ 0x1000, /* Any address within flash memory. */ 0xa502, /* Required setting for mass erase! */ null, blkin); if (q._SERIALPORT.IsOpen == true) q._SERIALPORT.Close(); } private void button4_Click(object sender, EventArgs e) { PortName = textBox2.Text; SerialPortCommunication q = new SerialPortCommunication(this, themainform, PortName); q.comInit(PortName); q.bslReset(true, q._SERIALPORT.PortName); q.txPasswd(passwdFile); q.bslTxRx(BSL_RXBLK, 0xFFE0, 32, null, blkin); string str; System.Text.ASCIIEncoding enc = new System.Text.ASCIIEncoding(); str = enc.GetString(blkin); if (q._SERIALPORT.IsOpen == true) q._SERIALPORT.Close(); } private void button5_Click(object sender, EventArgs e) { PortName = textBox2.Text; SerialPortCommunication q = new SerialPortCommunication(this, themainform, PortName); q.comInit(textBox2.Text); q.bslReset(true, q._SERIALPORT.PortName); q.txPasswd(passwdFile); q.bslTxRx(BSL_ERASEBLOCK, /* Command: Mass Erase */ 0xff00, /* Any address within flash memory. */ 0xa502, /* Required setting for mass erase! */ null, blkin); if (q._SERIALPORT.IsOpen==true) q._SERIALPORT.Close(); } private void SetSerialNumber_Load(object sender, EventArgs e) { for (int c=0;c<=12;c++) listBox1.Items.Add(Properties.Settings.Default.ComportStartingNumber+c); } } }