Beispiel #1
0
        private void toolBtOpenCom_Click(object sender, EventArgs e)
        {
            if (toolCmbPort.Items.Count <= 0)
            {
                MessageBox.Show("未发现可用串口,请检查硬件设备");
                return;
            }

            if (comDev.IsOpen == false)
            {
                comDev.PortName = toolCmbPort.ComboBox.SelectedValue.ToString();
                comDev.BaudRate = int.Parse(toolCmbBaudrate.Text);
                comDev.DataBits = 8;
                comDev.StopBits = StopBits.One;
                comDev.Parity   = Parity.None;
                try
                {
                    comDev.Open();
                    toolBtOpenCom.Text  = "Close";
                    toolBtOpenCom.Image = Properties.Resources.BMP_GREEN;
                    timPeridic.Start();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "打开失败", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }

                byte[] cmd1 = bglib.BLECommandSMSetBondableMode(1);
                bglib.SendCommand(comDev, cmd1);
                byte[] cmd2 = bglib.BLECommandSMSetParameters(1, 7, 2);//Keyboard Only
                bglib.SendCommand(comDev, cmd2);
                byte[] cmd3 = bglib.BLECommandSMWhitelistBonds();
                bglib.SendCommand(comDev, cmd3);
            }
            else
            {
                try
                {
                    comDev.Close();
                    toolBtOpenCom.Text  = "Open";
                    toolBtOpenCom.Image = Properties.Resources.BMP_GRAY;
                    timPeridic.Stop();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "关闭失败", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }
            }
            SetEnableByComSts(comDev.IsOpen);
        }