Click here to Skip to main content
15,915,702 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
HI....
I have 2 forms form1&form2. Form1 contains a serial port. This port is opened in form load.
C#
private void Form1_Load(object sender, EventArgs e)
          {
               serialPort1.Open();
          }

I want to write to serial port through form2. For that I created a function in form1 for writing to serial port.

C#
public void SerialPortValueUpdated()
       {
           byte[] head = new byte[1] { 0xAA };
           byte[] trail = new byte[1] { 0x55 };
           byte[] llen = new byte[1] { length };
           // head = Convert.ToByte(0xAA);
           //serialPort1.Open();
           serialPort1.Write(head, 0, 1);
           serialPort1.Write(llen, 0, 1);
           serialPort1.Write(trail, 0, 1);
       }


and called this function from form2 like this

C#
private void button1_Click(object sender, EventArgs e)
{
    Form1 F = new Form1();
    F.SerialPortValueUpdated();
   }

But when I calling this function I get an error that 'Port is closed'. How can I solve this.
Please help me.
Posted
Comments
George Jonsson 11-Sep-14 5:49am    
Why do you have two questions at the same time for what seems to be the same application?
Closing form in serialport datareceived event[^]
Maybe join the two.

Add a try-catch clause in your code.
You might have a hidden error in Form1_Load.

C#
private void Form1_Load(object sender, EventArgs e)
{
    try
    {
        serialPort1.Open();
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.ToString());
    }
}


[Update]
You need to show the form to activate Form_Load.
C#
private void button1_Click(object sender, EventArgs e)
{
    Form1 F = new Form1();
    F.Show();   // Will show a modless dialog.
    F.SerialPortValueUpdated();
}


This is not the best way to start a form with in a form.
Look into using a background worker instead.
BackgroundWorker Class[^]
But it depends what you want to do.

[Update]
For logging and status of your serial communication, I recommend to use Portmon[^] from Sysinternals

Additional reading:
Simple Serial Communication with Microsoft Visual C# Express[^]
How To Work With C# Serial Port Communication[^]
Serial Port Communication In C#[^]
 
Share this answer
 
v5
Comments
Member 10994712 11-Sep-14 3:26am    
I added try catch. But now also I get the same error
George Jonsson 11-Sep-14 3:38am    
See the updated answer.
Member 10994712 11-Sep-14 4:26am    
I tried this. But 'Access to com35 is denied' message get.
George Jonsson 11-Sep-14 4:37am    
That usually means that the port is already open by another application.
Can you open COM35 in a terminal program like HyperTerminal?
Member 10994712 11-Sep-14 4:46am    
NO..I think the problem is that form1 is already opened. I write code form1 f=new form1();
f.show() in form2 load().But form1 is not closing.
In your button1_Click method you are creating a new Form1 object, and calling the serial port method on that new object. But since that object has not gone through form loading it will not contain an open serial port. You need to call the method on the original Form1 object created at the start of the program.
 
Share this answer
 
Comments
Member 10994712 11-Sep-14 3:32am    
can you explain it detail?? I used the same object.
Richard MacCutchan 11-Sep-14 3:59am    
No you didn't, you create a new object in that method so it will not contain an open serialport, as I explained. If you don't understand object lifetimes you need to go and read your study guides.

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