4 Replies - 1943 Views - Last Post: 29 May 2012 - 03:29 AM

#1 coden4fun  Icon User is offline

  • if(coden4fun.NotTired){ ContinueToCode(); }
  • member icon

Reputation: 28
  • View blog
  • Posts: 696
  • Joined: 27-February 09

Check available ports

Posted 27 February 2009 - 07:34 PM

Description: Put this in a class by itself, and when on form_load call this, and if it returns true and you've set your Object obj to a Combobox it add all COM ports that are available and can be open for connection. You can replace the Object obj reference with a string[] and return that as well if you want. This is well integrated into our applications and libraries and is excellent! Hope you like.Is checks to see which available COM ports are open using System.IO.Ports then adds the available ports that are open to a combobox. You could replace the combobox to just a string[] and return that. This returns a bool, for it's integrated into our applications, and libraries, but works fine stand alone.
        #region[Check Available Ports]
        public bool IsPortOpen(Object obj)
        {
            //create vars for testing
            bool _available = false;
            SerialPort _tempPort;

            //create a loop for each string in SerialPort.GetPortNames
            foreach (string str in SerialPort.GetPortNames())
            {
                try
                {
                    _tempPort = new SerialPort(str);
                    _tempPort.Open();

                    //if the port exist and we can open it
                    if (_tempPort.IsOpen)
                    {
                        ((ComboBox)obj).Items.Add(str);
                        _tempPort.Close();
                        _available = true;
                    }
                }
                
                //else we have no ports or can't open them display the 
                //precise error of why we either don't have ports or can't open them
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString(), "Error - No Ports available", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    _available = false;
                }
            }
            
            //return the temp bool
            return _available;
        }


Is This A Good Question/Topic? 0
  • +

Replies To: Check available ports

#2 coden4fun  Icon User is offline

  • if(coden4fun.NotTired){ ContinueToCode(); }
  • member icon

Reputation: 28
  • View blog
  • Posts: 696
  • Joined: 27-February 09

Re: Check available ports

Posted 27 February 2009 - 07:34 PM

Description: Put this in a class by itself, and when on form_load call this, and if it returns true and you've set your Object obj to a Combobox it add all COM ports that are available and can be open for connection. You can replace the Object obj reference with a string[] and return that as well if you want. This is well integrated into our applications and libraries and is excellent! Hope you like.This checks to see which available COM ports are open using System.IO.Ports then adds the available ports that are open to a combobox. You could replace the combobox to just a string[] and return that. This returns a bool, for it's integrated into our applications, and libraries, but works fine stand alone.
        #region[Check Available Ports]
        public bool IsPortOpen(Object obj)
        {
            //create vars for testing
            bool _available = false;
            SerialPort _tempPort;

            //create a loop for each string in SerialPort.GetPortNames
            foreach (string str in SerialPort.GetPortNames())
            {
                try
                {
                    _tempPort = new SerialPort(str);
                    _tempPort.Open();

                    //if the port exist and we can open it
                    if (_tempPort.IsOpen)
                    {
                        ((ComboBox)obj).Items.Add(str);
                        _tempPort.Close();
                        _available = true;
                    }
                }
                
                //else we have no ports or can't open them display the 
                //precise error of why we either don't have ports or can't open them
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString(), "Error - No Ports available", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    _available = false;
                }
            }
            
            //return the temp bool
            return _available;
        }

Was This Post Helpful? 0
  • +
  • -

#3 renbell  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 0
  • Joined: 06-April 11

Re: Check available ports

Posted 06 April 2011 - 07:00 AM

How do you call this class? Where is the end of Region?
Was This Post Helpful? 0
  • +
  • -

#4 coden4fun  Icon User is offline

  • if(coden4fun.NotTired){ ContinueToCode(); }
  • member icon

Reputation: 28
  • View blog
  • Posts: 696
  • Joined: 27-February 09

Re: Check available ports

Posted 07 April 2011 - 11:21 PM

forgot to add this. Add it at the end of the last bracket.
Was This Post Helpful? 0
  • +
  • -

#5 sarija  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 0
  • Joined: 29-May 12

Re: Check available ports

Posted 29 May 2012 - 02:59 AM

It is not working. i want to add the list of available ports in combo box. ill attach the codes here pls tell me if any could help
Was This Post Helpful? 0
  • +
  • -

#6 sarija  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 0
  • Joined: 29-May 12

Re: Check available ports

Posted 29 May 2012 - 03:29 AM

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;

namespace WindowsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            string[] ports = SerialPort.GetPortNames();
            
        
        }
            public bool IsPortOpen(Object obj)
            {
                //create vars for testing
                bool _available = false;
                SerialPort _tempPort;

                //create a loop for each string in SerialPort.GetPortNames
                foreach (string str in SerialPort.GetPortNames())
                {
                    try
                    {
                        _tempPort = new SerialPort(str);
                        _tempPort.Open();

                        //if the port exist and we can open it
                        if (_tempPort.IsOpen)
                        {
                            comboBox1.Items.Add(str);
                            _tempPort.Close();
                            _available = true;
                        }
                    }

                    //else we have no ports or can't open them display the 
                    //precise error of why we either don't have ports or can't open them
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.ToString(), "Error - No Ports available", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        _available = false;
                    }
                }

                //return the temp bool
                return _available;
            }
    

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
          
            comboBox1.Items.Add(_tempport); 
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        
        }

}

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1