示例#1
0
        public bool SerailComListAdd(string name)
        {
            SerailCom SCom = new SerailCom(name);

            SerailComList.Add(SCom);
            return(true);
        }
示例#2
0
        private void BtnSave_Click(object sender, EventArgs e)
        {
            try
            {
                if (SerialLB.SelectedItem != null)
                {
                    SerailCom SelectPort = SerailMgr.SerailComList[SerialLB.SelectedIndex]; //SerialLB.SelectedItem as Panasonic_PLC;

                    if (SelectPort.DisConnect())                                            //关闭串口
                    {
                        SelectPort.Port.PortName = PortCB.Text.Trim();
                        SelectPort.Port.BaudRate = int.Parse(RateCB.Text.Trim());
                        SelectPort.Port.DataBits = int.Parse(BitCB.Text.Trim());
                        //停止位赋值
                        if (StopBitCB.Text.Trim() == "One")
                        {
                            SelectPort.Port.StopBits = StopBits.One;
                        }
                        else if (StopBitCB.Text.Trim() == "OnePointFive")
                        {
                            SelectPort.Port.StopBits = StopBits.OnePointFive;
                        }
                        else
                        {
                            SelectPort.Port.StopBits = StopBits.Two;
                        }
                        //奇偶校验位赋值
                        if (ParityCB.Text.Trim() == "None")
                        {
                            SelectPort.Port.Parity = Parity.None;
                        }
                        else if (ParityCB.Text.Trim() == "Odd")
                        {
                            SelectPort.Port.Parity = Parity.Odd;
                        }
                        else
                        {
                            SelectPort.Port.Parity = Parity.Even;
                        }

                        SelectPort.Port.ReadTimeout = Convert.ToInt32(RSNUD.Value);

                        if (SelectPort.Connect())//打开串口,如果能够打开则保存数据
                        {
                            SerailMgr.SerailComList_Save();
                            MessageBox.Show(SelectPort.Port.PortName + "保存成功");
                        }
                    }
                }
            }
            catch
            {
                MessageBox.Show("串口数据保存失败,请检查参数及线路是否正确");
            }
        }