Question

I have a main (Form1) Class, and a second class which handles all the Serial Communication (ComPort)

When a new data is received through the Serial port (Event), I'd like to pass it to the Form1 and do some manipulation on this data. Little Example: Creating long string from received data

Form1.cs

public partial class Form1 : Form  
{  
//Creating instance of SerialCommunication.
....
....
string receivedString = ""; //Here I will store all data

public void addNewDataMethod(string newDataFromSerial)
{ 
  receivedString = receivedString + newDataFromSerial;
  MessageBox.Show(receivedString);
}

}

SerialCommunication.cs

public partial class SerialCommunicationPort   
    {  
      constructor()  
      {      
       .... //open serial port with the relevant parameters  
       ComPort.DataReceived += new SerialDataReceivedEventHandler(ComPortBufferRead);//Create Handler  
      }  
     public void ComPortBufferRead(object sender, SerialDataReceivedEventArgs e)  
     {  
      //create array to store buffer data  
      byte[] inputData = new byte[ComPort.BytesToRead];  
      //read the data from buffer  
      ComPort.Read(inputData, 0, ComPort.BytesToRead);  

    //*****  What should I write here in order to  pass "inputData" to Form1.addNewDataMethod ?


    }  
  }

I Tried the following:

Form1 Form;  
Form1.addNewDataMethod(inputData.ToString());

The above code will generate an error: use of unassigned local variable "Form"

Form1 Form1 = new Form1(); 
Form1.addNewDataMethod(inputData.ToString());

the above will create a new instance of Form1, and will not contain the previous received data.

Any Suggestions ?

Was it helpful?

Solution

Create an event in SerialCommunication class which will be raised when data is arrived:

public partial class SerialCommunicationPort   
{ 
    public event Action<byte[]> DataReceived;

    public void ComPortBufferRead(object sender, SerialDataReceivedEventArgs e)  
    {       
       byte[] inputData = new byte[ComPort.BytesToRead];  
       ComPort.Read(inputData, 0, ComPort.BytesToRead);  

       if (DataReceived != null)
           DataReceived(inputData);
    }  
}

Then subscribe to this event in your form.

public partial class Form1 : Form  
{  
    SerialCommunication serialCommunication;

    public Form1()
    {
        InitializeComponent();
        serialCommunication = new SerialCommunication();
        serialCommunication.DataReceived += SerialCommunication_DataReceived;
    } 

    private void SerialCommunication_DataReceived(byte[] data)
    {
        // get string from byte array 
        // and call addNewDataMethod
    }
}

If you want to follow Microsoft coding guidelines for WinForms, then instead of Action<byte[]> delegate you should use EventHandler<DataReceivedEventArgs> delegate, where DataReceivedEventArgs is class inherited from EventArgs.

OTHER TIPS

probably the best way would be to use events. Define an event which would an EventArgs parameter that would take a string. In Form1 subscribe to the event and in your serial communication class just trigger the event passing the string you want to the eventargs.

The way you wanted should work as well, but something like this:

Form1 Form = new Form1();  
Form.Show(); // to display it...
/// to other stuff...
Form.addNewDataMethod(inputData); // call the method on the INSTANCE which you opened!

similar to usage of methods, you could define a property in Form1 :

private string _myString = "";
public string MyString {
    private get {   
}
set {
    string _myString= value;
   // to with the string what you want
}
}

and then similar to the method call use

Form.MyString = "abc";

Hope this help!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top