/// <summary>
        /// 初始化数据
        /// </summary>
        private void InitUI(StopwatchConfig config)
        {
            txtConfigName.Text = _StopwatchControl.ConfigName;//码表名称
            txtConfigName.Enabled = false;

            cmbType.Properties.Items.AddEnum(typeof(StopwatchType));
            cmbType.EditValue = _StopwatchControl.StopwatchType;

            cbxUnit.Properties.Items.AddEnum(typeof(StopwatchUnit));
            cbxUnit.EditValue = _StopwatchControl.StopwatchUnit;

            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;

            txtRatio.EditValue = config.Ratio;


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

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

            this._StopwatchConfigs.Add(config);
            this.Save();
        }
示例#3
0
        public StopwatchConfig GetConfig()
        {
            var config = new StopwatchConfig();
            config.ConfigName = this.ConfigName;
            config.StopwatchType = this.StopwatchType;
            config.Ratio = this.Ratio;
            config.StopwatchUnit = this.StopwatchUnit;

            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;
        }
示例#4
0
        public void ApplyConfig(StopwatchConfig config)
        {
            if (config == null)
                throw new ArgumentNullException("config");

            this.ConfigName = config.ConfigName;
            this.StopwatchType = config.StopwatchType;
            this.Ratio = config.Ratio;
            this.StopwatchUnit = config.StopwatchUnit;

            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;
            }

        }
        //点击保存
        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;
            }

            StopwatchConfig bs = new StopwatchConfig();
            bs.ConfigName = txtConfigName.Text;
            bs.StopwatchType = (StopwatchType)cmbType.EditValue;
            bs.PortName = cmbPortName.EditValue.ToStringEx();
            bs.BaudRate = Convert.ToInt32(txtBautTate.Text);
            bs.DataBits = Convert.ToInt32(txtDataBits.Text);
            bs.Parity = (Parity)cmbParity.EditValue;
            bs.StopBits = (StopBits)cmbStopBit.EditValue;
            bs.Handshake = (Handshake)cmbHandshake.EditValue;
            bs.StopBits = (StopBits)cmbStopBit.EditValue;
            bs.Ratio = txtRatio.Value;
            StopwatchConfigManager.Current.AddSetting(bs);

            MessageService.ShowMessage("配置保存成功。");
            this.DialogResult = DialogResult.OK;
        }