/// <summary> /// 获取串口配信息 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnGetConfigComData_Click(object sender, EventArgs e) { ConfigComType configComType = cc.GetConfigComData(); string strMessage = string.Format("端口号:{0} \n\r波特率:{1} \n\r数据位:{2} \n\r停止位:{3} \n\r检验位:{4} ", configComType.PortName, configComType.BaudRate, configComType.DataBits, configComType.StopBits, configComType.Parity); MessageBox.Show(strMessage, "串口配置 ConfigCom", MessageBoxButtons.OK); }
/// <summary> /// 串口对手初始化 /// </summary> private void InitSerialPort() { //初始化串口配置类 configCom1 = new ConfigCom(this.cbSerial1, this.cbBaudRate1, this.cbDataBits1, this.cbStop1, this.cbParity1); configCom2 = new ConfigCom(this.cbSerial2, this.cbBaudRate2, this.cbDataBits2, this.cbStop2, this.cbParity2); //初始化串口助手类 serialPort1 = new SerialPortHelper(); serialPort1.ConfigSerialPort = configCom1.GetConfigComData(); serialPort1.BindSerialPortDataReceivedProcessEvent(new SerialPortHelper.DelegateSerialPortDataReceivedProcessEvent(SerialPortDataReceivedProcess)); serialPort1.BindSerialPortErrorEvent(new SerialPortHelper.DelegateSerialPortErrorEvent(SerialPortErrorProcess)); serialPort1.SerialReceviedTimeInterval = 40; serialPort1.SerialWriteTimeInterval = 1; serialPort1.SerialReceviedLengthMax = 1024; serialPort1.SerialMark = "串口1"; serialPort2 = new SerialPortHelper(); serialPort2.ConfigSerialPort = configCom1.GetConfigComData(); serialPort2.BindSerialPortDataReceivedProcessEvent(new SerialPortHelper.DelegateSerialPortDataReceivedProcessEvent(SerialPortDataReceivedProcess)); serialPort2.BindSerialPortErrorEvent(new SerialPortHelper.DelegateSerialPortErrorEvent(SerialPortErrorProcess)); serialPort2.SerialReceviedTimeInterval = 40; serialPort2.SerialWriteTimeInterval = 1; serialPort2.SerialReceviedLengthMax = 1024; serialPort2.SerialMark = "串口2"; }
/// <summary> /// 初始化串口助手 /// </summary> private void initSerialPortHelper() { serialPortHelper = new SerialPortHelper(); serialPortHelper.ConfigSerialPort = configCom.GetConfigComData(); serialPortHelper.BindSerialPortDataReceivedProcessEvent(new SerialPortHelper.DelegateSerialPortDataReceivedProcessEvent(SerialPortDataReceivedProcess)); serialPortHelper.BindSerialPortErrorEvent(new SerialPortHelper.DelegateSerialPortErrorEvent(SerialPortErrorProcess)); serialPortHelper.SerialReceviedTimeInterval = 40; serialPortHelper.SerialWriteTimeInterval = 200; serialPortHelper.SerialReceviedLengthMax = 1024; }
/// <summary> /// 打开/关闭串口按钮 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnSerialPortSwitch_Click(object sender, EventArgs e) { if (btnSerialPortSwitch.Text == "打开") { serialPortHelper.OpenCom(configCom.GetConfigComData(), out string strError); if (strError != "null") { MessageBox.Show(strError); } else { Console.WriteLine("开启{0}端口成功。", configCom.PortName); toolBleWrite.Enabled = !checkAutoLinkBle.Checked; btnSerialPortSwitch.Text = "关闭"; //ClearListViewSerialReceviedValue(); AddSerialWrite("AT"); //SaveSerialConfig(configCom.GetConfigComData()); } } else { serialPortHelper.CloseCom(out string strError); if (strError != "null") { MessageBox.Show(strError); } else { Console.WriteLine("关闭端口成功。"); toolBleWrite.Enabled = false; btnSerialPortSwitch.Text = "打开"; } } }
/// <summary> /// 串口开关按钮事件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void btnSerialPortSwitch_Click(object sender, EventArgs e) { Button btn = (Button)sender; ConfigCom cc = (btn.Tag.ToString() == "1") ? configCom1 : configCom2; SerialPortHelper spb = (btn.Tag.ToString() == "1") ? serialPort1 : serialPort2; CheckBox chkTimer = (btn.Tag.ToString() == "1") ? chkSerialWriteLoop1 : chkSerialWriteLoop2; GroupBox grp = btn.Parent as GroupBox; if (btn.Text == "打开串口") { string strError; try { spb.OpenCom(cc.GetConfigComData(), out strError); } catch (Exception err) { strError = err.Message; } if (strError != "null") { MessageBox.Show(strError); } else { cc.GetDetectCom().Close(); Console.WriteLine("开启{0}端口成功。", cc.PortName); btn.Text = "关闭串口"; GroupBoxEnable(grp, false); } } else { spb.CloseCom(out string strError); if (strError != "null") { MessageBox.Show(strError); } else { cc.GetDetectCom().Open(); Console.WriteLine("关闭端口成功。"); btn.Text = "打开串口"; GroupBoxEnable(grp, true); chkTimer.Checked = false; } } }
/// <summary> /// 初始化串口助手 /// </summary> private void InitSerialPortHelper() { //实例化串口配置 configCom = new ConfigCom(comboSerial); configCom.PortName = comboSerial.Text; configCom.BaudRate = 115200; configCom.DataBits = 8; configCom.StopBits = StopBits.One; configCom.Parity = Parity.None; //设置串口搜索默认值 configCom.SetSerialPortDefaultInfo("USB 串行设备"); //TI CC2540 USB CDC Serial Port configCom.AddSerialPortDefaultInfo("TI CC2540 USB CDC Serial Port"); //实例化串口助手 serialPortHelper = new SerialPortHelper(configCom.GetConfigComData()); serialPortHelper.BindSerialPortDataReceivedProcessEvent(new SerialPortHelper.DelegateSerialPortDataReceivedProcessEvent(SerialPortDataReceivedProcess)); serialPortHelper.BindSerialPortErrorEvent(new SerialPortHelper.DelegateSerialPortErrorEvent(SerialPortErrorProcess)); serialPortHelper.SerialReceviedTimeInterval = 40; serialPortHelper.SerialWriteTimeInterval = 200; serialPortHelper.SerialReceviedLengthMax = 1024; }