public void AddNewPort()
 {
     // 判断当前串口数为多少,如果已经有16个,则退出
     if (base.m_dataTable.Rows.Count == m_listActualPortNumber.Count)
     {
         MessageBox.Show("当前配置已经达到了串口的最大值,无法继续添加串口配置");
         return;
     }
     base.AddRow(new string[]
     {
         "False",
         "",
         CEnumHelper.SerialTransTypeToUIStr(ESerialPortTransType.Nothing),
         CS_None,     /*信道协议*/
         CS_None,     /*数据协议*/
         "9600",
         "8",
         "1",
         CEnumHelper.PortParityTypeToUIStr(EPortParityType.ENone),
         CEnumHelper.SerialPortStreamTypeToUIStr(ESerialPortStreamType.ENone),
         CS_Break_Disabled,
         CS_SwitchStatus_Open, /*默认启用*/
         "-1",
         "-1"                  /*之前的串口号不存在*/
     }, EDataState.ENormal, false);
     base.m_listEditedRows.Add(m_dataTable.Rows.Count - 1);
     UpdatePortsCount(base.m_dataTable.Rows.Count);
     base.ClearSelection();
     base.UpdateDataToUI();
 }
        private static void AddPortSettingsTab()
        {
            if (m_SerialPortTabPage != null)
            {
                m_SerialPortTabPage.DataGrid.MouseDoubleClick -= new MouseEventHandler(SerialPortTabPage_DataGrid_MouseDoubleClick);
                m_tabControlUp.RemovePage(m_SerialPortTabPage);
                m_SerialPortTabPage = null;
            }
            m_SerialPortTabPage = new CDataGridTabPage()
            {
                Title            = "串口配置",
                BTabRectClosable = true,
                //DataGrid = new CDataGridViewSerialPort()
                DataGrid = new CExDataGridView()
                {
                    Header = new string[] { "串口号", "通讯方式", "数据协议", "波特率", "数据位", "停止位", "校验方式", "当前状态" }
                }
            };

            //(m_SerialPortTabPage.DataGrid as CDataGridViewSerialPort).InitDataSource(CDBDataMgr.GetInstance().GetSerialPortProxy());
            //(m_SerialPortTabPage.DataGrid as CDataGridViewSerialPort).LoadData();
            m_SerialPortTabPage.DataGrid.MouseDoubleClick += new MouseEventHandler(SerialPortTabPage_DataGrid_MouseDoubleClick);
            m_SerialPortTabPage.DisabledLeftLabel();
            m_SerialPortTabPage.TabClosed += (s, e) =>
            {
                m_SerialPortTabPage = null;
            };
            m_tabControlUp.AddPage(m_SerialPortTabPage);
            m_tabControlUp.SelectedIndex = m_tabControlUp.TabPages.Count - 1;
            var listPorts = CDBDataMgr.Instance.GetAllSerialPort();

            //  添加串口数据到表格中
            foreach (var item in listPorts)
            {
                int com        = item.PortNumber;
                var channelDll = XmlDocManager.Instance.GetChannelDllByComOrPort(com, true);
                var dataDll    = XmlDocManager.Instance.GetDataDllByComOrPort(com, true);
                if (channelDll == null || dataDll == null)
                {
                    continue;
                }
                m_SerialPortTabPage.DataGrid.AddRow(new string[]
                {
                    "COM" + item.PortNumber,
                    channelDll.Name,
                    dataDll.Name,
                    item.Baudrate.ToString(),
                    item.DataBit.ToString(),
                    item.StopBit.ToString(),
                    CEnumHelper.PortParityTypeToUIStr(item.ParityType).ToString(),
                    (item.SwitchSatus.Value?"开启":"关闭"),
                }, CExDataGridView.EDataState.ENormal);
            }
            //  更新UI
            m_SerialPortTabPage.DataGrid.UpdateDataToUI();
        }
        private void SetPorts(IList <CEntitySerialPort> ports)
        {
            base.m_dataTable.Rows.Clear();
            base.m_dataTableBak.Rows.Clear();
            int        iActualPortCount = 0;
            string     strMessageInfo   = "";
            bool       _bDoDelete       = false; //是否需要修改数据库,默认为false
            List <int> _listDoDelete    = new List <int>();

            foreach (var item in ports)
            {
                if (!m_listActualPortNumber.Contains(item.PortNumber))
                {
                    _bDoDelete = true;
                    _listDoDelete.Add(item.PortNumber); //删除列表
                    strMessageInfo += string.Format("数据库中配置的COM{0}不存在,自动删除\r\n", item.PortNumber);
                    continue;
                }

                // 获取数据协议与信道协议的配置
                CPortProtocolConfig config = GetPortConfig(item.PortNumber);
                if (config == null)
                {
                    config = new CPortProtocolConfig();
                    config.ProtocolChannelName = CS_None;
                    config.ProtocolDataName    = CS_None;
                }
                else
                {
                    if (config.ProtocolDataName == null || config.ProtocolDataName.Equals(""))
                    {
                        config.ProtocolDataName = CS_None;
                    }
                    if (config.ProtocolChannelName == null || config.ProtocolChannelName.Equals(""))
                    {
                        config.ProtocolChannelName = CS_None;
                    }
                }

                base.AddRow(new string[]
                {
                    "False",
                    item.PortNumber.ToString(),
                    CEnumHelper.SerialTransTypeToUIStr(item.TransType),
                    config.ProtocolChannelName,
                    config.ProtocolDataName,
                    item.Baudrate.ToString(),
                    item.DataBit.ToString(),
                    item.StopBit.ToString(),
                    CEnumHelper.PortParityTypeToUIStr(item.ParityType),
                    CEnumHelper.SerialPortStreamTypeToUIStr(item.Stream),
                    item.Break.Value ? CS_Break_Enabled : CS_Break_Disabled,
                    item.SwitchSatus.Value ? CS_SwitchStatus_Open : CS_SwitchStatus_Close,
                    "1",
                    item.PortNumber.ToString()
                }, EDataState.ENormal);
                iActualPortCount += 1;
            }
            UpdatePortsCount(iActualPortCount);
            if (!strMessageInfo.Equals(""))
            {
                MessageBox.Show(strMessageInfo);
                if (_bDoDelete)
                {
                    m_porxy.DeleteRange(_listDoDelete);
                    // 通知其他界面更新消息
                    CDBDataMgr.Instance.UpdateAllSerialPort();
                }
            }
        }
        public void SetEditMode(bool bEnable)
        {
            if (bEnable)
            {
                // 设定标题栏,默认有个隐藏列
                this.Header = new string[]
                {
                    CS_Delete,
                    CS_PortNumber,
                    CS_TransType,
                    CS_ProtocolChannel,
                    CS_ProtocolData,
                    CS_Baudrate,
                    CS_DataBit,
                    CS_StopBit,
                    CS_ParityType,
                    CS_Stream,
                    CS_Break,
                    CS_Open,
                    CS_PID,          /*用来标识是否是添加的串口号*/
                    CS_PrePortNumber /*之前的串口号,用来更改串口号,如果更改串口号,生成两条语句*/
                };
                // this.HideColomns = new int[] { 2, 8, 9, 10, /*11,*/12, 13 };
                this.HideColomns = new int[] { 2, 9, 10, /*11,*/ 12, 13 };
                var delCol = new DataGridViewCheckBoxColumn();
                base.SetColumnEditStyle(0, delCol);

                var portNumberCol = new DataGridViewComboBoxColumn();
                portNumberCol.DisplayStyle = DataGridViewComboBoxDisplayStyle.Nothing;
                // 16个串口

                /*portNumberCol.Items.AddRange(new object[]
                 * {
                 *  "1","2","3","4","5","6","7","8","9","10",
                 *  "11","12","13","14","15","16"
                 * });*/
                foreach (int portnumber in m_listActualPortNumber)
                {
                    portNumberCol.Items.Add(portnumber.ToString());
                }


                base.SetColumnEditStyle(1, portNumberCol);

                var transCol = new DataGridViewComboBoxColumn();
                transCol.DisplayStyle = DataGridViewComboBoxDisplayStyle.Nothing;
                transCol.Items.AddRange(new object[]
                {
                    CEnumHelper.SerialTransTypeToUIStr(ESerialPortTransType.Nothing),
                    CEnumHelper.SerialTransTypeToUIStr(ESerialPortTransType.EUSM),
                    CEnumHelper.SerialTransTypeToUIStr(ESerialPortTransType.ETelephone),
                    CEnumHelper.SerialTransTypeToUIStr(ESerialPortTransType.EBDSatelite),
                    CEnumHelper.SerialTransTypeToUIStr(ESerialPortTransType.EMarisat),
                    CEnumHelper.SerialTransTypeToUIStr(ESerialPortTransType.ESMS)
                });
                base.SetColumnEditStyle(2, transCol);

                // 信道协议
                DataGridViewComboBoxColumn colProtocolChannel = new DataGridViewComboBoxColumn();
                colProtocolChannel.DisplayStyle = DataGridViewComboBoxDisplayStyle.Nothing;
                colProtocolChannel.Items.Add(CS_None);
                // 添加信道协议
                for (int i = 0; i < m_listProtocolCom.Count; ++i)
                {
                    colProtocolChannel.Items.Add(m_listProtocolCom[i]);
                }
                base.SetColumnEditStyle(3, colProtocolChannel);

                // 数据协议
                DataGridViewComboBoxColumn colProtocolData = new DataGridViewComboBoxColumn();
                colProtocolData.DisplayStyle = DataGridViewComboBoxDisplayStyle.Nothing;
                colProtocolData.Items.Add(CS_None);
                // 添加数据协议
                for (int i = 0; i < m_listProtocolData.Count; ++i)
                {
                    colProtocolData.Items.Add(m_listProtocolData[i]);
                }
                base.SetColumnEditStyle(4, colProtocolData);

                //  edit by xiewj
                //var transCol = new DataGridViewComboBoxColumn();
                //transCol.DisplayStyle = DataGridViewComboBoxDisplayStyle.Nothing;
                //var items = Protocol.Manager.XmlDocManager.Instance.ChannelProtocolNames;
                //foreach (var item in items)
                //{
                //    transCol.Items.Add(item);
                //}
                //base.SetColumnEditStyle(2, transCol);

                var baudrateCol = new DataGridViewComboBoxColumn();
                baudrateCol.DisplayStyle = DataGridViewComboBoxDisplayStyle.Nothing;
                baudrateCol.Items.AddRange(new object[]
                {
                    /* "300","600",*/ "1200", "2400", "4800", "9600", "19200", "38400", "57600", "115200", "230400", "460800", "921600"
                });
                base.SetColumnEditStyle(5, baudrateCol);

                var dataBitCol = new DataGridViewComboBoxColumn();
                dataBitCol.DisplayStyle = DataGridViewComboBoxDisplayStyle.Nothing;
                dataBitCol.Items.AddRange(new object[]
                {
                    "8", "7", "6"
                });
                base.SetColumnEditStyle(6, dataBitCol);

                var stopBitCol = new DataGridViewComboBoxColumn();
                stopBitCol.DisplayStyle = DataGridViewComboBoxDisplayStyle.Nothing;
                stopBitCol.Items.AddRange(new object[]
                {
                    "1", "2"
                });
                base.SetColumnEditStyle(7, stopBitCol);

                //CS_ParityType ,
                //    CS_Stream ,
                //    CS_Break,
                //    CS_Open ,
                //    CS_PID

                var parityCol = new DataGridViewComboBoxColumn();
                parityCol.DisplayStyle = DataGridViewComboBoxDisplayStyle.Nothing;
                parityCol.Items.AddRange(new object[]
                {
                    CEnumHelper.PortParityTypeToUIStr(EPortParityType.ENone),
                    CEnumHelper.PortParityTypeToUIStr(EPortParityType.EEven),
                    CEnumHelper.PortParityTypeToUIStr(EPortParityType.EOdd)
                });
                base.SetColumnEditStyle(8, parityCol);

                var streamCol = new DataGridViewComboBoxColumn();
                streamCol.DisplayStyle = DataGridViewComboBoxDisplayStyle.Nothing;
                streamCol.Items.AddRange(new object[]
                {
                    CEnumHelper.SerialPortStreamTypeToUIStr(ESerialPortStreamType.ENone),
                    CEnumHelper.SerialPortStreamTypeToUIStr(ESerialPortStreamType.ERts),
                    CEnumHelper.SerialPortStreamTypeToUIStr(ESerialPortStreamType.EDtr),
                });
                base.SetColumnEditStyle(9, streamCol);

                var breakCol = new DataGridViewComboBoxColumn();
                breakCol.DisplayStyle = DataGridViewComboBoxDisplayStyle.Nothing;
                breakCol.Items.AddRange(new object[] { CS_Break_Disabled,
                                                       CS_Break_Enabled, });
                base.SetColumnEditStyle(10, breakCol);

                var openCol = new DataGridViewComboBoxColumn();
                openCol.DisplayStyle = DataGridViewComboBoxDisplayStyle.Nothing;
                openCol.Items.AddRange(new object[] { CS_SwitchStatus_Open,
                                                      CS_SwitchStatus_Close, });
                base.SetColumnEditStyle(11, openCol);

                var pidCol = new DataGridViewTextBoxColumn();
                base.SetColumnEditStyle(12, pidCol);
            }
            else
            {
                this.Header = new string[]
                {
                    CS_Delete,
                    CS_PortNumber,
                    CS_TransType,
                    CS_ProtocolChannel,
                    CS_ProtocolData,
                    CS_Baudrate,
                    CS_DataBit,
                    CS_StopBit,
                    CS_ParityType,
                    CS_Stream,
                    CS_Break,
                    CS_Open,
                    CS_PID,          /*用来标识是否是添加的串口号*/
                    CS_PrePortNumber /*之前的串口号,用来更改串口号,如果更改串口号,生成两条语句*/
                };
            }
        }