//打开端口按钮 private void Opencom_button_Click(object sender, EventArgs e) { Button btn = (Button)sender; bool a, b; a = CTSerialPort.SetSP(com_list.Text, bode_list.Text, data_box.Text, stop_box.Text); b = CTSerialPort.OpenSP(); if (a && b) { MessageBox.Show("打开成功"); //保存串口参数 controlconfig.AddOrModifyItem("portnum", com_list.Text); controlconfig.AddOrModifyItem("bodenum", bode_list.Text); controlconfig.AddOrModifyItem("datanum", data_box.Text); controlconfig.AddOrModifyItem("stopnum", stop_box.Text); if (btn.FindForm().IsMdiChild) { return; } this.Close(); Form_SP_Test form_sp_test = new Form_SP_Test(); form_sp_test.ShowDialog(); return; } return; }
/// <summary> /// 开始按钮 点击 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void ButtonStart_Click(object sender, EventArgs e) { //判断manageReadData线程是否在运行 //如果是,则直接return //如果不是,继续下边动作 if (manageReadDataFlag) { MessageBox.Show("already start!"); return; } if (CTSerialPort.SetSP() && CTSerialPort.OpenSP()) //判断串口是否能开启 { tablename = DateTime.Now.ToString(); //定义内存表名时间前缀 cTMySql.CreateDSTable(tablename + "_channel1"); //创建内存表 cTMySql.CreateDSTable(tablename + "_channel2"); //创建内存表 CTSerialPort.SendSP("E"); Thread.Sleep(10); CTSerialPort.ClearInBuffer(); manageReadDataFlag = true; //线程红绿灯信号 通过 Thread mrd = new Thread(ManageReadDataThread); //实例化ManageReadData线程 mrd.IsBackground = true; //设为后台线程 mrd.Start(); //线程开始 CTSerialPort.SendSP("A"); //给下位机发送开始信号 //波形1绘制线程 reflashWave1ThreadFlag = true; Thread rfwave1 = new Thread(ReflashWave1Thread); rfwave1.IsBackground = true; rfwave1.Start(tablename + "_channel1"); //波形2绘制线程 reflashWave2ThreadFlag = true; Thread rfwave2 = new Thread(ReflashWave2Thread); rfwave2.IsBackground = true; rfwave2.Start(tablename + "_channel2"); Console.WriteLine("开始接收数据"); ButtonStop.Enabled = true; ButtonStart.Enabled = false; } else { MessageBox.Show("串口通信参数有误,请重新设置"); } }