0

I'm a beginner to serial port programming. I'm using following method in Form1.cs which reads some value from Serial Port.

Note: Port is managed in a singleton class and same object is being used across multiple classes

    private void Form1_Load(object sender, EventArgs e)
    {
                    
        CheckForIllegalCrossThreadCalls = false;
        ComPort.DataReceived += SerialPortDataReceived;
    }

    private void SerialPortDataReceived(object sender, SerialDataReceivedEventArgs e)
    {
        var serialPort = (SerialPort)sender;
        data += serialPort.ReadExisting();
        Console.WriteLine("DEBUG: Data=<" + data + ">");
        SetText(data);
    }

    delegate void SetTextCallback(string text);

    private void SetText(string text)
    {
        
        if (this.rtxtDataArea.InvokeRequired)
        {
            SetTextCallback d = new SetTextCallback(SetText);
            this.Invoke(d, new object[] { text });
        }
        else
        {
            
            if (text.Substring(text.Length - 2, 2) == "##")
            {

               
                Console.WriteLine("DEBUG: Substring= " + text.Substring(0, text.Length - 2));
                if (text.Substring(0, text.Length - 2) == "OK##")
                {

                    CheckForIllegalCrossThreadCalls = false;
                    MessageBox.Show("OK!!! You can login now", "Received Signal", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    
                    this.Hide();
                    Form2 frm2 = new Form2();
                    frm2.ShowDialog();
                }
          }
       }

}

Now this function is working properly and I'm able to see proper output. But on Form2, I've another textbox where I need to receive inputs from Serial Port and when I try the same code on Form2.cs I get nothing in input. It seems that Form1.cs thread is blocking access.

I tried to close this port in Form1 but it just freezes the application.

Craig
  • 2,248
  • 1
  • 19
  • 23
  • have you tried `Dispose()` (not Close) the serial port and create and recreate?, Could you create custom events like `OnSuccesLogin` or similar to avoid close or dispose the port. – karritos Jun 04 '21 at 15:30
  • 1
    Form1 isn't blocking access. I suspect it's reading the data before Form2 gets called. Reading data from a stream is a one-time deal. If you want the data from the serial port to be available to more than one consumer, you'll need to build a storage mechanism that allows that. – glenebob Jun 04 '21 at 15:36
  • It is best to read serial port from a single class. The method can be called from multiple section of you code. I recommending creating an instance of the serial port than can be passed between the forms. See my two form application : https://stackoverflow.com/questions/34975508/reach-control-from-another-page-asp-net – jdweng Jun 04 '21 at 16:28
  • You can have only one handler for the serial port, if you expect reliable results. You _really_ should have the serial port code in a non-UI object, which provides whatever sharing mechanism you prefer so two or more forms can access the data. But given your current implementation, your best approach is to provide a means for the first form to report data received to the second. See duplicate for how to implement that sort of thing. – Peter Duniho Jun 04 '21 at 17:51

0 Answers0