Carlos Barberis

Carlos Barberis

  • 1.7k
  • 11
  • 2.8k

Receiving serial text stream into text box...HELP!

Jul 10 2012 3:32 PM
Hi I am very very new to Visual C# (using Visual C# express) and just created a small serial terminal program to talk to a micro, the terminal is very specific to my application and I would not want to use a regular "terminal program"
The problem I am having is that my textbox1 that receives data from the serial port always starts a line from the upper left hand corner but when it finishes writing that string, the next icoming string just overwrites the previous, i.e.

I receive this string: "hello this is a difficult issue to solve \r\n" followed by this string:"and I cannot seem to figure it out??? \r\n"

I want to see the following on my terminal:

hello this is a difficult issue to solve
and I cannot seem to figure it out???

but all I get is the last string received as the previous gets overwritten:
and I cannot seem to figure it out???

I tried changing the properties of the textbox to multiline, CR=true and whatever else to no avail


Here is a snippet of that part of my code:

******************************************************************************************************


private void button1_Click(object sender, EventArgs e)
{
if (!serialPort1.IsOpen)
{
serialPort1.PortName = "COM2";
serialPort1.DataReceived += new SerialDataReceivedEventHandler(serialPort1_DataReceived);
serialPort1.Open();
textBox1.Text = "Port Active on COM2,Baud=19200,8bits,1sb,NoFlow,NoParity";
}
}

private void serialPort1_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
SerialPort sp = (SerialPort)sender;
string indata = sp.ReadExisting();
this.BeginInvoke(new SetTextDeleg(si_DataReceived), new object[] { indata });

}

private void si_DataReceived(string data)
{
// textBox1.Text = data.Trim();
textBox1.Text = data +"\r\n";
//textBox1.Text = data + Environment.NewLine;
// textBox1.Text = "first line\r\nSecond line";

}
******************************************************************************************************

Any help would be appreciated

Answers (1)