0

I have a serial port and button is in my main form.I want that, when I click on button then it open another form and set serial port properties.for example stopbits and baudrate and ..... How can I access to serialport properties in form2? How can I write cods?.I Write this code

 //Form1

    public partial class Form1 : Form

{
    public Form1()
    {
        InitializeComponent();
    }

  public SerialPort port = new SerialPort("COM1",
      9600, Parity.None, 8, StopBits.One);
.
.
.
    private void button1_Click(object sender, EventArgs e)
    {
        Form2 frm = new Form2();
        frm.Show();
    }


}
// Form2

public partial class Form2 : Form
{
    public Form2()
    {
        InitializeComponent();
    }




    private void button1_Click(object sender, EventArgs e)
    {
               Form1 frm = new Form1();
               frm.port.BaudRate=9600;


    }
}

but this cod can't change BaudRate in Form1. can help me?

sm k
  • 37
  • 1
  • 8
  • 3
    Have you tried anything already? Where did you stuck, what problems are you facing? – Andrey Korneyev May 24 '17 at 08:57
  • "how can I access to serialport properties in form2" you need to pass the `SerialPort` object to the form2. Its like asking "how can my brother access my cell phone?" the answer would be you need to give your brother your cell phone... :) – Mong Zhu May 24 '17 at 08:59
  • Serial ports are special, they cannot be shared. Or in other words, there can only ever be one SerialPort instance for a port. So that gives you a good reason to declare the variable `static`. Now it is dead simple, just `Form1.port` – Hans Passant May 24 '17 at 11:12
  • thank you very much.so is this true? public static SerialPort port = new SerialPort("COM1", 9600, Parity.None, 8, StopBits.One); – sm k May 24 '17 at 20:28

0 Answers0