[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

serial ports

Posted on 2008-06-15
4
Medium Priority
?
834 Views
Last Modified: 2013-12-17
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

0
Comment
Question by:klay8
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 19

Expert Comment

by:elimesika
ID: 21790992
0
 
LVL 6

Accepted Solution

by:
Bruce_1975 earned 2000 total points
ID: 21792982
A COM-port can have just one owner. If you are unable to open the port you will get an exception:
a) Port is used by an other application (UnauthorizedAccessException)
b) Port is not existing (IOException)

If you are able to open the port, it is ready to communicate.

Regards,
Bruce
0
 
LVL 3

Expert Comment

by:Pigtor
ID: 21795855
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
0
 
LVL 3

Expert Comment

by:Pigtor
ID: 21795898
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;
            }
        }
0

Featured Post

Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A basic question.. “What is the Garbage Collector?” The usual answer given back: “Garbage collector is a background thread run by the CLR for freeing up the memory space used by the objects which are no longer used by the program.” I wondered …
Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…

649 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question