Click here to Skip to main content
15,890,579 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have following code for open serial port:

comPort = new SerialPort();
comPort.DataReceived += new                      SerialDataReceivedEventHandler(comPort_DataReceived);
//set the properties of our SerialPort Object
comPort.BaudRate = int.Parse(_baudRate);
comPort.DataBits = int.Parse(_dataBits);
comPort.StopBits = (StopBits)Enum.Parse(typeof(StopBits), _stopBits);
comPort.Parity = (Parity)Enum.Parse(typeof(Parity), _parity);
comPort.PortName = _portName;
comPort.Open();

From another form i set all values.
Now i want when i press close button from another page i want to close comport.

I use comport.close(); but it not work.
Posted
Updated 5-Dec-11 0:43am
v2
Comments
Rajesh Anuhya 5-Dec-11 6:44am    
What is the Error you are getting?
jaideepsinh 5-Dec-11 8:23am    
There is no error but after i open comport then its already opend.

As I remember, this[^] answer helps me year ago.
 
Share this answer
 
Comments
jaideepsinh 5-Dec-11 7:12am    
i use this but not help me.
I found solution i can't close port which is open because from which form i open port and set all values i closed that form after process so when ever i use port it give default value so now i an not close that form i just hide that form so now i can access open port in all other form.
if (comPort.IsOpen)
{
comPort.Close();
}
 
Share this answer
 
v2
It's not terribly clear, but it sounds as if you have one form that "owns" the comPort object, and another form that controls it. It would seem reasonable, then, to add an event handler to the owner form that subscribes to the btnClose.Click event on the controlling form. This handler would then call comPort.Close().
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900