Link to home
Start Free TrialLog in
Avatar of klay8
klay8

asked on

serial ports

hi
 i have to detect 3 status of a serial port in my application :
1. i opened the serialport and it's ready for use
2.the serial port is exsiting but it's busy and not by my appliccation
3.the serial port does not exist..

how can i do this in c# or C++ ,?
thanks
regard
this->serialPort1= gcnew System::IO::Ports::SerialPort("COM1",57600,System::IO::Ports::Parity::None,8,System::IO::Ports::StopBits::One);
				this->serialPort2= gcnew System::IO::Ports::SerialPort("COM2",57600,System::IO::Ports::Parity::None,8,System::IO::Ports::StopBits::One);
				this->serialPort3= gcnew System::IO::Ports::SerialPort("COM3",57600,System::IO::Ports::Parity::None,8,System::IO::Ports::StopBits::One);
				try{
					if (this->serialPort1->IsOpen) this->label1->Text="COM1 Busy";
					else {			 
						serialPort1->Open();
						if(serialPort1->IsOpen)
							this->label1->Text="COM1 is ready for use";
					}
							
					}
				catch(...)
				{
					this->label1->Text="COM1 is not existing";
				}
				try{
					if (this->serialPort2->IsOpen) this->label2->Text="COM2 Busy";
					else {			 
						serialPort2->Open();
						if(serialPort2->IsOpen)
							this->label2->Text="COM2 is ready for use";
					}
							
					}
				catch(...)
				{
					this->label2->Text="COM2 is not existing";
				}
				try{
					if (this->serialPort3->IsOpen) this->label3->Text="COM3 Busy";
					else {			 
						serialPort3->Open();
						if(serialPort3->IsOpen)
							this->label3->Text="COM3 is ready for use";
					}
							
					}
				catch(...)
				{
					this->label3->Text="COM3 is not existing";
				}

Open in new window

Avatar of elimesika
elimesika
Flag of Israel image

ASKER CERTIFIED SOLUTION
Avatar of Bruce_1975
Bruce_1975

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
1.  The OK status or Ready to use can be determined only when you have no exception raised during the Open method.
try
{
    ...  
port.Open();
//AT THIS POINT YOUR SERIAL PORT IS OPEN AND READY TO USE, OTHERWISE AN EXCEPTION WAS RAISED.
//YOU CAN ALSO CHECK THE "port.IsOpen" PROPERTY
}
catch (System.Exception ex)
{
//THE PORT WAS NOT OPEN
}

2.   As Bruce_1975 says, you should open the serialport and check for an UnauthorizedAccessException, but you should also check for an InvalidOperationException.  Check the msdn documentation at:
http://msdn.microsoft.com/en-us/library/system.io.ports.serialport.open.aspx

3.  To check whether a serial port exists, use the System.IO.Ports.SerialPort.GetPortNames() to retrieve all the available ports in the machine.  With this validation you can determine whether the user port selection was invalid.
If you are not using .NET 2.0, the serial port always send the error: "Invalid port number"


I hope this is what you are looking for
This function will return the state (1, 2 or 3)

        public int CheckSerialPort(System.IO.Ports.SerialPort port)
        {
            try
            {
                string[] ports = System.IO.Ports.SerialPort.GetPortNames();
                if (System.Array.IndexOf(ports, port.PortName) == -1) return 3;
                port.Open();
                return 1;
            }
            catch (System.Exception ex)
            {
                System.Windows.Forms.MessageBox.Show(ex.Message, port.PortName, System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error);
                return 2;
            }
        }