Exemplo n.º 1
0
        public static List<COMPortInfo> GetCOMPortsInfo()
        {
            List<COMPortInfo> comPortInfoList = new List<COMPortInfo>();

            ConnectionOptions options = ProcessConnectionOptions();
            ManagementScope connectionScope = ConnectionScope(Environment.MachineName, options, @"\root\CIMV2");

            ObjectQuery objectQuery = new ObjectQuery("SELECT * FROM Win32_PnPEntity WHERE ConfigManagerErrorCode = 0");
            ManagementObjectSearcher comPortSearcher = new ManagementObjectSearcher(connectionScope, objectQuery);

            using (comPortSearcher)
            {
                string caption = null;
                foreach (ManagementObject obj in comPortSearcher.Get())
                {
                    if (obj != null)
                    {
                        object captionObj = obj["Caption"];
                        if (captionObj != null)
                        {
                            caption = captionObj.ToString();
                            if (caption.Contains("(COM"))
                            {
                                COMPortInfo comPortInfo = new COMPortInfo();
                                comPortInfo.Name = caption.Substring(caption.LastIndexOf("(COM")).Replace("(", string.Empty).Replace(")",
                                                                     string.Empty);
                                comPortInfo.Description = caption;
                                comPortInfo.DeviceID = obj["DeviceID"].ToString();

                                comPortInfoList.Add(comPortInfo);
                            }
                        }
                    }
                }
            }
            return comPortInfoList;
        }
Exemplo n.º 2
0
        private void timThietBi()
        {
            dgvDevice.Columns.Clear();

            DataTable dtDevice = new DataTable();

            // Tao cau truc datatable
            dtDevice.Columns.Add("Chọn", typeof(bool));
            dtDevice.Columns.Add("Cổng", typeof(string));
            dtDevice.Columns.Add("Mạng", typeof(string));
            dtDevice.Columns.Add("Độ trễ", typeof(int));
            dtDevice.Columns.Add("Giới hạn", typeof(int));

            foreach (COMPortInfo comPort in COMPortInfo.GetCOMPortsInfo())
            {
                if (comPort.Description.Contains("PC UI Interface"))
                {
                    string portName = comPort.Name;
                    //Open communication port
                    SerialPort sPort = objclsSMS.OpenPort(portName, Convert.ToInt32(this.cboBaudRate.Text), Convert.ToInt32(this.cboDataBits.Text), Convert.ToInt32(this.txtReadTimeOut.Text), Convert.ToInt32(this.txtWriteTimeOut.Text));
                    if (sPort != null)
                    {
                        string copSim = objclsSMS.readSimCode(sPort);
                        if ("\"45201\"".Equals(copSim))
                        {
                            SettingDevice setting = ReadXML(envPath + "\\" + "Mobifone" + ".xml");
                            if (setting != null)
                            {
                                dtDevice.Rows.Add(false, portName, setting.Cops, setting.DelayTime.ToString(), setting.Limit.ToString());
                            }
                            else
                            {
                                dtDevice.Rows.Add(false, portName, "Mobifone", "0", "0");
                            }
                        }
                        if ("\"45202\"".Equals(copSim))
                        {
                            SettingDevice setting = ReadXML(envPath + "\\" + "Vinaphone" + ".xml");
                            if (setting != null)
                            {
                                dtDevice.Rows.Add(false, portName, setting.Cops, setting.DelayTime.ToString(), setting.Limit.ToString());
                            }
                            else
                            {
                                dtDevice.Rows.Add(false, portName, "Vinaphone", "0", "0");
                            }
                        }
                        if ("\"45204\"".Equals(copSim))
                        {
                            SettingDevice setting = ReadXML(envPath + "\\" + "Viettel" + ".xml");
                            if (setting != null)
                            {
                                dtDevice.Rows.Add(false, portName, setting.Cops, setting.DelayTime.ToString(), setting.Limit.ToString());
                            }
                            else
                            {
                                dtDevice.Rows.Add(false, portName, "Viettel", "0", "0");
                            }
                        }
                    }

                    else
                    {
                        //MessageBox.Show("Invalid port settings");
                        this.statusBar1.Text = "Invalid port settings";
                    }
                    objclsSMS.ClosePort(sPort);
                }
                dgvDevice.DataSource = dtDevice;
                dgvDevice.Refresh();
            }

            DataGridViewButtonColumn btnSetup = new DataGridViewButtonColumn();

            dgvDevice.Columns.Add(btnSetup);
            btnSetup.Text = "Cài đặt";
            btnSetup.Name = "";
            btnSetup.UseColumnTextForButtonValue = true;
        }