/// <summary> /// 窗体关闭 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void Form_Closing(object sender, FormClosingEventArgs e) { Form fm = (Form)sender; //fm.Enabled = false; switch (fm.Name) { case "CMcuFormAVR8BitsForm": if (e.CloseReason == CloseReason.MdiFormClosing) { return; } else if (e.CloseReason == CloseReason.UserClosing) { if (DialogResult.OK == CMessageBoxPlus.Show(this, "你确定要关闭应用程序吗?", "关闭提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question)) { //---为保证Application.Exit();时不再弹出提示,所以将FormClosing事件取消 this.FormClosing -= new FormClosingEventHandler(this.Form_Closing); //---检查通讯端口 if (this.defaultCComm != null) { //---关闭端口 while (true) { if (this.defaultCComm.mCOMMSTATE == CCOMM_STATE.STATE_IDLE) { break; } Application.DoEvents(); } this.defaultCComm.CloseDevice(); } //---确认关闭事件 e.Cancel = false; //---退出当前窗体 this.Dispose(); } else { //---取消关闭事件 e.Cancel = true; } } break; default: break; } //fm.Enabled = true; fm.Focus(); }
/// <summary> /// Button事件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void Button_Click(object sender, EventArgs e) { //---检验当前任务空闲 if ((sender == null) || (this.defaultTaskIdle == true)) { return; } if (this.defaultCComm.mCOMMOpen == false) { CMessageBoxPlus.Show(this, "通讯端口初始化异常!", "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } Button bt = (Button)sender; //---任务执行中 this.defaultTaskIdle = true; //bt.Enabled = false; //bt.Focus(); //---无参数的线程 ThreadStart threadStart = delegate { if (bt.InvokeRequired) { bt.BeginInvoke((EventHandler) (delegate { this.UI_Button_Click(bt); })); } else { this.UI_Button_Click(bt); } }; //---后台线程执行函数 Thread t = new Thread(threadStart); //---校验线程有效 if (t != null) { //---后台任务 t.IsBackground = true; //---启动任务 t.Start(); } //this.UI_Button_Click(bt); //bt.Enabled = true; }
/// <summary> /// /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void Form_FormClosing(object sender, FormClosingEventArgs e) { switch (e.CloseReason) { case CloseReason.None: break; case CloseReason.WindowsShutDown: break; case CloseReason.MdiFormClosing: case CloseReason.UserClosing: if (DialogResult.OK == CMessageBoxPlus.Show(this, "你确定要关闭退出登录吗?", "关闭提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question)) { if (this.IsMdiContainer) { //----为保证Application.Exit();时不再弹出提示,所以将FormClosing事件取消 this.FormClosing -= new System.Windows.Forms.FormClosingEventHandler(this.Form_FormClosing); } //---确认关闭事件 e.Cancel = false; //---退出当前窗体 this.Dispose(); } else { //---取消关闭事件 e.Cancel = true; } break; case CloseReason.TaskManagerClosing: break; case CloseReason.FormOwnerClosing: break; case CloseReason.ApplicationExitCall: break; default: break; } }
/// <summary> /// 使用PreMaKe创建VS项目 /// </summary> /// <returns></returns> private bool UsePreMakeToVisualStudio() { //---要装换为vs工程的文件 string toVSPath = null; //---vs项目的文件 string vsSlnPath = null; //---获取文件工程 string vsPath = null; //---文件名称 string fileName = null; if (this.comboBox_ProjectIDE.Text == "IAR") { toVSPath = Path.GetDirectoryName(this.TextBox_SrcProjectPath.Text);; } else if (this.comboBox_ProjectIDE.Text == "Keil") { toVSPath = Path.GetDirectoryName(this.TextBox_SrcProjectPath.Text); } else { return(false); } //---解决方案路径 vsSlnPath = Path.GetDirectoryName(toVSPath); //---工程路径 vsPath = toVSPath.Split('\\')[vsSlnPath.Split('\\').Length]; //---文件名称 fileName = Path.ChangeExtension(this.TextBox_SrcProjectPath.Text, "vcxproj").Split('\\')[Path.ChangeExtension(this.TextBox_SrcProjectPath.Text, "vcxproj").Split('\\').Length - 1]; //---解决方案路劲 vsSlnPath = this.TextBox_SrcProjectPath.Text.Replace("\\" + vsPath, ""); //---启动进程 Process proc = new Process { StartInfo = new ProcessStartInfo { FileName = @"Resources\premake5.exe", Arguments = "--File=\"" + toVSPath + "\\premake5.lua\" " + this.comboBox_VSVersion.Text, UseShellExecute = false, RedirectStandardOutput = true, RedirectStandardError = true, RedirectStandardInput = true, CreateNoWindow = true } }; //---启动应用 proc.Start(); string makeOut = proc.StandardOutput.ReadToEnd(); //---创建VS工程 if (proc.ExitCode == 0) { CMessageBoxPlus.Show(this, makeOut, @"Make output"); //---创建VS工程 if (this.comboBox_VSVersion.Text.Contains("vs")) { //---移动vssln文件 //---读取sln文件 string str = Path.ChangeExtension(this.TextBox_SrcProjectPath.Text, "sln"); //---读取数据 StreamReader sr = new StreamReader(str, Encoding.UTF8); //---读取的内容 string content = sr.ReadToEnd(); sr.Close(); content = content.Replace(fileName, vsPath + "\\" + fileName); //---修改正路径 str = str.Replace("\\" + vsPath, ""); //---写入文件 StreamWriter sw = new StreamWriter(str, false, Encoding.UTF8); sw.Write(content); sw.Close(); //---是否需要启动VS if (this.checkBox_OpenVSProject.Checked) { DialogResult dialogResult = CMessageBoxPlus.Show(this, "\tOpen " + this.comboBox_VSVersion.Text + " Project ?", this.Text, MessageBoxButtons.YesNo); if (dialogResult == DialogResult.Yes) { ProcessStartInfo psi = new ProcessStartInfo(Path.ChangeExtension(vsSlnPath, "sln")); Process.Start(psi); } } } } else { CMessageBoxPlus.Show(this, makeOut, @"Make output", MessageBoxButtons.OK, MessageBoxIcon.Error); } return(true); }
/// <summary> /// 打开设备 /// </summary> /// <param name="argName"></param> /// <param name="msg"></param> /// <returns></returns> public override int OpenDevice(string argName, RichTextBox msg = null) { int _return = 0; if ((!string.IsNullOrEmpty(argName)) && (argName.StartsWith("COM", System.StringComparison.CurrentCultureIgnoreCase))) { //---判断串口类是否初始化 if (this.defaultSerialPort == null) { this.defaultSerialPort = new SerialPort(); } //---判断当前端口是否可用 if (this.defaultSerialPort.IsOpen) { //---等待端口事件处理完成 while (!((this.defaultSerialSTATE == CCOMM_STATE.STATE_IDLE) || (this.defaultSerialSTATE == CCOMM_STATE.STATE_ERROR))) { Application.DoEvents(); } //---端口状态,空闲 this.defaultSerialSTATE = CCOMM_STATE.STATE_IDLE; //---关闭端口 this.defaultSerialPort.Close(); } //---判断端口状态 if (this.defaultSerialPort.IsOpen == false) { //---获取端口名称 if (this.defaultSerialPort.PortName != argName) { this.defaultSerialPort.PortName = argName; } //---查空操作 if (this.defaultSerialPortParam == null) { this.defaultSerialPortParam = new CSerialPortParam(); } //---使用的设备端口 this.mCOMMName = argName; //---波特率 if (this.defaultSerialPort.BaudRate != int.Parse(this.defaultSerialPortParam.mBaudRate)) { this.defaultSerialPort.BaudRate = int.Parse(this.defaultSerialPortParam.mBaudRate); } //---校验位 if (this.defaultSerialPort.Parity != this.GetParityBits(this.defaultSerialPortParam.mParity)) { this.defaultSerialPort.Parity = this.GetParityBits(this.defaultSerialPortParam.mParity); } //---停止位 if (this.defaultSerialPort.StopBits != this.GetStopBits(this.defaultSerialPortParam.mStopBits)) { this.defaultSerialPort.StopBits = this.GetStopBits(this.defaultSerialPortParam.mStopBits); } //---数据位 if (this.defaultSerialPort.DataBits != int.Parse(this.defaultSerialPortParam.mDataBits)) { this.defaultSerialPort.DataBits = int.Parse(this.defaultSerialPortParam.mDataBits); } try { //---打开端口 this.defaultSerialPort.Open(); //---判断端口打开是否成功 if (this.defaultSerialPort.IsOpen == false) { //---端口状态,错误 this.defaultSerialSTATE = CCOMM_STATE.STATE_ERROR; this.defaultSerialMsg = "端口:" + this.mCOMMName + "打开失败!"; _return = 2; } else { this.defaultConnected = true; this.defaultSerialMsg = "端口:" + this.mCOMMName + "打开成功!"; //---注册事件接收函数 if (this.defaultHaveEventDataReceivedState == false) { this.defaultSerialPort.DataReceived += new SerialDataReceivedEventHandler(this.HandleDataReceivedEvent); this.defaultHaveEventDataReceivedState = true; } _return = 0; } } catch { this.defaultSerialMsg = "端口:" + this.mCOMMName + "打开异常!"; _return = 3; } } else { //---端口状态,错误 this.defaultSerialSTATE = CCOMM_STATE.STATE_ERROR; this.defaultSerialMsg = "端口:" + argName + "初始化失败!"; _return = 4; } } if (_return > 0) { //---消息插件弹出 CMessageBoxPlus.Show(this.mCCommForm, this.defaultSerialMsg + "\r\n" + "错误号:" + _return.ToString() + "\r\n", "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Error); } else if (_return < 0) { CMessageBoxPlus.Show(this.mCCommForm, "端口名称不合法!\r\n", "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Error); _return = 5; } else { //---消息显示 if (msg != null) { CRichTextBoxPlus.AppendTextInfoTopWithDataTime(msg, this.defaultSerialMsg, _return == 0 ? Color.Black : Color.Red); } } return(_return); }
/// <summary> /// /// </summary> /// <param name="bt"></param> private void UI_Button_Click(Button bt) { switch (bt.Name) { //---设备接口发生变化 case "button_SetChipInterface": if (!string.IsNullOrEmpty(this.comboBox_ChipInterface.Text)) { switch (this.comboBox_ChipInterface.Text.ToUpper()) { case "ISP": if (this.defaultCMcuFunc.GetType() != typeof(CMcuFuncAVR8BitsISP)) { this.defaultCMcuFunc = new CMcuFuncAVR8BitsISP(this.defaultCMcuFunc); } break; case "JTAG": if (this.defaultCMcuFunc.GetType() != typeof(CMcuFuncAVR8BitsJTAG)) { this.defaultCMcuFunc = new CMcuFuncAVR8BitsJTAG(this.defaultCMcuFunc); } break; case "HVPP": if (this.defaultCMcuFunc.GetType() != typeof(CMcuFuncAVR8BitsHVPP)) { this.defaultCMcuFunc = new CMcuFuncAVR8BitsHVPP(this.defaultCMcuFunc); } break; case "HVSP": if (this.defaultCMcuFunc.GetType() != typeof(CMcuFuncAVR8BitsHVSP)) { this.defaultCMcuFunc = new CMcuFuncAVR8BitsHVSP(this.defaultCMcuFunc); } break; default: break; } //---写入配置信息 CIniFile ini = new CIniFile(Application.StartupPath + @"\Config.ini", true); //---记录设备接口 if (ini.CIniFileSectionExists("Interface")) { ini.CIniFileEraseSection("Interface"); } ini.CIniFileWriteInt("Interface", this.comboBox_ChipInterface.Text, this.comboBox_ChipInterface.SelectedIndex); this.cMcuFormAVR8BitsFuseAndLockControl_ChipFuse.mCMcuFunc = this.defaultCMcuFunc; } else { CMessageBoxPlus.Show(this, "???不识别的编程接口", "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Error); } break; //---读取供电电压 case "button_ReadChipPWR": this.defaultCMcuFunc.CMcuFunc_ReadChipPower(this.textBox_ChipPWR, this.cRichTextBoxEx_ChipMsg); break; //---设置供电电压 case "button_SetChipPWR": this.defaultCMcuFunc.CMcuFunc_WriteChipPower(this.textBox_ChipPWR, this.cRichTextBoxEx_ChipMsg, this.checkBox_ChipPWR.Checked); break; //---设置编程时钟 case "button_SetChipClock": this.defaultCMcuFunc.CMcuFunc_SetProgClock((byte)this.trackBar_ChipClock.Value, this.cRichTextBoxEx_ChipMsg); break; //---加载Flash数据 case "button_LoadFlashFile": this.defaultCMcuFunc.CMcuFunc_LoadFlashHexFile(this.cHexBox_Flash, this.label_FlashSize, this.cRichTextBoxEx_ChipMsg); break; //---加载Eeprom case "button_LoadEepromFile": this.defaultCMcuFunc.CMcuFunc_LoadEepromHexFile(this.cHexBox_Eeprom, this.label_EepromSize, this.cRichTextBoxEx_ChipMsg); break; //---保存Flash case "button_SaveFlashFile": this.defaultCMcuFunc.CMcuFunc_SaveFlashHexFile(this.cHexBox_Flash, this.cRichTextBoxEx_ChipMsg); break; //---保存EEPROM文件 case "button_SaveEepromFile": this.defaultCMcuFunc.CMcuFunc_SaveEepromHexFile(this.cHexBox_Eeprom, this.cRichTextBoxEx_ChipMsg); break; //---自动 case "button_AutoChip": case "button_ChipAuto": this.defaultCMcuFunc.CMcuFunc_DoChipTask(this.defaultCMcuFunc.mMcuInfoParam.mChipFuse, Convert.ToByte(this.mLockFuse.Text, 16), this.cHexBox_Flash, this.cHexBox_Eeprom, this.cRichTextBoxEx_ChipMsg, this.toolStripLabel_ChipState, this.toolStripLabel_ChipTime, this.toolStripProgressBar_ChipBar); break; //---擦除 case "button_Erase": case "button_EraseChip": this.defaultCMcuFunc.CMcuFunc_EraseChip(this.mLockFuse, this.cRichTextBoxEx_ChipMsg); break; //---存储器查空操作 case "button_CheckEmpty": this.defaultCMcuFunc.CMcuFunc_CheckChipMemeryEmpty(this.cRichTextBoxEx_ChipMsg, false); break; //---读取识别字 case "button_ReadIDChip": case "button_ReadChipID": this.defaultCMcuFunc.CMcuFunc_ReadChipID(this.cRichTextBoxEx_ChipMsg, this); break; //---读取Flash case "button_ReadChipFlash": this.defaultCMcuFunc.CMcuFunc_ReadChipFlash(this.cHexBox_Flash, this.cRichTextBoxEx_ChipMsg, this.toolStripLabel_ChipState, this.toolStripLabel_ChipTime, this.toolStripProgressBar_ChipBar); break; //---编程Flash case "button_WriteChipFlash": this.defaultCMcuFunc.CMcuFunc_WriteChipFlash(this.cHexBox_Flash, this.cRichTextBoxEx_ChipMsg, true, this.toolStripLabel_ChipState, this.toolStripLabel_ChipTime, this.toolStripProgressBar_ChipBar); break; //---校验Flash case "button_CheckChipFlash": this.defaultCMcuFunc.CMcuFunc_CheckChipFlash(this.cHexBox_Flash, this.cRichTextBoxEx_ChipMsg, this.toolStripLabel_ChipState, this.toolStripLabel_ChipTime, this.toolStripProgressBar_ChipBar); break; //---读取Eeprom case "button_ReadChipEeprom": this.defaultCMcuFunc.CMcuFunc_ReadChipEeprom(this.cHexBox_Eeprom, this.cRichTextBoxEx_ChipMsg, this.toolStripLabel_ChipState, this.toolStripLabel_ChipTime, this.toolStripProgressBar_ChipBar); break; //---编程Eeprom case "button_WriteChipEeprom": this.defaultCMcuFunc.CMcuFunc_WriteChipEeprom(this.cHexBox_Eeprom, this.cRichTextBoxEx_ChipMsg, true, this.toolStripLabel_ChipState, this.toolStripLabel_ChipTime, this.toolStripProgressBar_ChipBar); break; //---校验Eeprom case "button_CheckChipEeprom": this.defaultCMcuFunc.CMcuFunc_CheckChipEeprom(this.cHexBox_Eeprom, this.cRichTextBoxEx_ChipMsg, this.toolStripLabel_ChipState, this.toolStripLabel_ChipTime, this.toolStripProgressBar_ChipBar); break; //---编程熔丝位 case "button_WriteChipFuse": this.defaultCMcuFunc.CMcuFunc_WriteChipFuse(this.cMcuFormAVR8BitsFuseAndLockControl_ChipFuse.mCMcuFunc.mMcuInfoParam.mChipFuse, this.cRichTextBoxEx_ChipMsg); break; //---编程加密位 case "button_WriteChipLock": this.defaultCMcuFunc.CMcuFunc_WriteChipLock(this.mLockFuse, this.cRichTextBoxEx_ChipMsg); break; //---读取ROM信息 case "button_ReadChipROM": this.defaultCMcuFunc.CMcuFunc_ReadChipRom(this.cHexBox_ROM, this.cRichTextBoxEx_ChipMsg, true); break; default: break; } //---任务空闲 this.defaultTaskIdle = false; }
/// <summary> /// TabControl页切换事件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void TabControl_Selected(object sender, TabControlEventArgs e) { if (sender == null) { return; } //---转化为TabControl控件 TabControl tcl = (TabControl)sender; //tcl.Focus(); //---判断控件 switch (tcl.Name) { case "tabControl_ChipMenu": if (tcl.SelectedTab.Text == "编程") { if (this.cHexBox_Flash.mNewDataChange || (this.cHexBox_Eeprom.mNewDataChange)) { if (DialogResult.OK == CMessageBoxPlus.Show(this, "是否更新数据缓存区?", "问题提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question)) { //---Flash数据发生变化 if (this.cHexBox_Flash.mNewDataChange) { this.cHexBox_Flash.mNewDataChange = false; this.cHexBox_Flash.AddData(this.cHexBox_Flash.mNowData); } //---Eeprom数据发生变化 if (this.cHexBox_Eeprom.mNewDataChange) { this.cHexBox_Eeprom.mNewDataChange = false; this.cHexBox_Eeprom.AddData(this.cHexBox_Eeprom.mNowData); } } else { //---Flash数据发生变化 if (this.cHexBox_Flash.mNewDataChange) { this.cHexBox_Flash.mNewDataChange = false; this.cHexBox_Flash.AddData(this.cHexBox_Flash.mLastData); } //---Eeprom数据发生变化 if (this.cHexBox_Eeprom.mNewDataChange) { this.cHexBox_Eeprom.mNewDataChange = false; this.cHexBox_Eeprom.AddData(this.cHexBox_Eeprom.mLastData); } } } } break; case "tabControl_ChipMemery": break; default: break; } }
/// <summary> /// 菜单单击事件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void ToolStripMenuItem_Click(object sender, EventArgs e) { ToolStripMenuItem tsm = (ToolStripMenuItem)sender; tsm.Enabled = false; int i = 0; string exePatch = null; switch (tsm.Name) { //---退出操作 case "ToolStripMenuItem_ExitTools": if (DialogResult.OK == CMessageBoxPlus.Show(this, "你确定要关闭应用程序吗?", "关闭提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question)) { if (this.IsMdiContainer) { //----为保证Application.Exit();时不再弹出提示,所以将FormClosing事件取消 this.FormClosing -= new System.Windows.Forms.FormClosingEventHandler(this.Form_FormClosing); } //---退出当前窗体 this.Dispose(); } break; //---调用计算器,只能调用,不能嵌套 case "ToolStripMenuItem_CalcTools": System.Diagnostics.ProcessStartInfo Info = new System.Diagnostics.ProcessStartInfo(); Info.FileName = "calc.exe "; //"calc.exe"为计算器,"notepad.exe"为记事本 System.Diagnostics.Process Proc = System.Diagnostics.Process.Start(Info); break; //---调用文本编辑器 case "ToolStripMenuItem_NoteTools": for (i = 0; i < this.MdiChildren.Length; i++) { if (this.MdiChildren[i].Text == "记事本") { //---判断当前窗体是否处于最小化状态, if (this.MdiChildren[i].WindowState == FormWindowState.Minimized) { this.MdiChildren[i].WindowState = FormWindowState.Normal; } this.MdiChildren[i].Activate(); return; } } exePatch = CGenFuncEXE.GenFuncGetExeNamePatch("notepad++.exe"); //---检查应用是否存在 if (exePatch == null) { exePatch = @"C:\Windows\system32\notepad.exe"; } //---将外部应用嵌套当前窗体 CEmbeddedForm txtForm = new CEmbeddedForm(exePatch, "记事本"); txtForm.MdiParent = this; txtForm.StartPosition = FormStartPosition.CenterScreen; txtForm.Show(); txtForm.Focus(); //txtForm.StartProcess(@"C:\Windows\system32\notepad.exe"); break; //---AVR的8Bit的MCU的在线下载器 case "ToolStripMenuItem_OnlineAVRDownTools": this.FormCMcuFormAVR8BitsForm_Init(); break; default: break; } tsm.Enabled = true; }
/// <summary> /// 错误信息 /// </summary> public virtual void ErrMessage() { CMessageBoxPlus.Show(null, "设备操作异常!错误操作:" + this.button_COMM.Text, "错误提示"); }