none
C# How to access the Serial IO Port from Multiple Forms. My main form doesn't close the port, when the other forms need it. RRS feed

  • Question

  •  

    Hi Experts,

            I seek your help to resolve this problem, i have a main form which wld be shown at all times when my application is launched & from there the users may open other forms whenever needed.

            Previously only the other forms in my appln needed to get data from the Serial Port & i was handling it by opening the serialport when that form is opened & closing back the serial port once the form is closed.

            But now i have a need that my Main form also needs to wait for a data from the serial port at all times &  if any of the other froms are opened, the main form which wld never be closed unless the appln is closed should be able to close the serial port & give way for the other form to open the serial port for its data collection.

            The problem i face is my other forms cant access the serial port as the Main form doesn't close the serial port even though i'm no more using the main form & using other form..

    Would like to have some inputs from you Experts as how to close the Serial port when others forms are opened & reopen the serial port of Main form once other forms have closed.

    Thanks & lot in advance..

     

    My code looks like this

     

    in my Main Form i have coded as below

    private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)

    {

    Thread.Sleep(200);

    Application.DoEvents();

    string buf = serialPort1.ReadExisting().Trim();

    ProcessScannedData(buf);

    }

    private void MainFrm_Leave(object sender, EventArgs e)

    {

    if (serialPort1.IsOpen)

    serialPort1.Close();

    }

     

    in my Other forms i have a code like this

    private void Form2_Load(object sender, EventArgs e)

    {

    serialPort1.PortName = "COM1";

    serialPort1.BaudRate = 9600;

    serialPort1.DataBits = 8;

    serialPort1.StopBits = StopBits.One;

    serialPort1.Parity = Parity.None;

    if (!serialPort1.IsOpen)

    serialPort1.Open();

    }

    private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)

    {

    this.BeginInvoke(new EventHandler(GetPortData));

    }

    private void Form2_FormClosed(object sender, FormClosedEventArgs e)

    {

    if (serialPort1.IsOpen)

    serialPort1.Close();

    }


     

    Friday, May 9, 2008 5:53 AM

Answers

  • Best thing to do is to *not* close the port but let your main form tell other forms that it got something from the serial port.  Use an event to tell them that.  For example:

      public partial class Form1 : Form {
        public delegate void MyDataReceivedEventHandler(string data);
        public event MyDataReceivedEventHandler DataReceived;
        private MyDataReceivedEventHandler uiHelper;

        public Form1() {
          InitializeComponent();
          serialPort1.DataReceived += serialPort1_DataReceived;
          uiHelper = new MyDataReceivedEventHandler(uiHelper);
          // Open the port...
        }

        private void DataReceivedOnUI(string data) {
          // Tell the client forms about it
          if (DataReceived != null) DataReceived(data);
        }

        private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e) {
          string data = serialPort1.ReadExisting();
          // So that client forms don't have to use Invoke:
          this.BeginInvoke(uiHelper, data);
        }

    In order to let the client forms subscribe to the DataReceived event, they need a reference to the main form:

        private void button1_Click(object sender, EventArgs e) {
          new Form2(this);
        }

    and the sample client form:

      public partial class Form2 : Form {
        private Form1 mMain;
        public Form2(Form1 main) {
          InitializeComponent();
          mMain = main;
          mMain.DataReceived += myDataReceived;
          this.FormClosing += form2_Closing;
        }
        private void myDataReceived(string data) {
          // Do something with <data>
        }
        private void form2_Closing(object sender, EventArgs e) {
          // No longer want events
          mMain.DataReceived -= myDataReceived;
        }
      }
    Saturday, May 10, 2008 12:51 PM
    Moderator

All replies

  • Best thing to do is to *not* close the port but let your main form tell other forms that it got something from the serial port.  Use an event to tell them that.  For example:

      public partial class Form1 : Form {
        public delegate void MyDataReceivedEventHandler(string data);
        public event MyDataReceivedEventHandler DataReceived;
        private MyDataReceivedEventHandler uiHelper;

        public Form1() {
          InitializeComponent();
          serialPort1.DataReceived += serialPort1_DataReceived;
          uiHelper = new MyDataReceivedEventHandler(uiHelper);
          // Open the port...
        }

        private void DataReceivedOnUI(string data) {
          // Tell the client forms about it
          if (DataReceived != null) DataReceived(data);
        }

        private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e) {
          string data = serialPort1.ReadExisting();
          // So that client forms don't have to use Invoke:
          this.BeginInvoke(uiHelper, data);
        }

    In order to let the client forms subscribe to the DataReceived event, they need a reference to the main form:

        private void button1_Click(object sender, EventArgs e) {
          new Form2(this);
        }

    and the sample client form:

      public partial class Form2 : Form {
        private Form1 mMain;
        public Form2(Form1 main) {
          InitializeComponent();
          mMain = main;
          mMain.DataReceived += myDataReceived;
          this.FormClosing += form2_Closing;
        }
        private void myDataReceived(string data) {
          // Do something with <data>
        }
        private void form2_Closing(object sender, EventArgs e) {
          // No longer want events
          mMain.DataReceived -= myDataReceived;
        }
      }
    Saturday, May 10, 2008 12:51 PM
    Moderator
  • I am using Visual C#.

    In mine code "this" argument is null. ArgumentException: Delegate to an instance method cannot have null 'this'. How assign it?



    Tuesday, July 10, 2018 10:22 AM