Exemplo n.º 1
0
        private void BtnGetSerialPorts_Click(object sender, EventArgs e)
        {
            string[] ArrayComPortsNames;
            int      index       = -1;
            string   ComPortName = null;

            cboPorts.Items.Clear();

            try
            {
                ArrayComPortsNames = SerialPort.GetPortNames();
                Array.Sort(ArrayComPortsNames);
                do
                {
                    index += 1;
                    cboPorts.Items.Add(ArrayComPortsNames[index]);
                }while (!((ArrayComPortsNames[index] == ComPortName) ||
                          (index == ArrayComPortsNames.GetUpperBound(0))));

                if (index == ArrayComPortsNames.GetUpperBound(0))
                {
                    ComPortName = ArrayComPortsNames[0];
                }
                cboPorts.Text = ArrayComPortsNames[0];
                PortFlag      = true;
                SetMessage((index + 1) + " COM devices detected.");
            }
            catch
            {
                SetMessage("No COM devices detected...");
                PortFlag = false;
            }
        }
Exemplo n.º 2
0
        private void button_search_maxim_Click(object sender, EventArgs e)
        {
            try
            {
                maximComboBox_port_select.Items.Clear(); //Clears the Combo box
                maximComboBox_port_select.Text = "";
                string[] ArrayComPortsNames;
                int      index       = -1;
                string   ComPortName = "";
                ArrayComPortsNames = SerialPort.GetPortNames(); //Fills the combo box all the available ports on the combo box
                do
                {
                    index += 1;
                    maximComboBox_port_select.Items.Add(ArrayComPortsNames[index]);
                }while (!((ArrayComPortsNames[index] == ComPortName) ||
                          (index == ArrayComPortsNames.GetUpperBound(0))));
                Array.Sort(ArrayComPortsNames); //Sorts all the available com ports

                //want to get first out
                if (index == ArrayComPortsNames.GetUpperBound(0))
                {
                    ComPortName = ArrayComPortsNames[0];
                }
                maximComboBox_port_select.Text = ArrayComPortsNames[0]; //Displays all the available ports on the combo box
            }
            catch (NotImplementedException notImp)
            {
                Console.WriteLine(notImp.Message);
            }

            catch (System.IndexOutOfRangeException ex)
            {
                MessageBox.Show("No COM ports available");
            }
        }
Exemplo n.º 3
0
        // The method reads configuration data from SQL-database
        public static int InitConfig()
        {
            aTimer.Enabled = false;

            // The static method
            byte len = SqlData.QueryPortsConfig(ref config);

            if (len == 0)
            {
                aTimer.Enabled = true;
                log.Warn("No valid configuration data found in MS SQL database");
                return(0x0A);
            }

            string[] ArrayComPortsNames;
            int      index = -1;

            try
            {
                // Call the method to query the current computer for a list of valid serial port
                ArrayComPortsNames = SerialPort.GetPortNames();
            }
            catch (Win32Exception e)
            {
                log.Error(e);
                aTimer.Enabled = true;
                return(0x0B);
            }

            Array.Sort(ArrayComPortsNames);

            byte activePorts = 0;

            // Compare SQL database data with the list of valid serial port
            if (ArrayComPortsNames.GetUpperBound(0) != index)
            {
                do
                {
                    index += 1;
                    for (int i = 0; i < len; i++)
                    {
                        if (ArrayComPortsNames[index] == config[i].portName)
                        {
                            if (comPortArr[i] != null)
                            {
                                // The thread is already running
                                if (comPortArr[i]._continue)
                                {
                                    activePorts++;
                                    break;
                                }
                            }
                            // A new serial port initialization
                            comPortArr[i] = new ComPort(config[i]);
                            comPortArr[i].Open();
                            activePorts++;
                            break;
                        }
                    }
                }while (index != ArrayComPortsNames.GetUpperBound(0));
            }

            if (activePorts == len)
            {
                log.Debug("ODBC connection is established. All serial ports are found!");
                return(0x00);
            }
            else
            {
                log.Error("The serial port with the given name was not detected!");
                aTimer.Enabled = true;
                return(0x0C);
            }
        }