6

I have the following code which needs the data to be read from port and then display in a textbox. I am using DataReceived event handler for this purpose but donot know how to display this data in textbox. From various sources i learnt that Invoke method should be used for this but donot know how to use it. Suggestions please...

    private void Form1_Load(object sender, EventArgs e)
    {
        //SerialPort mySerialPort = new SerialPort("COM3");
        mySerialPort.PortName = "COM3";
        mySerialPort.BaudRate = 9600;
        mySerialPort.Parity = Parity.None;
        mySerialPort.StopBits = StopBits.One;
        mySerialPort.DataBits = 8;
        mySerialPort.Handshake = Handshake.None;
        mySerialPort.DataReceived += new SerialDataReceivedEventHandler(mySerialPort_DataReceived);
        mySerialPort.Open();
    }

    private void mySerialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
    {
        SerialPort sp = (SerialPort)sender;
        string s= sp.ReadExisting();
        // next i want to display the data in s in a textbox. textbox1.text=s gives a cross thread exception
    }
    private void button1_Click(object sender, EventArgs e)
    {

        mySerialPort.WriteLine("AT+CMGL=\"ALL\"");

    }
Shiridish
  • 4,942
  • 5
  • 33
  • 64
  • i have tried your sample code and build simple C# Solution based on this thread. i couldn't read `DataReceived ` in my modem. I am using wavecom modem to check balance. Help me please to fix it? – aminvincent Sep 14 '15 at 03:20

3 Answers3

14

The MSDN contains a good article with examples about using control methods and properties from other threads.

In short, what you need is a delegate method that sets the Text property of your text box with a given string. You then call that delegate from within your mySerialPort_DataReceived handler via the TextBox.Invoke() method. Something like this:

public delegate void AddDataDelegate(String myString);
public AddDataDelegate myDelegate;

private void Form1_Load(object sender, EventArgs e)
{
    //...
    this.myDelegate = new AddDataDelegate(AddDataMethod);
}

public void AddDataMethod(String myString)
{
    textbox1.AppendText(myString);
}

private void mySerialPort_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
   SerialPort sp = (SerialPort)sender;
   string s= sp.ReadExisting();

   textbox1.Invoke(this.myDelegate, new Object[] {s});       
}
Frank Bollack
  • 24,478
  • 5
  • 49
  • 58
2

Try this (works for me):

private delegate void UpdateUiTextDelegate(string text);

private void Receive(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
{
    if (mySerialPort.IsOpen)
    {
        RxString = mySerialPort.ReadLine();
        Dispatcher.Invoke(DispatcherPriority.Send, new UpdateUiTextDelegate(DisplayText), RxString);
    }
}

private void DisplayText(string RxString)
{
    myTextBox.Text = RxString;
}
KMC
  • 19,548
  • 58
  • 164
  • 253
2

I'm creating a GUI "Form" for USB COM Ports. This is how I send data to the window without getting a "Cross-Thread" error.

private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
    string inData = serialPort1.ReadLine(); // ReadLine includes the + "\n"
    displayToWindow(inData);
}

private void displayToWindow(string inData)
{
    BeginInvoke(new EventHandler(delegate
    {
        richTextBox1.AppendText(inData);
        richTextBox1.ScrollToCaret();
    }));
}
Fixer
  • 23
  • 1
  • 8