/// <summary>
        /// 初始化数据
        /// </summary>
        private void InitUI(ElectronicScalesConfig config)
        {
            txtConfigName.Text = _ElectronicScalesControl.ConfigName;
            txtConfigName.Enabled = false;

            cmbType.Properties.Items.AddEnum(typeof(ElectronicScalesType));
            cmbType.EditValue = _ElectronicScalesControl.ElectronicScalesType;

            string[] portNames = SerialPort.GetPortNames();//串口名称
            if (portNames.Length <= 0)
            {
                MessageService.ShowError("本机没有检测到串口,请检查。");
                cmbPortName.Enabled = false;
            }
            else
            {
                cmbPortName.Properties.Items.AddRange(portNames);
                if (portNames.Any(p => p.Equals(config.PortName)))
                    cmbPortName.EditValue = config.PortName;
                else
                    cmbPortName.SelectedIndex = 0;
            }

            txtBautTate.Text = config.BaudRate.ToString();//每秒位数(比特率)
            txtDataBits.Text = config.DataBits.ToString();//数据位

            cmbParity.Properties.Items.Add(Parity.None);//奇偶校验
            cmbParity.Properties.Items.Add(Parity.Even);
            cmbParity.Properties.Items.Add(Parity.Mark);
            cmbParity.Properties.Items.Add(Parity.Odd);
            cmbParity.Properties.Items.Add(Parity.Space);
            cmbParity.EditValue = config.Parity;

            cmbStopBit.Properties.Items.Add(StopBits.None);//停止位
            cmbStopBit.Properties.Items.Add(StopBits.One);
            cmbStopBit.Properties.Items.Add(StopBits.OnePointFive);
            cmbStopBit.Properties.Items.Add(StopBits.Two);
            cmbStopBit.EditValue = config.StopBits;

            cmbHandshake.Properties.Items.Add(Handshake.None);//流控制
            cmbHandshake.Properties.Items.Add(Handshake.RequestToSend);
            cmbHandshake.Properties.Items.Add(Handshake.RequestToSendXOnXOff);
            cmbHandshake.Properties.Items.Add(Handshake.XOnXOff);
            cmbHandshake.EditValue = config.Handshake;

        }
        public void ApplyConfig(ElectronicScalesConfig config)
        {
            if (config == null)
                throw new ArgumentNullException("config");

            this.ConfigName = config.ConfigName;
            this.ElectronicScalesType = config.ElectronicScalesType;

            if (ComPortEnable)
            {
                if (!string.IsNullOrWhiteSpace(config.PortName))
                    this.serialPort.PortName = config.PortName;

                this.serialPort.BaudRate = config.BaudRate;
                this.serialPort.DataBits = config.DataBits;
                this.serialPort.Parity = config.Parity;
                this.serialPort.StopBits = config.StopBits;
                this.serialPort.Handshake = config.Handshake;
            }

        }
        /// <summary>
        /// 点击保存
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSave_Click(object sender, EventArgs e)
        {
            int i = 0;
            var result = int.TryParse(this.txtBautTate.EditValue.ToStringEx(), out i);
            if (!result)
            {
                MessageService.ShowError("每秒位数必须为数字.");
                return;
            }
            result = int.TryParse(this.txtDataBits.EditValue.ToStringEx(), out i);
            if (!result)
            {
                MessageService.ShowError("数据位必须为数字.");
                return;
            }

            ElectronicScalesConfig config = new ElectronicScalesConfig();
            config.ConfigName = txtConfigName.Text;
            config.ElectronicScalesType = (ElectronicScalesType)cmbType.EditValue;
            config.PortName = cmbPortName.EditValue.ToStringEx();
            config.BaudRate = Convert.ToInt32(txtBautTate.Text);
            config.DataBits = Convert.ToInt32(txtDataBits.Text);
            config.Parity = (Parity)cmbParity.EditValue;
            config.StopBits = (StopBits)cmbStopBit.EditValue;
            config.Handshake = (Handshake)cmbHandshake.EditValue;
            config.StopBits = (StopBits)cmbStopBit.EditValue;
            ElectronicScalesConfigManager.Current.AddSetting(config);

            MessageService.ShowMessage("配置保存成功。");
            this.DialogResult = DialogResult.OK;
        }
        public ElectronicScalesConfig GetConfig()
        {
            var config = new ElectronicScalesConfig();
            config.ConfigName = this.ConfigName;
            config.ElectronicScalesType = this.ElectronicScalesType;


            config.PortName = this.serialPort.PortName;
            config.BaudRate = Convert.ToInt32(this.serialPort.BaudRate);
            config.DataBits = this.serialPort.DataBits;
            config.Parity = this.serialPort.Parity;
            config.StopBits = this.serialPort.StopBits;
            config.Handshake = this.serialPort.Handshake;

            return config;
        }
        public void AddSetting(ElectronicScalesConfig config)
        {
            if (config == null)
                throw new ArgumentNullException("setting");

            var data = GetConfig(config.ConfigName);
            if (data != null)
                this._ElectronicScalesConfigs.Remove(data);

            this._ElectronicScalesConfigs.Add(config);
            this.Save();
        }